added test framework (#113)

* added test framework

* added toString on motors

* enabling logs
This commit is contained in:
Peli de Halleux
2017-12-19 07:07:50 -08:00
committed by GitHub
parent 60bf3a17d3
commit 9e427898ae
10 changed files with 311 additions and 135 deletions

View File

@ -43,7 +43,7 @@
"brick.setStatusLight|param|pattern": "the lights pattern to use.",
"brick.showImage": "Shows an image on screen",
"brick.showImage|param|image": "image to draw",
"console": "Reading and writing data to the console output.\r\n\r\nReading and writing data to the console output.",
"console": "Reading and writing data to the console output.\n\nReading and writing data to the console output.",
"console.addListener": "Adds a listener for the log messages",
"console.log": "Write a line of text to the console output.",
"console.logValue": "Write a name:value pair as a line of text to the console output.",
@ -58,23 +58,24 @@
"control.raiseEvent": "Announce that an event happened to registered handlers.",
"control.raiseEvent|param|src": "ID of the Component that generated the event",
"control.raiseEvent|param|value": "Component specific code indicating the cause of the event.",
"motors.Motor.isReady": "Returns a value indicating if the motor is still running a previous command.",
"motors.Motor.move": "Moves the motor by a number of rotations, degress or seconds",
"motors.Motor.move|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.Motor.move|param|unit": "the meaning of the value",
"motors.Motor.move|param|value": "the move quantity, eg: 2",
"motors.Motor.pauseUntilReady": "Pauses the execution until the previous command finished.",
"motors.Motor.pauseUntilReady|param|timeOut": "optional maximum pausing time in milliseconds",
"motors.Motor.reset": "Resets the motor(s).",
"motors.Motor.setBrake": "Sets the automatic brake on or off when the motor is off",
"motors.Motor.setBrake|param|brake": "a value indicating if the motor should break when off",
"motors.Motor.setReversed": "Reverses the motor polarity",
"motors.Motor.setSpeed": "Sets the speed of the motor.",
"motors.Motor.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.Motor.stop": "Stops the motor(s).",
"motors.SingleMotor.angle": "Gets motor ration angle.",
"motors.SingleMotor.clearCount": "Clears the motor count",
"motors.SingleMotor.speed": "Gets motor actual speed.",
"motors.Motor.angle": "Gets motor ration angle.",
"motors.Motor.clearCount": "Clears the motor count",
"motors.Motor.speed": "Gets motor actual speed.",
"motors.Motor.toString": "Returns the status of the motor",
"motors.MotorBase.isReady": "Returns a value indicating if the motor is still running a previous command.",
"motors.MotorBase.move": "Moves the motor by a number of rotations, degress or seconds",
"motors.MotorBase.move|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.MotorBase.move|param|unit": "the meaning of the value",
"motors.MotorBase.move|param|value": "the move quantity, eg: 2",
"motors.MotorBase.pauseUntilReady": "Pauses the execution until the previous command finished.",
"motors.MotorBase.pauseUntilReady|param|timeOut": "optional maximum pausing time in milliseconds",
"motors.MotorBase.reset": "Resets the motor(s).",
"motors.MotorBase.setBrake": "Sets the automatic brake on or off when the motor is off",
"motors.MotorBase.setBrake|param|brake": "a value indicating if the motor should break when off",
"motors.MotorBase.setReversed": "Reverses the motor polarity",
"motors.MotorBase.setSpeed": "Sets the speed of the motor.",
"motors.MotorBase.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.MotorBase.stop": "Stops the motor(s).",
"motors.SynchedMotorPair.steer": "Turns the motor and the follower motor by a number of rotations",
"motors.SynchedMotorPair.steer|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.SynchedMotorPair.steer|param|steering": "the ratio of power sent to the follower motor, from ``-100`` to ``100``",
@ -84,7 +85,17 @@
"motors.SynchedMotorPair.tank|param|speedRight": "the speed on the right motor, eg: 50",
"motors.SynchedMotorPair.tank|param|unit": "@param speedLeft the speed on the left motor, eg: 50",
"motors.SynchedMotorPair.tank|param|value": "the amount of movement, eg: 2",
"motors.SynchedMotorPair.toString": "Returns the name(s) of the motor",
"motors.mkCmd": "Allocates a message buffer",
"motors.mkCmd|param|addSize": "required additional bytes",
"motors.mkCmd|param|cmd": "command id",
"motors.mkCmd|param|out": "ports",
"motors.readPWM": "Sends and receives a message from the motors device",
"motors.readPWM|param|buf": "message buffer",
"motors.resetAllMotors": "Resets all motors",
"motors.stopAllMotors": "Stops all motors",
"motors.writePWM": "Sends a command to the motors device",
"motors.writePWM|param|buf": "the command buffer",
"output.createBuffer": "Create a new zero-initialized buffer.",
"output.createBuffer|param|size": "number of bytes in the buffer",
"screen.clear": "Clear screen and reset font to normal.",

View File

@ -47,13 +47,13 @@
"console|block": "console",
"control.raiseEvent|block": "raise event|from %src|with value %value",
"control|block": "control",
"motors.Motor.move|block": "move `icons.motorLarge` %motor|for %value|%unit|at %speed|%",
"motors.Motor.pauseUntilReady|block": "%motor|pause until ready",
"motors.Motor.setBrake|block": "set `icons.motorLarge` %motor|brake %brake",
"motors.Motor.setReversed|block": "set `icons.motorLarge` %motor|reversed %reversed",
"motors.Motor.setSpeed|block": "set speed of `icons.motorLarge` %motor|to %speed|%",
"motors.SingleMotor.angle|block": "`icons.motorLarge` %motor|angle",
"motors.SingleMotor.speed|block": "`icons.motorLarge` %motor|speed",
"motors.Motor.angle|block": "`icons.motorLarge` %motor|angle",
"motors.Motor.speed|block": "`icons.motorLarge` %motor|speed",
"motors.MotorBase.move|block": "move `icons.motorLarge` %motor|for %value|%unit|at %speed|%",
"motors.MotorBase.pauseUntilReady|block": "%motor|pause until ready",
"motors.MotorBase.setBrake|block": "set `icons.motorLarge` %motor|brake %brake",
"motors.MotorBase.setReversed|block": "set `icons.motorLarge` %motor|reversed %reversed",
"motors.MotorBase.setSpeed|block": "set speed of `icons.motorLarge` %motor|to %speed|%",
"motors.SynchedMotorPair.steer|block": "steer %chassis|%steering|%|at speed %speed|%|by %value|%unit",
"motors.SynchedMotorPair.tank|block": "tank %chassis|left %speedLeft|%|right %speedRight|%|by %value|%unit",
"motors.largeAB|block": "large A+B",

View File

@ -53,32 +53,56 @@ namespace motors {
motorMM = control.mmap("/dev/lms_motor", MotorDataOff.Size * DAL.NUM_OUTPUTS, 0)
if (!motorMM) control.fail("no motor file")
resetMotors()
resetAllMotors()
let buf = output.createBuffer(1)
const buf = output.createBuffer(1)
buf[0] = DAL.opProgramStart
writePWM(buf)
}
function writePWM(buf: Buffer): void {
/**
* Sends a command to the motors device
* @param buf the command buffer
*/
//%
export function writePWM(buf: Buffer): void {
init()
pwmMM.write(buf)
}
function readPWM(buf: Buffer): number {
/**
* Sends and receives a message from the motors device
* @param buf message buffer
*/
//%
export function readPWM(buf: Buffer): number {
init()
return pwmMM.read(buf);
}
function mkCmd(out: Output, cmd: number, addSize: number) {
/**
* Allocates a message buffer
* @param out ports
* @param cmd command id
* @param addSize required additional bytes
*/
//%
export function mkCmd(out: Output, cmd: number, addSize: number) {
const b = output.createBuffer(2 + addSize)
b.setNumber(NumberFormat.UInt8LE, 0, cmd)
b.setNumber(NumberFormat.UInt8LE, 1, out)
return b
}
function resetMotors() {
reset(Output.ALL)
function outputToName(out: Output): string {
let r = "";
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
if (out & (1 << i)) {
if (r.length > 0) r += "+";
r += "ABCD"[i];
}
}
return r;
}
/**
@ -92,9 +116,18 @@ namespace motors {
writePWM(b)
}
/**
* Resets all motors
*/
//% group="Motion"
export function resetAllMotors() {
reset(Output.ALL)
}
//% fixedInstances
export class Motor extends control.Component {
export class MotorBase extends control.Component {
protected _port: Output;
protected _portName: string;
protected _brake: boolean;
private _initialized: boolean;
private _init: () => void;
@ -104,6 +137,7 @@ namespace motors {
constructor(port: Output, init: () => void, setSpeed: (speed: number) => void, move: (steps: boolean, stepsOrTime: number, speed: number) => void) {
super();
this._port = port;
this._portName = outputToName(this._port);
this._brake = false;
this._initialized = false;
this._init = init;
@ -244,7 +278,7 @@ namespace motors {
}
//% fixedInstances
export class SingleMotor extends Motor {
export class Motor extends MotorBase {
private _large: boolean;
constructor(port: Output, large: boolean) {
@ -322,34 +356,42 @@ namespace motors {
}
}
}
/**
* Returns the status of the motor
*/
//%
toString(): string {
return `${this._large ? "" : "M"}${this._portName} ${this.speed()}% ${this.angle()}>`;
}
}
//% whenUsed fixedInstance block="large A"
export const largeA = new SingleMotor(Output.A, true);
export const largeA = new Motor(Output.A, true);
//% whenUsed fixedInstance block="large B"
export const largeB = new SingleMotor(Output.B, true);
export const largeB = new Motor(Output.B, true);
//% whenUsed fixedInstance block="large C"
export const largeC = new SingleMotor(Output.C, true);
export const largeC = new Motor(Output.C, true);
//% whenUsed fixedInstance block="large D"
export const largeD = new SingleMotor(Output.D, true);
export const largeD = new Motor(Output.D, true);
//% whenUsed fixedInstance block="medium A"
export const mediumA = new SingleMotor(Output.A, false);
export const mediumA = new Motor(Output.A, false);
//% whenUsed fixedInstance block="medium B"
export const mediumB = new SingleMotor(Output.B, false);
export const mediumB = new Motor(Output.B, false);
//% whenUsed fixedInstance block="medium C"
export const mediumC = new SingleMotor(Output.C, false);
export const mediumC = new Motor(Output.C, false);
//% whenUsed fixedInstance block="medium D"
export const mediumD = new SingleMotor(Output.D, false);
export const mediumD = new Motor(Output.D, false);
//% fixedInstances
export class SynchedMotorPair extends Motor {
export class SynchedMotorPair extends MotorBase {
constructor(ports: Output) {
super(ports, () => this.__init(), (speed) => this.__setSpeed(speed), (steps, stepsOrTime, speed) => this.__move(steps, stepsOrTime, speed));
@ -458,6 +500,20 @@ namespace motors {
const steering = (speedRight * 100 / speedLeft) >> 0;
this.steer(speedLeft, steering, value, unit);
}
/**
* Returns the name(s) of the motor
*/
//%
toString(): string {
let r = outputToName(this._port);
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
if (this._port & (1 << i)) {
r += ` ${getMotorData(1 << i).actualSpeed}%`
}
}
return r;
}
}
//% whenUsed fixedInstance block="large B+C"