2018-01-30 17:49:10 +01:00
|
|
|
/**
|
|
|
|
* A differential drive robot
|
|
|
|
*/
|
|
|
|
//% weight=50 color=#cf00cf
|
2018-01-06 01:02:52 +01:00
|
|
|
namespace chassis {
|
|
|
|
/**
|
|
|
|
* A differential drive robot
|
|
|
|
*/
|
|
|
|
//% fixedInstances
|
|
|
|
export class Chassis {
|
|
|
|
// the motor pair
|
|
|
|
public motors: motors.SynchedMotorPair;
|
|
|
|
// the radius of the wheel (cm)
|
|
|
|
public wheelRadius: number;
|
|
|
|
// the distance between the wheels (cm)
|
|
|
|
public baseLength: number;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.motors = motors.largeBC;
|
|
|
|
this.wheelRadius = 3;
|
|
|
|
this.baseLength = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a differential drive robot move with a given speed (cm/s) and rotation rate (deg/s)
|
|
|
|
* using a unicycle model.
|
|
|
|
* @param speed speed of the center point between motors, eg: 10
|
|
|
|
* @param rotationSpeed rotation of the robot around the center point, eg: 30
|
2018-01-30 17:49:10 +01:00
|
|
|
* @param distance
|
|
|
|
**/
|
2018-01-10 20:14:25 +01:00
|
|
|
//% blockId=motorDrive block="drive %chassis|at %speed|cm/s|turning %rotationSpeed|deg/s"
|
2018-01-06 01:02:52 +01:00
|
|
|
//% inlineInputMode=inline
|
|
|
|
//% weight=95 blockGap=8
|
2018-01-30 17:49:10 +01:00
|
|
|
drive(speed: number, rotationSpeed: number, distance: number = 0) {
|
|
|
|
if (!speed) {
|
|
|
|
this.motors.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-06 01:02:52 +01:00
|
|
|
// speed is expressed in %
|
|
|
|
const R = this.wheelRadius; // cm
|
|
|
|
const L = this.baseLength; // cm
|
|
|
|
const PI = 3.14;
|
|
|
|
const maxw = 170 / 60 * 2 * PI; // rad / s
|
|
|
|
const maxv = maxw * R; // cm / s
|
|
|
|
// speed is cm / s
|
|
|
|
const v = speed; // cm / s
|
|
|
|
const w = rotationSpeed / 360 * 2 * PI; // rad / s
|
|
|
|
|
|
|
|
const vr = (2 * v + w * L) / (2 * R); // rad / s
|
|
|
|
const vl = (2 * v - w * L) / (2 * R); // rad / s
|
|
|
|
|
|
|
|
const sr = vr / maxw * 100; // %
|
|
|
|
const sl = vl / maxw * 100; // %
|
|
|
|
|
2018-01-30 17:49:10 +01:00
|
|
|
// cm / (cm/s) = s
|
|
|
|
const seconds = distance / speed;
|
|
|
|
|
|
|
|
this.motors.tank(sr, sl, seconds, MoveUnit.Seconds)
|
2018-01-06 01:02:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-30 17:49:10 +01:00
|
|
|
* Sets the wheel radius in centimeters
|
|
|
|
* @param cm
|
2018-01-06 01:02:52 +01:00
|
|
|
*/
|
2018-01-30 17:49:10 +01:00
|
|
|
//% blockId=chassisSetWheelRadius block="set %chassis|wheel radius to %cm|(cm)"
|
|
|
|
setWheelRadius(cm: number) {
|
|
|
|
this.wheelRadius = cm;
|
2018-01-06 01:02:52 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:49:10 +01:00
|
|
|
/**
|
|
|
|
* Sets the base length in centimeters
|
|
|
|
* @param cm
|
|
|
|
*/
|
|
|
|
//% blockId=chassisSetBaseLength block="set %chassis|base length to %cm|(cm)"
|
|
|
|
setBaseLength(cm: number) {
|
|
|
|
this.baseLength = cm;
|
|
|
|
}
|
|
|
|
|
2018-01-06 01:02:52 +01:00
|
|
|
/**
|
|
|
|
* Sets the motors used by the chassis, default is B+C
|
|
|
|
* @param motors
|
|
|
|
*/
|
|
|
|
//% blockId=chassisSetMotors block="set %chassis|motors to %motors"
|
|
|
|
//% weight=10
|
|
|
|
setMotors(motors: motors.SynchedMotorPair) {
|
|
|
|
this.motors = motors;
|
|
|
|
}
|
2018-01-30 17:49:10 +01:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `chassis base ${this.baseLength}, wheel ${this.wheelRadius}`;
|
|
|
|
}
|
2018-01-06 01:02:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//% fixedInstance whenUsed
|
|
|
|
export const chassis = new Chassis();
|
|
|
|
}
|