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

@@ -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"