Skip to content

Commit

Permalink
scaling the angle for the real servo
Browse files Browse the repository at this point in the history
  • Loading branch information
bacalj committed Mar 28, 2024
1 parent 3103bc4 commit b2209fc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/models/stores/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ export class SerialDevice {

public writeToOutForServo(n:number, liveOutputType: string){
const outputConfig = NodeLiveOutputTypes.find(o => o.name === liveOutputType);

Check warning on line 174 in src/models/stores/serial.ts

View check run for this annotation

Codecov / codecov/patch

src/models/stores/serial.ts#L173-L174

Added lines #L173 - L174 were not covered by tests
if (this.hasPort() && outputConfig?.angleBase !== undefined){
const constrainedAngle = Math.min(180, Math.max(0, n));
this.writer.write(`${constrainedAngle.toString()}\n`);
if (this.hasPort() && outputConfig?.angleOffset !== undefined){
const scaledAngle = (outputConfig.angleScale * n) + outputConfig.angleOffset;
const roundedScaled = Math.round(scaledAngle);

Check warning on line 177 in src/models/stores/serial.ts

View check run for this annotation

Codecov / codecov/patch

src/models/stores/serial.ts#L176-L177

Added lines #L176 - L177 were not covered by tests
//console.log("| GOT:", n, "| SENDING :", roundedScaled);
this.writer.write(`${roundedScaled.toString()}\n`);

Check warning on line 179 in src/models/stores/serial.ts

View check run for this annotation

Codecov / codecov/patch

src/models/stores/serial.ts#L179

Added line #L179 was not covered by tests
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/dataflow/model/utilities/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ export const NodeLiveOutputTypes = [
name: "Servo",
icon: ServoIcon,
angleBase: 0,
angleOffset: 30,
angleScale: 2 / 3,
sweep: 180
}
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ export class LiveOutputReteNodeFactory extends DataflowReteNodeFactory {
}

if(kServoOutputTypes.includes(outputType)){
// TODO: test with real servo and invalid value, does it go to 0 or 180, or not move at all?
// if goes from zero to 180, then we can just constrain, like this:
// angles out of range are set to the nearest valid value
newValue = Math.min(Math.max(newValue, 0), 180);

Check warning on line 65 in src/plugins/dataflow/nodes/factories/live-output-rete-node-factory.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dataflow/nodes/factories/live-output-rete-node-factory.tsx#L65

Added line #L65 was not covered by tests
nodeValue?.setDisplayMessage(`${newValue}°`);

// otherwise, we need to check if the value is valid,
// and if not, we need to set it to the last valid value
// leaving the alternative approach in place if needed
// this just will not move if given an invalid value
// const isValidServoValue = newValue >= 0 && newValue <= 180;
// if (!isValidServoValue) newValue = getLastValidServoValue(_node);
}
Expand Down

0 comments on commit b2209fc

Please sign in to comment.