2017-07-07 16:15:36 +02:00
|
|
|
enum Output {
|
2017-10-25 05:16:33 +02:00
|
|
|
//% block="A"
|
2017-07-07 16:15:36 +02:00
|
|
|
A = 0x01,
|
2017-10-25 05:16:33 +02:00
|
|
|
//% block="B"
|
2017-07-07 16:15:36 +02:00
|
|
|
B = 0x02,
|
2017-10-25 05:16:33 +02:00
|
|
|
//% block="C"
|
2017-07-07 16:15:36 +02:00
|
|
|
C = 0x04,
|
2017-10-25 05:16:33 +02:00
|
|
|
//% block="D"
|
2017-07-07 16:15:36 +02:00
|
|
|
D = 0x08,
|
2017-12-12 23:08:45 +01:00
|
|
|
//% block="B+C"
|
2017-12-15 02:01:23 +01:00
|
|
|
BC = Output.B | Output.C,
|
|
|
|
//% block="A+B"
|
|
|
|
AB = Output.A | Output.B,
|
|
|
|
//% block="C+D"
|
|
|
|
CD = Output.C | Output.D,
|
|
|
|
//% block="A+D"
|
2018-01-03 01:20:00 +01:00
|
|
|
AD = Output.A | Output.D,
|
2017-10-25 05:16:33 +02:00
|
|
|
//% block="All"
|
2017-07-07 16:15:36 +02:00
|
|
|
ALL = 0x0f
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OutputType {
|
|
|
|
None = 0,
|
|
|
|
Tacho = 7,
|
|
|
|
MiniTacho = 8,
|
|
|
|
}
|
|
|
|
|
2017-12-14 07:40:40 +01:00
|
|
|
enum MoveUnit {
|
|
|
|
//% block="rotations"
|
|
|
|
Rotations,
|
|
|
|
//% block="degrees"
|
|
|
|
Degrees,
|
2018-01-06 03:24:23 +01:00
|
|
|
//% block="seconds"
|
|
|
|
Seconds,
|
2017-12-18 23:13:38 +01:00
|
|
|
//% block="milliseconds"
|
|
|
|
MilliSeconds
|
2017-12-14 07:40:40 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 15:22:12 +02:00
|
|
|
enum MovePhase {
|
|
|
|
//% block="acceleration"
|
|
|
|
Acceleration,
|
|
|
|
//% block="deceleration"
|
|
|
|
Deceleration
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:01:11 +02:00
|
|
|
namespace motors {
|
2017-07-11 16:58:51 +02:00
|
|
|
let pwmMM: MMap
|
|
|
|
let motorMM: MMap
|
|
|
|
|
|
|
|
const enum MotorDataOff {
|
|
|
|
TachoCounts = 0, // int32
|
|
|
|
Speed = 4, // int8
|
|
|
|
Padding = 5, // int8[3]
|
|
|
|
TachoSensor = 8, // int32
|
|
|
|
Size = 12
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
if (pwmMM) return
|
|
|
|
pwmMM = control.mmap("/dev/lms_pwm", 0, 0)
|
|
|
|
if (!pwmMM) control.fail("no PWM file")
|
|
|
|
motorMM = control.mmap("/dev/lms_motor", MotorDataOff.Size * DAL.NUM_OUTPUTS, 0)
|
2017-12-13 23:55:14 +01:00
|
|
|
if (!motorMM) control.fail("no motor file")
|
2017-08-08 11:41:47 +02:00
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
resetAllMotors()
|
2017-08-09 19:02:58 +02:00
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
const buf = output.createBuffer(1)
|
2017-07-11 16:58:51 +02:00
|
|
|
buf[0] = DAL.opProgramStart
|
|
|
|
writePWM(buf)
|
|
|
|
}
|
2017-10-27 09:09:00 +02:00
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
/**
|
|
|
|
* Sends a command to the motors device
|
|
|
|
* @param buf the command buffer
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
export function writePWM(buf: Buffer): void {
|
2017-07-11 16:58:51 +02:00
|
|
|
init()
|
|
|
|
pwmMM.write(buf)
|
|
|
|
}
|
2017-07-07 16:15:36 +02:00
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
/**
|
|
|
|
* Sends and receives a message from the motors device
|
|
|
|
* @param buf message buffer
|
|
|
|
*/
|
|
|
|
//%
|
2017-12-19 22:10:40 +01:00
|
|
|
export function readPWM(buf: Buffer): void {
|
2017-08-08 02:39:37 +02:00
|
|
|
init()
|
2017-12-19 22:10:40 +01:00
|
|
|
pwmMM.read(buf);
|
2017-08-08 02:39:37 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-10-27 20:01:11 +02:00
|
|
|
const b = output.createBuffer(2 + addSize)
|
2017-07-07 16:15:36 +02:00
|
|
|
b.setNumber(NumberFormat.UInt8LE, 0, cmd)
|
|
|
|
b.setNumber(NumberFormat.UInt8LE, 1, out)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:27:37 +01:00
|
|
|
export function outputToName(out: Output): string {
|
2017-12-19 16:07:50 +01:00
|
|
|
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;
|
2017-10-27 09:09:00 +02:00
|
|
|
}
|
2017-10-25 05:16:33 +02:00
|
|
|
|
2017-10-27 11:52:42 +02:00
|
|
|
/**
|
|
|
|
* Stops all motors
|
|
|
|
*/
|
2017-12-14 22:07:10 +01:00
|
|
|
//% blockId=motorStopAll block="stop all motors"
|
2018-01-11 07:29:35 +01:00
|
|
|
//% weight=1
|
2018-01-08 05:33:02 +01:00
|
|
|
//% group="Move"
|
2018-02-14 17:56:12 +01:00
|
|
|
//% help=motors/stop-all
|
|
|
|
export function stopAll() {
|
2017-10-27 11:52:42 +02:00
|
|
|
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
2019-09-17 20:23:40 +02:00
|
|
|
writePWM(b);
|
|
|
|
pause(1);
|
2017-10-27 11:52:42 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
/**
|
|
|
|
* Resets all motors
|
|
|
|
*/
|
2018-01-08 05:33:02 +01:00
|
|
|
//% group="Move"
|
2017-12-19 16:07:50 +01:00
|
|
|
export function resetAllMotors() {
|
|
|
|
reset(Output.ALL)
|
2019-09-17 20:23:40 +02:00
|
|
|
pause(1);
|
2017-12-19 16:07:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 05:57:23 +02:00
|
|
|
interface MoveSchedule {
|
|
|
|
speed: number;
|
|
|
|
useSteps: boolean;
|
|
|
|
steps: number[];
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:16:33 +02:00
|
|
|
//% fixedInstances
|
2017-12-19 16:07:50 +01:00
|
|
|
export class MotorBase extends control.Component {
|
2017-12-18 07:47:13 +01:00
|
|
|
protected _port: Output;
|
2017-12-19 16:07:50 +01:00
|
|
|
protected _portName: string;
|
2017-12-18 07:47:13 +01:00
|
|
|
protected _brake: boolean;
|
2019-09-03 05:57:23 +02:00
|
|
|
protected _regulated: boolean;
|
2018-06-01 17:51:50 +02:00
|
|
|
private _pauseOnRun: boolean;
|
2017-12-18 17:54:53 +01:00
|
|
|
private _initialized: boolean;
|
2019-08-27 23:57:28 +02:00
|
|
|
private _brakeSettleTime: number;
|
2017-12-18 17:54:53 +01:00
|
|
|
private _init: () => void;
|
2019-09-03 05:57:23 +02:00
|
|
|
private _accelerationSteps: number;
|
|
|
|
private _accelerationTime: number;
|
|
|
|
private _decelerationSteps: number;
|
|
|
|
private _decelerationTime: number;
|
2017-12-18 17:54:53 +01:00
|
|
|
|
2018-12-04 01:37:53 +01:00
|
|
|
protected static output_types: number[] = [0x7, 0x7, 0x7, 0x7];
|
|
|
|
|
2019-09-03 05:57:23 +02:00
|
|
|
constructor(port: Output, init: () => void) {
|
2017-10-25 05:16:33 +02:00
|
|
|
super();
|
2017-12-14 07:40:40 +01:00
|
|
|
this._port = port;
|
2017-12-19 16:07:50 +01:00
|
|
|
this._portName = outputToName(this._port);
|
2017-12-14 07:40:40 +01:00
|
|
|
this._brake = false;
|
2019-09-03 05:57:23 +02:00
|
|
|
this._regulated = true;
|
2018-06-01 17:51:50 +02:00
|
|
|
this._pauseOnRun = true;
|
2017-12-18 17:54:53 +01:00
|
|
|
this._initialized = false;
|
2019-08-27 23:57:28 +02:00
|
|
|
this._brakeSettleTime = 10;
|
2017-12-18 17:54:53 +01:00
|
|
|
this._init = init;
|
2019-09-03 05:57:23 +02:00
|
|
|
this._accelerationSteps = 0;
|
|
|
|
this._accelerationTime = 0;
|
|
|
|
this._decelerationSteps = 0;
|
|
|
|
this._decelerationTime = 0;
|
2017-12-14 07:40:40 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
/**
|
|
|
|
* Lazy initialization code
|
2017-12-15 02:01:23 +01:00
|
|
|
*/
|
2017-12-18 17:54:53 +01:00
|
|
|
protected init() {
|
|
|
|
if (!this._initialized) {
|
|
|
|
this._initialized = true;
|
|
|
|
this._init();
|
|
|
|
}
|
2017-12-15 02:01:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
/**
|
|
|
|
* Sets the automatic brake on or off when the motor is off
|
|
|
|
* @param brake a value indicating if the motor should break when off
|
|
|
|
*/
|
2018-01-29 20:20:34 +01:00
|
|
|
//% blockId=outputMotorSetBrakeMode block="set %motor|brake %brake=toggleOnOff"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2017-12-18 07:47:13 +01:00
|
|
|
//% weight=60 blockGap=8
|
2018-04-03 13:30:10 +02:00
|
|
|
//% group="Properties"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/set-brake
|
2017-12-18 07:47:13 +01:00
|
|
|
setBrake(brake: boolean) {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-18 07:47:13 +01:00
|
|
|
this._brake = brake;
|
2017-12-18 17:54:53 +01:00
|
|
|
}
|
2017-12-18 07:47:13 +01:00
|
|
|
|
2018-06-01 17:51:50 +02:00
|
|
|
/**
|
|
|
|
* Indicates to pause while a motor moves for a given distance or duration.
|
|
|
|
* @param value true to pause; false to continue the program execution
|
|
|
|
*/
|
|
|
|
//% blockId=outputMotorSetPauseMode block="set %motor|pause on run %brake=toggleOnOff"
|
|
|
|
//% motor.fieldEditor="motors"
|
|
|
|
//% weight=60 blockGap=8
|
|
|
|
//% group="Properties"
|
|
|
|
setPauseOnRun(value: boolean) {
|
|
|
|
this.init();
|
|
|
|
this._pauseOnRun = value;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
/**
|
2018-02-07 08:18:36 +01:00
|
|
|
* Inverts the motor polarity
|
2017-12-18 07:47:13 +01:00
|
|
|
*/
|
2018-02-07 08:18:36 +01:00
|
|
|
//% blockId=motorSetInverted block="set %motor|inverted %reversed=toggleOnOff"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2018-01-11 08:34:27 +01:00
|
|
|
//% weight=59 blockGap=8
|
2018-04-03 13:30:10 +02:00
|
|
|
//% group="Properties"
|
2018-02-07 08:18:36 +01:00
|
|
|
//% help=motors/motor/set-inverted
|
|
|
|
setInverted(inverted: boolean) {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-18 07:47:13 +01:00
|
|
|
const b = mkCmd(this._port, DAL.opOutputPolarity, 1)
|
2018-02-07 08:18:36 +01:00
|
|
|
b.setNumber(NumberFormat.Int8LE, 2, inverted ? 0 : 1);
|
2017-12-18 07:47:13 +01:00
|
|
|
writePWM(b)
|
2017-12-18 08:19:38 +01:00
|
|
|
}
|
2017-12-18 07:47:13 +01:00
|
|
|
|
2019-08-27 23:57:28 +02:00
|
|
|
/**
|
|
|
|
* Set the settle time after braking in milliseconds (default is 10ms).
|
|
|
|
*/
|
|
|
|
//% blockId=motorSetBrakeSettleTime block="set %motor|brake settle time %millis|ms"
|
|
|
|
//% motor.fieldEditor="motors"
|
|
|
|
//% weight=1 blockGap=8
|
|
|
|
//% group="Properties"
|
|
|
|
//% millis.defl=200 millis.min=0 millis.max=500
|
2019-09-06 15:22:12 +02:00
|
|
|
//% help=motors/motor/set-brake-settle-time
|
2019-08-27 23:57:28 +02:00
|
|
|
setBrakeSettleTime(millis: number) {
|
|
|
|
this.init();
|
|
|
|
// ensure in [0,500]
|
|
|
|
this._brakeSettleTime = Math.max(0, Math.min(500, millis | 0))
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
/**
|
|
|
|
* Stops the motor(s).
|
|
|
|
*/
|
2018-01-09 21:46:48 +01:00
|
|
|
//% weight=6 blockGap=8
|
|
|
|
//% group="Move"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/stop
|
2018-02-02 01:21:08 +01:00
|
|
|
//% blockId=motorStop block="stop %motors|"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motors.fieldEditor="motors"
|
2017-12-18 07:47:13 +01:00
|
|
|
stop() {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-18 07:47:13 +01:00
|
|
|
stop(this._port, this._brake);
|
2018-01-19 06:28:00 +01:00
|
|
|
this.settle();
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:51:50 +02:00
|
|
|
protected settle() {
|
2018-01-19 06:28:00 +01:00
|
|
|
// if we've recently completed a motor command with brake
|
|
|
|
// allow 500ms for robot to settle
|
2019-08-27 23:57:28 +02:00
|
|
|
if (this._brake && this._brakeSettleTime > 0)
|
|
|
|
pause(this._brakeSettleTime);
|
2019-09-17 20:23:40 +02:00
|
|
|
else
|
|
|
|
pause(1); // give a tiny breather
|
2017-12-18 07:47:13 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 17:51:50 +02:00
|
|
|
protected pauseOnRun(stepsOrTime: number) {
|
|
|
|
if (stepsOrTime && this._pauseOnRun) {
|
|
|
|
// wait till motor is done with this work
|
|
|
|
this.pauseUntilReady();
|
|
|
|
// allow robot to settle
|
|
|
|
this.settle();
|
2019-09-17 19:52:00 +02:00
|
|
|
} else {
|
|
|
|
// give a breather to the event system in tight loops
|
|
|
|
pause(1);
|
2018-06-01 17:51:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
/**
|
|
|
|
* Resets the motor(s).
|
|
|
|
*/
|
2018-01-11 07:29:35 +01:00
|
|
|
//% weight=5
|
|
|
|
//% group="Move"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/reset
|
2018-02-02 01:21:08 +01:00
|
|
|
//% blockId=motorReset block="reset %motors|"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motors.fieldEditor="motors"
|
2017-12-18 07:47:13 +01:00
|
|
|
reset() {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-18 07:47:13 +01:00
|
|
|
reset(this._port);
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:57:23 +02:00
|
|
|
private normalizeSchedule(speed: number, step1: number, step2: number, step3: number, unit: MoveUnit): MoveSchedule {
|
|
|
|
const r: MoveSchedule = {
|
|
|
|
speed: Math.clamp(-100, 100, speed >> 0),
|
|
|
|
useSteps: true,
|
2019-09-04 23:51:45 +02:00
|
|
|
steps: [step1 || 0, step2 || 0, step3 || 0]
|
2019-09-03 05:57:23 +02:00
|
|
|
}
|
|
|
|
let scale = 1;
|
|
|
|
switch (unit) {
|
|
|
|
case MoveUnit.Rotations:
|
|
|
|
scale = 360;
|
|
|
|
r.useSteps = true;
|
|
|
|
break;
|
|
|
|
case MoveUnit.Degrees:
|
|
|
|
r.useSteps = true;
|
|
|
|
break;
|
|
|
|
case MoveUnit.Seconds:
|
|
|
|
scale = 1000;
|
|
|
|
r.useSteps = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r.useSteps = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < r.steps.length; ++i)
|
|
|
|
r.steps[i] = Math.max(0, (r.steps[i] * scale) | 0);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:16:33 +02:00
|
|
|
/**
|
2018-02-19 16:35:08 +01:00
|
|
|
* Runs the motor at a given speed for limited time or distance.
|
2017-12-14 07:54:08 +01:00
|
|
|
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
|
2018-01-09 00:27:37 +01:00
|
|
|
* @param value (optional) measured distance or rotation
|
|
|
|
* @param unit (optional) unit of the value
|
2017-10-25 05:16:33 +02:00
|
|
|
*/
|
2018-02-19 16:35:08 +01:00
|
|
|
//% blockId=motorRun block="run %motor at %speed=motorSpeedPicker|\\%||for %value %unit"
|
2018-01-09 00:27:37 +01:00
|
|
|
//% weight=100 blockGap=8
|
2018-01-08 05:33:02 +01:00
|
|
|
//% group="Move"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2018-02-09 19:25:39 +01:00
|
|
|
//% expandableArgumentMode=toggle
|
2018-02-19 16:35:08 +01:00
|
|
|
//% help=motors/motor/run
|
|
|
|
run(speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2019-09-03 05:57:23 +02:00
|
|
|
const schedule = this.normalizeSchedule(speed, 0, value, 0, unit);
|
2018-01-10 20:14:18 +01:00
|
|
|
// stop if speed is 0
|
2019-09-03 05:57:23 +02:00
|
|
|
if (!schedule.speed) {
|
2017-12-14 07:40:40 +01:00
|
|
|
this.stop();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-10 20:14:18 +01:00
|
|
|
// special: 0 is infinity
|
2019-09-03 05:57:23 +02:00
|
|
|
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
|
|
|
this._run(schedule.speed);
|
2019-09-17 20:23:40 +02:00
|
|
|
pause(1);
|
2018-01-10 20:14:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-09-03 05:57:23 +02:00
|
|
|
|
2018-01-10 20:29:27 +01:00
|
|
|
// timed motor moves
|
2019-09-03 05:57:23 +02:00
|
|
|
const steps = schedule.steps;
|
|
|
|
const useSteps = schedule.useSteps;
|
|
|
|
|
|
|
|
// compute ramp up and down
|
|
|
|
steps[0] = (useSteps ? this._accelerationSteps : this._accelerationTime) || 0;
|
|
|
|
steps[2] = (useSteps ? this._decelerationSteps : this._decelerationTime) || 0;
|
|
|
|
if (steps[0] + steps[2] > steps[1]) {
|
|
|
|
// rescale
|
|
|
|
const r = steps[1] / (steps[0] + steps[2]);
|
|
|
|
steps[0] = Math.floor(steps[0] * r);
|
|
|
|
steps[2] *= Math.floor(steps[2] * r);
|
|
|
|
}
|
|
|
|
steps[1] -= (steps[0] + steps[2]);
|
|
|
|
|
|
|
|
// send ramped command
|
|
|
|
this._schedule(schedule);
|
|
|
|
this.pauseOnRun(steps[0] + steps[1] + steps[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules a run of the motor with an acceleration, constant and deceleration phase.
|
|
|
|
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
|
2019-09-04 23:51:45 +02:00
|
|
|
* @param value measured distance or rotation, eg: 500
|
|
|
|
* @param unit (optional) unit of the value, eg: MoveUnit.MilliSeconds
|
|
|
|
* @param acceleration acceleration phase measured distance or rotation, eg: 500
|
|
|
|
* @param deceleration deceleration phase measured distance or rotation, eg: 500
|
2019-09-03 05:57:23 +02:00
|
|
|
*/
|
2019-09-04 23:51:45 +02:00
|
|
|
//% blockId=motorSchedule block="ramp %motor at %speed=motorSpeedPicker|\\%|for %value|%unit||accelerate %acceleration|decelerate %deceleration"
|
2019-09-03 05:57:23 +02:00
|
|
|
//% weight=99 blockGap=8
|
|
|
|
//% group="Move"
|
|
|
|
//% motor.fieldEditor="motors"
|
2019-09-04 23:51:45 +02:00
|
|
|
//% help=motors/motor/ramp
|
2019-09-03 05:57:23 +02:00
|
|
|
//% inlineInputMode=inline
|
2019-09-04 23:51:45 +02:00
|
|
|
//% expandableArgumentMode=toggle
|
|
|
|
//% value.defl=500
|
|
|
|
ramp(speed: number, value: number = 500, unit: MoveUnit = MoveUnit.MilliSeconds, acceleration?: number, deceleration?: number) {
|
2019-09-03 05:57:23 +02:00
|
|
|
this.init();
|
|
|
|
const schedule = this.normalizeSchedule(speed, acceleration, value, deceleration, unit);
|
|
|
|
// stop if speed is 0
|
|
|
|
if (!schedule.speed) {
|
|
|
|
this.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// special case: do nothing
|
|
|
|
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// timed motor moves
|
|
|
|
const steps = schedule.steps;
|
|
|
|
// send ramped command
|
|
|
|
this._schedule(schedule);
|
|
|
|
this.pauseOnRun(steps[0] + steps[1] + steps[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies the amount of rotation or time for the acceleration
|
|
|
|
* of run commands.
|
|
|
|
*/
|
2019-09-06 15:22:12 +02:00
|
|
|
//% blockId=outputMotorsetRunRamp block="set %motor|run %ramp to $value||$unit"
|
2019-09-03 05:57:23 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
|
|
|
//% weight=21 blockGap=8
|
|
|
|
//% group="Properties"
|
2019-09-06 15:22:12 +02:00
|
|
|
//% help=motors/motor/set-run-phase
|
|
|
|
setRunPhase(phase: MovePhase, value: number, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
|
|
|
let temp: number;
|
2019-09-03 05:57:23 +02:00
|
|
|
switch (unit) {
|
|
|
|
case MoveUnit.Rotations:
|
2019-09-06 15:22:12 +02:00
|
|
|
temp = Math.max(0, (value * 360) | 0);
|
|
|
|
if (phase == MovePhase.Acceleration)
|
|
|
|
this._accelerationSteps = temp;
|
|
|
|
else
|
|
|
|
this._decelerationSteps = temp;
|
2019-09-03 05:57:23 +02:00
|
|
|
break;
|
|
|
|
case MoveUnit.Degrees:
|
2019-09-06 15:22:12 +02:00
|
|
|
temp = Math.max(0, value | 0);
|
|
|
|
if (phase == MovePhase.Acceleration)
|
|
|
|
this._accelerationSteps = temp;
|
|
|
|
else
|
|
|
|
this._decelerationSteps = temp;
|
2019-09-03 05:57:23 +02:00
|
|
|
break;
|
|
|
|
case MoveUnit.Seconds:
|
2019-09-06 15:22:12 +02:00
|
|
|
temp = Math.max(0, (value * 1000) | 0);
|
|
|
|
if (phase == MovePhase.Acceleration)
|
|
|
|
this._accelerationTime = temp;
|
|
|
|
else
|
|
|
|
this._decelerationTime = temp;
|
2019-09-03 05:57:23 +02:00
|
|
|
break;
|
|
|
|
case MoveUnit.MilliSeconds:
|
2019-09-06 15:22:12 +02:00
|
|
|
temp = Math.max(0, value | 0);
|
|
|
|
if (phase == MovePhase.Acceleration)
|
|
|
|
this._accelerationTime = temp;
|
|
|
|
else
|
|
|
|
this._decelerationTime = temp;
|
2019-09-03 05:57:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _run(speed: number) {
|
|
|
|
// ramp up acceleration
|
|
|
|
if (this._accelerationTime) {
|
|
|
|
this._schedule({ speed: speed, useSteps: false, steps: [this._accelerationTime, 100, 0] });
|
|
|
|
pause(this._accelerationTime);
|
|
|
|
}
|
|
|
|
// keep going
|
|
|
|
const b = mkCmd(this._port, this._regulated ? DAL.opOutputSpeed : DAL.opOutputPower, 1)
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 2, speed)
|
|
|
|
writePWM(b)
|
|
|
|
if (speed) {
|
|
|
|
writePWM(mkCmd(this._port, DAL.opOutputStart, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _schedule(schedule: MoveSchedule) {
|
|
|
|
const p = {
|
|
|
|
useSteps: schedule.useSteps,
|
|
|
|
step1: schedule.steps[0],
|
|
|
|
step2: schedule.steps[1],
|
|
|
|
step3: schedule.steps[2],
|
|
|
|
speed: this._regulated ? schedule.speed : undefined,
|
|
|
|
power: this._regulated ? undefined : schedule.speed,
|
|
|
|
useBrake: this._brake
|
|
|
|
};
|
|
|
|
step(this._port, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if the motor(s) speed should be regulated. Default is true.
|
|
|
|
* @param value true for regulated motor
|
|
|
|
*/
|
|
|
|
//% blockId=outputMotorSetRegulated block="set %motor|regulated %value=toggleOnOff"
|
|
|
|
//% motor.fieldEditor="motors"
|
|
|
|
//% weight=58 blockGap=8
|
|
|
|
//% group="Properties"
|
|
|
|
//% help=motors/motor/set-regulated
|
|
|
|
setRegulated(value: boolean) {
|
|
|
|
this._regulated = value;
|
2017-10-25 05:16:33 +02:00
|
|
|
}
|
2017-12-18 23:13:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a value indicating if the motor is still running a previous command.
|
|
|
|
*/
|
2018-01-04 17:25:02 +01:00
|
|
|
//% group="Sensors"
|
2017-12-18 23:13:38 +01:00
|
|
|
isReady(): boolean {
|
|
|
|
this.init();
|
2017-12-19 22:10:40 +01:00
|
|
|
const buf = mkCmd(this._port, DAL.opOutputTest, 2);
|
|
|
|
readPWM(buf)
|
|
|
|
const flags = buf.getNumber(NumberFormat.UInt8LE, 2);
|
2018-01-06 09:00:04 +01:00
|
|
|
return (~flags & this._port) == this._port;
|
2017-12-18 23:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses the execution until the previous command finished.
|
|
|
|
* @param timeOut optional maximum pausing time in milliseconds
|
|
|
|
*/
|
2018-02-02 01:21:08 +01:00
|
|
|
//% blockId=motorPauseUntilRead block="pause until %motor|ready"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2019-09-18 00:14:14 +02:00
|
|
|
//% weight=90 blockGap=8
|
2018-01-08 05:33:02 +01:00
|
|
|
//% group="Move"
|
2017-12-18 23:45:44 +01:00
|
|
|
pauseUntilReady(timeOut?: number) {
|
2017-12-18 23:13:38 +01:00
|
|
|
pauseUntil(() => this.isReady(), timeOut);
|
|
|
|
}
|
2018-04-11 16:48:07 +02:00
|
|
|
|
2019-09-03 05:57:23 +02:00
|
|
|
setRunSmoothness(accelerationPercent: number, decelerationPercent: number) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:48:07 +02:00
|
|
|
protected setOutputType(large: boolean) {
|
2019-08-30 14:40:51 +02:00
|
|
|
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
|
2018-04-11 16:48:07 +02:00
|
|
|
if (this._port & (1 << i)) {
|
2018-12-04 01:37:53 +01:00
|
|
|
// (0x07: Large motor, Medium motor = 0x08)
|
|
|
|
MotorBase.output_types[i] = large ? 0x07 : 0x08;
|
2018-04-11 16:48:07 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-04 01:37:53 +01:00
|
|
|
MotorBase.setTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, we are having to create our own buffer here as mkCmd creates a buffer with a command
|
|
|
|
// In the case of opOutputSetType, it expects the arguments to be opOutputSetType [type0, type1, type2, type3]
|
|
|
|
static setTypes() {
|
|
|
|
const b = output.createBuffer(5)
|
|
|
|
b.setNumber(NumberFormat.UInt8LE, 0, DAL.opOutputSetType)
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 1, MotorBase.output_types[0]);
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 2, MotorBase.output_types[1]);
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 3, MotorBase.output_types[2]);
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 4, MotorBase.output_types[3]);
|
|
|
|
writePWM(b)
|
2018-04-11 16:48:07 +02:00
|
|
|
}
|
2017-12-18 07:47:13 +01:00
|
|
|
}
|
2017-12-18 17:54:53 +01:00
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
//% fixedInstances
|
2017-12-19 16:07:50 +01:00
|
|
|
export class Motor extends MotorBase {
|
2017-12-18 07:47:13 +01:00
|
|
|
private _large: boolean;
|
|
|
|
|
|
|
|
constructor(port: Output, large: boolean) {
|
2019-09-03 05:57:23 +02:00
|
|
|
super(port, () => this.__init());
|
2017-12-18 07:47:13 +01:00
|
|
|
this._large = large;
|
2017-12-18 22:04:17 +01:00
|
|
|
this.markUsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
markUsed() {
|
|
|
|
motors.__motorUsed(this._port, this._large);
|
2017-12-07 07:41:51 +01:00
|
|
|
}
|
2017-10-31 05:29:18 +01:00
|
|
|
|
2018-06-01 17:51:50 +02:00
|
|
|
private __init() {
|
2018-04-11 16:48:07 +02:00
|
|
|
this.setOutputType(this._large);
|
2017-10-27 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:16:33 +02:00
|
|
|
/**
|
|
|
|
* Gets motor actual speed.
|
|
|
|
* @param motor the port which connects to the motor
|
|
|
|
*/
|
2017-12-19 20:37:33 +01:00
|
|
|
//% blockId=motorSpeed block="%motor|speed"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2018-01-04 17:25:02 +01:00
|
|
|
//% weight=72
|
|
|
|
//% blockGap=8
|
2018-01-06 01:02:52 +01:00
|
|
|
//% group="Counters"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/speed
|
2017-10-27 11:52:42 +02:00
|
|
|
speed(): number {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-14 07:40:40 +01:00
|
|
|
return getMotorData(this._port).actualSpeed;
|
2017-10-27 09:09:00 +02:00
|
|
|
}
|
2017-10-27 11:52:42 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-04 17:25:02 +01:00
|
|
|
* Gets motor angle.
|
2017-10-27 11:52:42 +02:00
|
|
|
* @param motor the port which connects to the motor
|
|
|
|
*/
|
2018-01-04 21:55:30 +01:00
|
|
|
//% blockId=motorAngle block="%motor|angle"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2017-12-14 07:40:40 +01:00
|
|
|
//% weight=70
|
2018-01-06 01:02:52 +01:00
|
|
|
//% blockGap=8
|
|
|
|
//% group="Counters"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/angle
|
2017-12-18 23:13:38 +01:00
|
|
|
angle(): number {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-18 23:13:38 +01:00
|
|
|
return getMotorData(this._port).count;
|
2017-10-27 11:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the motor count
|
|
|
|
*/
|
2018-02-02 01:21:08 +01:00
|
|
|
//% blockId=motorClearCount block="clear %motor|counters"
|
2018-04-10 00:59:28 +02:00
|
|
|
//% motor.fieldEditor="motors"
|
2018-01-04 17:25:02 +01:00
|
|
|
//% weight=68
|
|
|
|
//% blockGap=8
|
2018-01-06 01:02:52 +01:00
|
|
|
//% group="Counters"
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/motor/clear-counts
|
2018-01-04 17:25:02 +01:00
|
|
|
clearCounts() {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-14 07:40:40 +01:00
|
|
|
const b = mkCmd(this._port, DAL.opOutputClearCount, 0)
|
2017-10-27 11:52:42 +02:00
|
|
|
writePWM(b)
|
|
|
|
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
|
2017-12-14 07:40:40 +01:00
|
|
|
if (this._port & (1 << i)) {
|
2017-10-27 11:52:42 +02:00
|
|
|
motorMM.setNumber(NumberFormat.Int32LE, i * MotorDataOff.Size + MotorDataOff.TachoSensor, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 16:07:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the status of the motor
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
toString(): string {
|
|
|
|
return `${this._large ? "" : "M"}${this._portName} ${this.speed()}% ${this.angle()}>`;
|
|
|
|
}
|
2019-09-18 00:14:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses the program until the motor is stalled.
|
|
|
|
*/
|
|
|
|
//% blockId=motorPauseUntilStall block="pause until %motor|stalled"
|
|
|
|
//% motor.fieldEditor="motors"
|
|
|
|
//% weight=89
|
|
|
|
//% group="Move"
|
|
|
|
//% help=motors/motor/pause-until-stalled
|
|
|
|
pauseUntilStalled(timeOut?: number): void {
|
|
|
|
// let it start
|
|
|
|
pause(50);
|
|
|
|
let previous = this.angle();
|
|
|
|
let stall = 0;
|
|
|
|
pauseUntil(() => {
|
|
|
|
let current = this.angle();
|
|
|
|
if (Math.abs(current - previous) < 1) {
|
|
|
|
if (stall++ > 2) {
|
|
|
|
return true; // not moving
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stall = 0;
|
|
|
|
previous = current;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}, timeOut)
|
|
|
|
}
|
2017-12-15 02:01:23 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="large motor A" jres=icons.portA
|
2017-12-19 16:07:50 +01:00
|
|
|
export const largeA = new Motor(Output.A, true);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="large motor B" jres=icons.portB
|
2017-12-19 16:07:50 +01:00
|
|
|
export const largeB = new Motor(Output.B, true);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="large motor C" jres=icons.portC
|
2017-12-19 16:07:50 +01:00
|
|
|
export const largeC = new Motor(Output.C, true);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="large motor D" jres=icons.portD
|
2017-12-19 16:07:50 +01:00
|
|
|
export const largeD = new Motor(Output.D, true);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="medium motor A" jres=icons.portA
|
2017-12-19 16:07:50 +01:00
|
|
|
export const mediumA = new Motor(Output.A, false);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="medium motor B" jres=icons.portB
|
2017-12-19 16:07:50 +01:00
|
|
|
export const mediumB = new Motor(Output.B, false);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="medium motor C" jres=icons.portC
|
2017-12-19 16:07:50 +01:00
|
|
|
export const mediumC = new Motor(Output.C, false);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="medium motor D" jres=icons.portD
|
2017-12-19 16:07:50 +01:00
|
|
|
export const mediumD = new Motor(Output.D, false);
|
2017-12-15 02:01:23 +01:00
|
|
|
|
|
|
|
//% fixedInstances
|
2017-12-19 16:07:50 +01:00
|
|
|
export class SynchedMotorPair extends MotorBase {
|
2017-12-18 17:54:53 +01:00
|
|
|
|
2017-12-15 02:01:23 +01:00
|
|
|
constructor(ports: Output) {
|
2019-09-03 05:57:23 +02:00
|
|
|
super(ports, () => this.__init());
|
2017-12-18 22:04:17 +01:00
|
|
|
this.markUsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
markUsed() {
|
|
|
|
motors.__motorUsed(this._port, true);
|
2017-12-15 02:01:23 +01:00
|
|
|
}
|
2017-12-14 07:40:40 +01:00
|
|
|
|
2017-12-18 17:54:53 +01:00
|
|
|
private __init() {
|
2018-04-11 16:48:07 +02:00
|
|
|
this.setOutputType(true);
|
2017-12-18 07:47:13 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 01:02:52 +01:00
|
|
|
/**
|
|
|
|
* The Move Tank block can make a robot drive forward, backward, turn, or stop.
|
|
|
|
* Use the Move Tank block for robot vehicles that have two Large Motors,
|
|
|
|
* with one motor driving the left side of the vehicle and the other the right side.
|
|
|
|
* You can make the two motors go at different speeds or in different directions
|
|
|
|
* to make your robot turn.
|
|
|
|
* @param speedLeft the speed on the left motor, eg: 50
|
|
|
|
* @param speedRight the speed on the right motor, eg: 50
|
2018-01-09 00:27:37 +01:00
|
|
|
* @param value (optional) move duration or rotation
|
|
|
|
* @param unit (optional) unit of the value
|
2018-01-06 01:02:52 +01:00
|
|
|
*/
|
2018-04-10 00:59:28 +02:00
|
|
|
//% blockId=motorPairTank block="tank **motors** %motors %speedLeft=motorSpeedPicker|\\% %speedRight=motorSpeedPicker|\\%||for %value %unit"
|
|
|
|
//% motors.fieldEditor="ports"
|
2018-01-08 05:33:02 +01:00
|
|
|
//% weight=96 blockGap=8
|
2018-01-03 09:27:05 +01:00
|
|
|
//% inlineInputMode=inline
|
2018-01-09 00:27:37 +01:00
|
|
|
//% group="Move"
|
2018-02-09 19:25:39 +01:00
|
|
|
//% expandableArgumentMode=toggle
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/synced/tank
|
2018-01-09 00:27:37 +01:00
|
|
|
tank(speedLeft: number, speedRight: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
2018-01-03 09:27:05 +01:00
|
|
|
this.init();
|
|
|
|
|
|
|
|
speedLeft = Math.clamp(-100, 100, speedLeft >> 0);
|
|
|
|
speedRight = Math.clamp(-100, 100, speedRight >> 0);
|
|
|
|
|
|
|
|
const speed = Math.abs(speedLeft) > Math.abs(speedRight) ? speedLeft : speedRight;
|
2019-08-30 14:40:51 +02:00
|
|
|
let turnRatio = speedLeft == speed
|
|
|
|
? speedLeft == 0 ? 0 : (100 - speedRight / speedLeft * 100)
|
|
|
|
: speedRight == 0 ? 0 : (speedLeft / speedRight * 100 - 100);
|
|
|
|
turnRatio = Math.floor(turnRatio);
|
2018-01-06 01:02:52 +01:00
|
|
|
|
2019-08-30 14:40:51 +02:00
|
|
|
//control.dmesg(`tank ${speedLeft} ${speedRight} => ${turnRatio} ${speed}`)
|
2018-01-09 00:27:37 +01:00
|
|
|
this.steer(turnRatio, speed, value, unit);
|
2018-01-03 09:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 01:02:52 +01:00
|
|
|
* Turns the motor and the follower motor by a number of rotations
|
|
|
|
* @param turnRatio the ratio of power sent to the follower motor, from ``-200`` to ``200``, eg: 0
|
|
|
|
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
|
2018-01-09 00:27:37 +01:00
|
|
|
* @param value (optional) move duration or rotation
|
|
|
|
* @param unit (optional) unit of the value
|
2018-01-03 09:27:05 +01:00
|
|
|
*/
|
2018-04-10 00:59:28 +02:00
|
|
|
//% blockId=motorPairSteer block="steer **motors** %chassis turn ratio %turnRatio=motorTurnRatioPicker speed %speed=motorSpeedPicker|\\%||for %value %unit"
|
|
|
|
//% chassis.fieldEditor="ports"
|
2018-01-08 05:33:02 +01:00
|
|
|
//% weight=95
|
2018-01-06 01:02:52 +01:00
|
|
|
//% turnRatio.min=-200 turnRatio=200
|
2018-01-03 09:27:05 +01:00
|
|
|
//% inlineInputMode=inline
|
2018-01-08 05:33:02 +01:00
|
|
|
//% group="Move"
|
2018-02-09 19:25:39 +01:00
|
|
|
//% expandableArgumentMode=toggle
|
2018-02-07 04:43:50 +01:00
|
|
|
//% help=motors/synced/steer
|
2018-01-09 00:27:37 +01:00
|
|
|
steer(turnRatio: number, speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
2017-12-18 17:54:53 +01:00
|
|
|
this.init();
|
2017-12-15 02:01:23 +01:00
|
|
|
speed = Math.clamp(-100, 100, speed >> 0);
|
|
|
|
if (!speed) {
|
2019-09-17 20:23:40 +02:00
|
|
|
this.stop();
|
2017-12-15 02:01:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-03 01:20:00 +01:00
|
|
|
turnRatio = Math.clamp(-200, 200, turnRatio >> 0);
|
2017-12-15 02:01:23 +01:00
|
|
|
let useSteps: boolean;
|
|
|
|
let stepsOrTime: number;
|
|
|
|
switch (unit) {
|
|
|
|
case MoveUnit.Rotations:
|
|
|
|
stepsOrTime = (value * 360) >> 0;
|
|
|
|
useSteps = true;
|
|
|
|
break;
|
|
|
|
case MoveUnit.Degrees:
|
|
|
|
stepsOrTime = value >> 0;
|
|
|
|
useSteps = true;
|
|
|
|
break;
|
2018-01-06 03:24:23 +01:00
|
|
|
case MoveUnit.Seconds:
|
|
|
|
stepsOrTime = (value * 1000) >> 0;
|
|
|
|
useSteps = false;
|
|
|
|
break;
|
2017-12-15 02:01:23 +01:00
|
|
|
default:
|
2018-01-03 01:20:00 +01:00
|
|
|
stepsOrTime = value >> 0;
|
2017-12-15 02:01:23 +01:00
|
|
|
useSteps = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:47:13 +01:00
|
|
|
syncMotors(this._port, {
|
2017-12-15 02:01:23 +01:00
|
|
|
useSteps: useSteps,
|
|
|
|
speed: speed,
|
|
|
|
turnRatio: turnRatio,
|
|
|
|
stepsOrTime: stepsOrTime,
|
|
|
|
useBrake: this._brake
|
|
|
|
});
|
2018-06-01 17:51:50 +02:00
|
|
|
|
|
|
|
this.pauseOnRun(stepsOrTime);
|
2018-01-06 03:24:23 +01:00
|
|
|
}
|
2017-12-15 02:01:23 +01:00
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
/**
|
|
|
|
* Returns the name(s) of the motor
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
toString(): string {
|
2018-01-03 09:27:05 +01:00
|
|
|
this.init();
|
|
|
|
|
2017-12-19 16:07:50 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-07-07 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="B+C" jres=icons.portBC
|
2017-12-15 02:01:23 +01:00
|
|
|
export const largeBC = new SynchedMotorPair(Output.BC);
|
2017-10-27 09:09:00 +02:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="A+D" jres=icons.portAD
|
2017-12-15 02:01:23 +01:00
|
|
|
export const largeAD = new SynchedMotorPair(Output.AD);
|
2017-10-27 09:09:00 +02:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="A+B" jres=icons.portAB
|
2017-12-15 02:01:23 +01:00
|
|
|
export const largeAB = new SynchedMotorPair(Output.AB);
|
2017-10-27 09:09:00 +02:00
|
|
|
|
2018-04-10 00:59:28 +02:00
|
|
|
//% whenUsed fixedInstance block="C+D" jres=icons.portCD
|
2017-12-15 02:01:23 +01:00
|
|
|
export const largeCD = new SynchedMotorPair(Output.CD);
|
2017-10-25 05:16:33 +02:00
|
|
|
|
|
|
|
function reset(out: Output) {
|
2017-12-19 22:10:40 +01:00
|
|
|
writePWM(mkCmd(out, DAL.opOutputReset, 0))
|
|
|
|
writePWM(mkCmd(out, DAL.opOutputClearCount, 0))
|
2017-07-07 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 11:41:47 +02:00
|
|
|
function outOffset(out: Output) {
|
|
|
|
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
|
|
|
|
if (out & (1 << i))
|
|
|
|
return i * MotorDataOff.Size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:26:57 +01:00
|
|
|
export interface MotorData {
|
2017-08-08 11:41:47 +02:00
|
|
|
actualSpeed: number; // -100..+100
|
|
|
|
tachoCount: number;
|
|
|
|
count: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only a single output at a time
|
2017-10-25 05:16:33 +02:00
|
|
|
function getMotorData(out: Output): MotorData {
|
2017-12-13 23:55:14 +01:00
|
|
|
init()
|
2017-12-19 23:26:57 +01:00
|
|
|
const buf = motorMM.slice(outOffset(out), MotorDataOff.Size)
|
2017-08-08 11:41:47 +02:00
|
|
|
return {
|
|
|
|
actualSpeed: buf.getNumber(NumberFormat.Int8LE, MotorDataOff.Speed),
|
|
|
|
tachoCount: buf.getNumber(NumberFormat.Int32LE, MotorDataOff.TachoCounts),
|
|
|
|
count: buf.getNumber(NumberFormat.Int32LE, MotorDataOff.TachoSensor),
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 02:39:37 +02:00
|
|
|
|
2017-12-19 23:26:57 +01:00
|
|
|
export function getAllMotorData(): MotorData[] {
|
|
|
|
init();
|
|
|
|
return [Output.A, Output.B, Output.C, Output.D].map(out => getMotorData(out));
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:20:25 +01:00
|
|
|
interface SyncOptions {
|
|
|
|
useSteps?: boolean;
|
|
|
|
speed: number;
|
|
|
|
turnRatio: number;
|
2017-12-14 07:40:40 +01:00
|
|
|
stepsOrTime?: number;
|
2017-12-12 22:20:25 +01:00
|
|
|
useBrake?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
function syncMotors(out: Output, opts: SyncOptions) {
|
|
|
|
const cmd = opts.useSteps ? DAL.opOutputStepSync : DAL.opOutputTimeSync;
|
|
|
|
const b = mkCmd(out, cmd, 11);
|
|
|
|
const speed = Math.clamp(-100, 100, opts.speed);
|
|
|
|
const turnRatio = Math.clamp(-200, 200, opts.turnRatio);
|
|
|
|
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 2, speed)
|
|
|
|
// note that b[3] is padding
|
|
|
|
b.setNumber(NumberFormat.Int16LE, 4 + 4 * 0, turnRatio)
|
|
|
|
// b[6], b[7] is padding
|
|
|
|
b.setNumber(NumberFormat.Int32LE, 4 + 4 * 1, opts.stepsOrTime || 0)
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 4 + 4 * 2, opts.useBrake ? 1 : 0)
|
|
|
|
writePWM(b)
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:16:33 +02:00
|
|
|
interface StepOptions {
|
2017-07-07 16:15:36 +02:00
|
|
|
power?: number;
|
|
|
|
speed?: number; // either speed or power has to be present
|
|
|
|
step1: number;
|
|
|
|
step2: number;
|
|
|
|
step3: number;
|
|
|
|
useSteps?: boolean; // otherwise use milliseconds
|
2017-08-08 02:39:37 +02:00
|
|
|
useBrake?: boolean;
|
2017-07-07 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 23:08:45 +01:00
|
|
|
function start(out: Output) {
|
|
|
|
const b = mkCmd(out, DAL.opOutputStart, 0)
|
|
|
|
writePWM(b);
|
|
|
|
}
|
|
|
|
|
2017-12-18 06:12:18 +01:00
|
|
|
function stop(out: Output, brake: boolean) {
|
2017-12-14 07:40:40 +01:00
|
|
|
const b = mkCmd(out, DAL.opOutputStop, 1)
|
2017-12-18 06:12:18 +01:00
|
|
|
b.setNumber(NumberFormat.UInt8LE, 2, brake ? 1 : 0)
|
2017-12-14 07:40:40 +01:00
|
|
|
writePWM(b);
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:16:33 +02:00
|
|
|
function step(out: Output, opts: StepOptions) {
|
2018-09-21 23:07:11 +02:00
|
|
|
control.dmesg('step')
|
2017-07-11 16:18:59 +02:00
|
|
|
let op = opts.useSteps ? DAL.opOutputStepSpeed : DAL.opOutputTimeSpeed
|
2017-07-07 16:15:36 +02:00
|
|
|
let speed = opts.speed
|
2018-09-21 23:07:11 +02:00
|
|
|
if (undefined == speed) {
|
2017-07-07 16:15:36 +02:00
|
|
|
speed = opts.power
|
2017-07-11 16:18:59 +02:00
|
|
|
op = opts.useSteps ? DAL.opOutputStepPower : DAL.opOutputTimePower
|
2018-09-21 23:07:11 +02:00
|
|
|
if (undefined == speed)
|
2017-07-07 16:15:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
speed = Math.clamp(-100, 100, speed)
|
|
|
|
let b = mkCmd(out, op, 15)
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 2, speed)
|
|
|
|
// note that b[3] is padding
|
|
|
|
b.setNumber(NumberFormat.Int32LE, 4 + 4 * 0, opts.step1)
|
|
|
|
b.setNumber(NumberFormat.Int32LE, 4 + 4 * 1, opts.step2)
|
|
|
|
b.setNumber(NumberFormat.Int32LE, 4 + 4 * 2, opts.step3)
|
2018-09-21 23:07:11 +02:00
|
|
|
const br = !!opts.useBrake ? 1 : 0;
|
|
|
|
b.setNumber(NumberFormat.Int8LE, 4 + 4 * 3, br)
|
2017-07-07 16:15:36 +02:00
|
|
|
writePWM(b)
|
|
|
|
}
|
2017-07-10 10:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface Buffer {
|
|
|
|
[index: number]: number;
|
|
|
|
// rest defined in buffer.cpp
|
2017-07-07 16:15:36 +02:00
|
|
|
}
|