Skip to content

Commit

Permalink
Send encoder and gyro data
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhiquan Yeo committed Oct 18, 2023
1 parent f1fea28 commit b7918d5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
43 changes: 43 additions & 0 deletions public/js/xrp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const ROBOT_SIZE = 40; // 40px
const ARENA_WIDTH = 800;
const ARENA_HEIGHT = 500;

// Robot is 10 physical inches,
// thus if we've moved 40px, we've moved 10 inches

const INCH_PER_PIXEL = 0.5 / 40;
const DISTANCE_PER_WHEEL_REV_IN = 2.3622 * Math.PI;
const ENCODER_COUNT_PER_PIXEL = DISTANCE_PER_WHEEL_REV_IN * INCH_PER_PIXEL * 585;

class Robot {
constructor() {
this._position = {
Expand All @@ -14,6 +21,9 @@ class Robot {
this._leftSpeed = 0;
this._rightSpeed = 0;
this._bearing = 0;

this._leftEncoder = 0;
this._rightEncoder = 0;
}

get robotDivPosition() {
Expand Down Expand Up @@ -82,9 +92,22 @@ class Robot {
this._rightSpeed = val;
}

get leftEncoderValue() {
return this._leftEncoder;
}

get rightEncoderValue() {
return this._rightEncoder;
}

resetPosition() {
this._leftEncoder = 0;
this._rightEncoder = 0;
this._leftSpeed = 0;
this._rightSpeed = 0;
this.position = { x: ARENA_WIDTH / 2, y: ARENA_HEIGHT / 2};
this.bearing = 0;

}

update() {
Expand All @@ -98,6 +121,13 @@ class Robot {
_processTick(timeDelta) {
const timeInSec = timeDelta / 1000;

// The speed values are in "pixels per second"
const leftDelta = this._leftSpeed * timeInSec;
const rightDelta = this._rightSpeed * timeInSec;

this._leftEncoder += leftDelta * ENCODER_COUNT_PER_PIXEL;
this._rightEncoder += rightDelta * ENCODER_COUNT_PER_PIXEL;

const newPos = _calculateNewPosition(this._position, this._bearing,
this._leftSpeed, this._rightSpeed, timeInSec);

Expand Down Expand Up @@ -199,4 +229,17 @@ setInterval(() => {
_lastTime = currTime;
}, 10);

setInterval(() => {
if (shouldSendSensorData) {
ws.send(JSON.stringify({
type: "SensorData",
payload: {
heading: robot.bearing,
leftEncoder: robot.leftEncoderValue,
rightEncoder: robot.rightEncoderValue
}
}))
}
}, 50);

};
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ wsServer.on("connection", (ws, request) => {

ws.on("message", (data) => {
// Sensor data TODO
try {
const messageJson = JSON.parse(data.toString());

if (messageJson.type === "SensorData") {
const payload = messageJson.payload;
xrp.setHeading(payload.heading);
xrp.setLeftEncoderValue(payload.leftEncoder);
xrp.setRightEncoderValue(payload.rightEncoder);

}
} catch (err) {

}
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/xrp-sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default class XRPSim {
}

public setHeading(hdg: number) {
console.log("Heading: ", hdg);
this._xrpState.setHeading(hdg);
}

Expand All @@ -133,6 +134,7 @@ export default class XRPSim {

private _sendToWPILib() {
if (this._remoteSocketInfo !== null) {
// console.log("sendToWPILib ", this._xrpState.getLeftEncoder(), this._xrpState.getRightEncoder());
const header = Buffer.alloc(3);
header.writeUInt16BE(this._outboundSeq, 0);

Expand Down
8 changes: 8 additions & 0 deletions src/xrp-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ export default class XRPState {
this._encoderInputs.set(0, val);
}

public getLeftEncoder(): number {
return this._encoderInputs.get(0) || 0;
}

public setRightEncoder(val: number) {
this._encoderInputs.set(1, val);
}

public getRightEncoder(): number {
return this._encoderInputs.get(1) || 0;
}

public setHeading(val: number) {
this._heading = val;
}
Expand Down

0 comments on commit b7918d5

Please sign in to comment.