Merge "Adjust field to be drawn at new 0,0."
diff --git a/aos/network/web_proxy_main.cc b/aos/network/web_proxy_main.cc
index 0e49760..c674520 100644
--- a/aos/network/web_proxy_main.cc
+++ b/aos/network/web_proxy_main.cc
@@ -24,7 +24,6 @@
 
   LOG(INFO) << "My node is " << aos::FlatbufferToJson(self);
 
-  // TODO(alex): skip fetchers on the wrong node.
   for (uint i = 0; i < config.message().channels()->size(); ++i) {
     auto channel = config.message().channels()->Get(i);
     if (aos::configuration::ChannelIsReadableOnNode(channel, self)) {
diff --git a/aos/network/www/config_handler.ts b/aos/network/www/config_handler.ts
index 3d20f0d..6615f39 100644
--- a/aos/network/www/config_handler.ts
+++ b/aos/network/www/config_handler.ts
@@ -3,13 +3,14 @@
 import {Connection} from './proxy';
 
 export class ConfigHandler {
-  private readonly root_div = document.getElementById('config');
+  private readonly root_div = document.createElement('div');
   private readonly tree_div;
   private config: Configuration|null = null
 
   constructor(private readonly connection: Connection) {
     this.connection.addConfigHandler((config) => this.handleConfig(config));
 
+    document.body.appendChild(this.root_div);
     const show_button = document.createElement('button');
     show_button.addEventListener('click', () => this.toggleConfig());
     const show_text = document.createTextNode('Show/Hide Config');
diff --git a/aos/network/www/proxy.ts b/aos/network/www/proxy.ts
index a675340..7bcf575 100644
--- a/aos/network/www/proxy.ts
+++ b/aos/network/www/proxy.ts
@@ -66,6 +66,11 @@
     this.configHandlers.add(handler);
   }
 
+  /**
+   * Add a handler for a specific message type. Until we need to handle
+   * different channel names with the same type differently, this is good
+   * enough.
+   */
   addHandler(id: string, handler: (data: Uint8Array) => void): void {
     this.handlerFuncs.set(id, handler);
   }
@@ -183,7 +188,8 @@
   }
 
   /**
-   * Subscribes to messages.
+   * Subscribes to messages. Only the most recent connect message is in use. Any
+   * channels not specified in the message are implicitely unsubscribed.
    * @param a Finished flatbuffer.Builder containing a Connect message to send.
    */
   sendConnectMessage(builder: any) {
diff --git a/y2020/www/BUILD b/y2020/www/BUILD
index e8c227d..a035d84 100644
--- a/y2020/www/BUILD
+++ b/y2020/www/BUILD
@@ -25,6 +25,7 @@
     ],
     deps = [
         "//aos/network/www:proxy",
+        "//y2020/vision/sift:sift_ts_fbs",
     ],
 )
 
diff --git a/y2020/www/field.html b/y2020/www/field.html
index 37452a3..ad449db 100644
--- a/y2020/www/field.html
+++ b/y2020/www/field.html
@@ -5,8 +5,6 @@
     <link rel="stylesheet" href="styles.css">
   </head>
   <body>
-    <div id="config">
-    </div>
   </body>
 </html>
 
diff --git a/y2020/www/field_handler.ts b/y2020/www/field_handler.ts
index a960a63..f05f5d5 100644
--- a/y2020/www/field_handler.ts
+++ b/y2020/www/field_handler.ts
@@ -1,33 +1,36 @@
+import {Configuration, Channel} from 'aos/configuration_generated';
+import {Connection} from 'aos/network/www/proxy';
+import {Connect} from 'aos/network/connect_generated';
 import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
+import {ImageMatchResult} from 'y2020/vision/sift/sift_generated'
 
+// (0,0) is field center, +X is toward red DS
 const FIELD_SIDE_Y = FIELD_WIDTH / 2;
-const FIELD_CENTER_X = (198.75 + 116) * IN_TO_M;
+const FIELD_EDGE_X = FIELD_LENGTH / 2;
 
 const DS_WIDTH = 69 * IN_TO_M;
 const DS_ANGLE = 20 * Math.PI / 180;
-const DS_END_X = DS_WIDTH * Math.sin(DS_ANGLE);
-const OTHER_DS_X = FIELD_LENGTH - DS_END_X;
+const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
 const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
 
-const TRENCH_START_X = 206.57 * IN_TO_M;
-const TRENCH_END_X = FIELD_LENGTH - TRENCH_START_X;
+const TRENCH_X = 108 * IN_TO_M;
 const TRENCH_WIDTH = 55.5 * IN_TO_M;
 const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
 
 const SPINNER_LENGTH = 30 * IN_TO_M;
-const SPINNER_TOP_X = 374.59 * IN_TO_M;
+const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
 const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
 
-const SHIELD_BOTTOM_X = FIELD_CENTER_X - 116 * IN_TO_M;
+const SHIELD_BOTTOM_X = -116 * IN_TO_M;
 const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
 
-const SHIELD_TOP_X = FIELD_CENTER_X + 116 * IN_TO_M;
+const SHIELD_TOP_X = 116 * IN_TO_M;
 const SHIELD_TOP_Y = -43.75 * IN_TO_M;
 
-const SHIELD_RIGHT_X = FIELD_CENTER_X - 51.06 * IN_TO_M;
+const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
 const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
 
-const SHIELD_LEFT_X = FIELD_CENTER_X + 51.06 * IN_TO_M;
+const SHIELD_LEFT_X = 51.06 * IN_TO_M;
 const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
 
 const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
@@ -36,18 +39,78 @@
 const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
 const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
 
-const INITIATION_X = 120 * IN_TO_M;
-const FAR_INITIATION_X = FIELD_LENGTH - 120 * IN_TO_M;
+const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
 
-const TARGET_ZONE_TIP_X = 30 * IN_TO_M;
+const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
 const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
 const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
 
+/**
+ * All the messages that are required to display camera information on the field.
+ * Messages not readable on the server node are ignored.
+ */
+const REQUIRED_CHANNELS = [
+  {
+    name: '/pi1/camera',
+    type: 'frc971.vision.sift.ImageMatchResult',
+  },
+  {
+    name: '/pi2/camera',
+    type: 'frc971.vision.sift.ImageMatchResult',
+  },
+  {
+    name: '/pi3/camera',
+    type: 'frc971.vision.sift.ImageMatchResult',
+  },
+  {
+    name: '/pi4/camera',
+    type: 'frc971.vision.sift.ImageMatchResult',
+  },
+  {
+    name: '/pi5/camera',
+    type: 'frc971.vision.sift.ImageMatchResult',
+  },
+];
+
 export class FieldHandler {
   private canvas = document.createElement('canvas');
+  private imageMatchResult :ImageMatchResult|null = null
 
-  constructor() {
+  constructor(private readonly connection: Connection) {
     document.body.appendChild(this.canvas);
+
+    this.connection.addConfigHandler(() => {
+      this.sendConnect();
+    });
+    this.connection.addHandler(ImageMatchResult.getFullyQualifiedName(), (res) => {
+      this.handleImageMatchResult(res);
+    });
+  }
+
+  private handleImageMatchResult(data: Uint8Array): void {
+    const fbBuffer = new flatbuffers.ByteBuffer(data);
+    this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult(fbBuffer);
+  }
+
+  private sendConnect(): void {
+    const builder = new flatbuffers.Builder(512);
+    const channels: flatbuffers.Offset[] = [];
+    for (const channel of REQUIRED_CHANNELS) {
+      const nameFb = builder.createString(channel.name);
+      const typeFb = builder.createString(channel.type);
+      Channel.startChannel(builder);
+      Channel.addName(builder, nameFb);
+      Channel.addType(builder, typeFb);
+      const channelFb = Channel.endChannel(builder);
+      channels.push(channelFb);
+    }
+
+    const channelsFb = Connect.createChannelsToTransferVector(builder, channels);
+    Connect.startConnect(builder);
+    Connect.addChannelsToTransfer(builder, channelsFb);
+    const connect = Connect.endConnect(builder);
+    builder.finish(connect);
+    this.connection.sendConnectMessage(builder);
   }
 
   drawField(): void {
@@ -56,15 +119,15 @@
     const ctx = this.canvas.getContext('2d');
     // draw perimiter
     ctx.beginPath();
-    ctx.moveTo(0, DS_INSIDE_Y);
+    ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
     ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
-    ctx.lineTo(OTHER_DS_X, FIELD_SIDE_Y);
-    ctx.lineTo(FIELD_LENGTH, DS_INSIDE_Y);
-    ctx.lineTo(FIELD_LENGTH, -DS_INSIDE_Y);
-    ctx.lineTo(OTHER_DS_X, -FIELD_SIDE_Y);
+    ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
+    ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
+    ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
+    ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
     ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
-    ctx.lineTo(0, -DS_INSIDE_Y);
-    ctx.lineTo(0, DS_INSIDE_Y);
+    ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
+    ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
     ctx.stroke();
 
     // draw shield generator
@@ -78,21 +141,20 @@
     ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
     ctx.stroke();
 
-    // draw trenches
-    ctx.strokeStyle = MY_COLOR;
-    ctx.beginPath();
-    ctx.moveTo(TRENCH_START_X, FIELD_SIDE_Y);
-    ctx.lineTo(TRENCH_START_X, TRENCH_INSIDE);
-    ctx.lineTo(TRENCH_END_X, TRENCH_INSIDE);
-    ctx.lineTo(TRENCH_END_X, FIELD_SIDE_Y);
-    ctx.stroke();
+    this.drawHalfField(ctx, 'red');
+    ctx.rotate(Math.PI);
+    this.drawHalfField(ctx, 'blue');
+    ctx.rotate(Math.PI);
+  }
 
-    ctx.strokeStyle = OTHER_COLOR;
+  drawHalfField(ctx, color: string): void {
+    // trenches
+    ctx.strokeStyle = color;
     ctx.beginPath();
-    ctx.moveTo(TRENCH_START_X, -FIELD_SIDE_Y);
-    ctx.lineTo(TRENCH_START_X, -TRENCH_INSIDE);
-    ctx.lineTo(TRENCH_END_X, -TRENCH_INSIDE);
-    ctx.lineTo(TRENCH_END_X, -FIELD_SIDE_Y);
+    ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
+    ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
+    ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
+    ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
     ctx.stroke();
 
     ctx.strokeStyle = 'black';
@@ -101,46 +163,59 @@
     ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
     ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
     ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
-    ctx.moveTo(FIELD_LENGTH - SPINNER_TOP_X, -FIELD_SIDE_Y);
-    ctx.lineTo(FIELD_LENGTH - SPINNER_TOP_X, -TRENCH_INSIDE);
-    ctx.lineTo(FIELD_LENGTH - SPINNER_BOTTOM_X, -TRENCH_INSIDE);
-    ctx.lineTo(FIELD_LENGTH - SPINNER_BOTTOM_X, -FIELD_SIDE_Y);
     ctx.stroke();
 
-    // draw initiation lines
     ctx.beginPath();
     ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
     ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
-    ctx.moveTo(FAR_INITIATION_X, FIELD_SIDE_Y);
-    ctx.lineTo(FAR_INITIATION_X, -FIELD_SIDE_Y);
     ctx.stroke();
 
-    // draw target/loading zones
-    ctx.strokeStyle = MY_COLOR;
+    // target/loading
+    ctx.strokeStyle = color;
     ctx.beginPath();
-    ctx.moveTo(0, DS_INSIDE_Y);
+    ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
     ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
-    ctx.lineTo(0, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
+    ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
 
-    ctx.moveTo(FIELD_LENGTH, DS_INSIDE_Y);
-    ctx.lineTo(
-        FIELD_LENGTH - TARGET_ZONE_TIP_X,
-        DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
-    ctx.lineTo(FIELD_LENGTH, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
+    ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
+    ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
+    ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
     ctx.stroke();
+  }
 
-    ctx.strokeStyle = OTHER_COLOR;
+  drawCamera(x: number, y: number, theta: number): void {
+    const ctx = this.canvas.getContext('2d');
+    ctx.save();
+    ctx.translate(x, y);
+    ctx.rotate(theta);
     ctx.beginPath();
-    ctx.moveTo(0, -DS_INSIDE_Y);
-    ctx.lineTo(TARGET_ZONE_TIP_X, -(DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH));
-    ctx.lineTo(0, -(DS_INSIDE_Y - LOADING_ZONE_WIDTH));
-
-    ctx.moveTo(FIELD_LENGTH, -DS_INSIDE_Y);
-    ctx.lineTo(
-        FIELD_LENGTH - TARGET_ZONE_TIP_X,
-        -(DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH));
-    ctx.lineTo(FIELD_LENGTH, -(DS_INSIDE_Y - TARGET_ZONE_WIDTH));
+    ctx.moveTo(0.5, 0.5);
+    ctx.lineTo(0, 0);
+    ctx.lineTo(0.5, -0.5);
     ctx.stroke();
+    ctx.beginPath();
+    ctx.arc(0, 0, 0.25, -Math.PI/4, Math.PI/4);
+    ctx.stroke();
+    ctx.restore();
+  }
+
+  draw(): void  {
+    this.reset();
+    this.drawField();
+    //draw cameras
+    if (this.imageMatchResult) {
+      console.log(this.imageMatchResult.cameraPosesLength());
+      for (const i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) {
+        const pose = this.imageMatchResult.cameraPoses(i);
+        const mat = pose.fieldToCamera();
+        const x = mat.data(3);
+        const y = mat.data(7);
+        this.drawCamera(x, y, 0);
+        console.log(x, y);
+      }
+    }
+
+    window.requestAnimationFrame(() => this.draw());
   }
 
   reset(): void {
@@ -148,17 +223,17 @@
     ctx.setTransform(1, 0, 0, 1, 0, 0);
     const size = window.innerHeight * 0.9;
     ctx.canvas.height = size;
-    ctx.canvas.width = size / 2 + 10;
-    ctx.clearRect(0, 0, size, size / 2 + 10);
+    const width = size / 2 + 20;
+    ctx.canvas.width = width;
+    ctx.clearRect(0, 0, size, width);
 
-    // Translate to center of bottom of display.
-    ctx.translate(size / 4, size);
+    // Translate to center of display.
+    ctx.translate(width / 2, size / 2);
     // Coordinate system is:
     // x -> forward.
     // y -> to the left.
     ctx.rotate(-Math.PI / 2);
     ctx.scale(1, -1);
-    ctx.translate(5, 0);
 
     const M_TO_PX = (size - 10) / FIELD_LENGTH;
     ctx.scale(M_TO_PX, M_TO_PX);
diff --git a/y2020/www/field_main.ts b/y2020/www/field_main.ts
index adcaa27..163c817 100644
--- a/y2020/www/field_main.ts
+++ b/y2020/www/field_main.ts
@@ -6,8 +6,7 @@
 
 conn.connect();
 
-const fieldHandler = new FieldHandler();
+const fieldHandler = new FieldHandler(conn);
 
-fieldHandler.reset();
-fieldHandler.drawField();
+fieldHandler.draw();
 
diff --git a/y2020/www/index.html b/y2020/www/index.html
index 97e16d4..20b9785 100644
--- a/y2020/www/index.html
+++ b/y2020/www/index.html
@@ -5,7 +5,5 @@
     <link rel="stylesheet" href="styles.css">
   </head>
   <body>
-    <div id="config">
-    </div>
   </body>
 </html>