Behaviors Driven Robotics (#178)
* adding avoid crash behavior * more implementation * add another simple behavior * fixed synched motors * bump common packages
This commit is contained in:
parent
69f8453947
commit
7da811246c
14
libs/behaviors/_locales/behaviors-jsdoc-strings.json
Normal file
14
libs/behaviors/_locales/behaviors-jsdoc-strings.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"behaviors": "Behavior drive blocks",
|
||||
"behaviors.Behavior": "A behavior",
|
||||
"behaviors.BehaviorManager": "A manager for behaviors",
|
||||
"behaviors.BehaviorManager.add": "Adds a new behavior to the behavior manager",
|
||||
"behaviors.BehaviorManager.add|param|behavior": "the behavior to add",
|
||||
"behaviors.BehaviorManager.start": "Starts the behavior control loop",
|
||||
"behaviors.BehaviorManager.stop": "Stops the execution loop",
|
||||
"behaviors.addBehavior": "Adds the behavior and starts it",
|
||||
"behaviors.addBehavior|param|behavior": "a behavior",
|
||||
"behaviors.avoidCrash": "A behavior that stops all motors if the sensor distance get too short",
|
||||
"behaviors.driveForward": "A behavior that turns on the motors to the specified speed",
|
||||
"behaviors.driveForward|param|motors": "@param speed the desired speed, eg: 50"
|
||||
}
|
7
libs/behaviors/_locales/behaviors-strings.json
Normal file
7
libs/behaviors/_locales/behaviors-strings.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"behaviors.addBehavior|block": "add behavior %behavior",
|
||||
"behaviors.avoidCrash|block": "avoid crash using %ultrasonic",
|
||||
"behaviors.driveForward|block": "drive %motors|forward at %speed|%",
|
||||
"behaviors|block": "behaviors",
|
||||
"{id:category}Behaviors": "Behaviors"
|
||||
}
|
6
libs/behaviors/pxt.json
Normal file
6
libs/behaviors/pxt.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/behaviors",
|
||||
"dependencies": {
|
||||
"core": "file:../ev3"
|
||||
}
|
||||
}
|
56
libs/behaviors/targetoverrides.ts
Normal file
56
libs/behaviors/targetoverrides.ts
Normal file
@ -0,0 +1,56 @@
|
||||
namespace behaviors {
|
||||
class AvoidCrashBehavior extends behaviors.Behavior {
|
||||
private ultrasonic: sensors.UltraSonicSensor;
|
||||
constructor(ultrasonic: sensors.UltraSonicSensor) {
|
||||
super();
|
||||
this.ultrasonic = ultrasonic;
|
||||
}
|
||||
|
||||
shouldRun(): boolean {
|
||||
return this.ultrasonic.distance() < 5;
|
||||
}
|
||||
|
||||
run(): void {
|
||||
motors.stopAllMotors();
|
||||
this.active = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A behavior that stops all motors if the sensor distance get too short
|
||||
*/
|
||||
//% blockId=behaviorsAvoidCrash block="avoid crash using %ultrasonic"
|
||||
export function avoidCrash(ultrasonic: sensors.UltraSonicSensor) : behaviors.Behavior {
|
||||
return new AvoidCrashBehavior(ultrasonic);
|
||||
}
|
||||
|
||||
class DriveForwardBehavior extends behaviors.Behavior {
|
||||
private motors: motors.MotorBase;
|
||||
private speed: number;
|
||||
constructor(motors: motors.MotorBase, speed: number) {
|
||||
super();
|
||||
this.motors = motors;
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
shouldRun(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
run(): void {
|
||||
this.motors.setSpeed(this.speed);
|
||||
pauseUntil(() => !this.active);
|
||||
this.motors.setSpeed(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A behavior that turns on the motors to the specified speed
|
||||
* @param motors
|
||||
* @param speed the desired speed, eg: 50
|
||||
*/
|
||||
//% blockId=behaviorsDriveForward block="drive %motors|forward at %speed|%"
|
||||
export function driveForward(motors: motors.MotorBase, speed: number): behaviors.Behavior {
|
||||
return new DriveForwardBehavior(motors, speed);
|
||||
}
|
||||
}
|
@ -343,7 +343,7 @@ namespace motors {
|
||||
* Gets motor angle.
|
||||
* @param motor the port which connects to the motor
|
||||
*/
|
||||
//% blockId=motorTachoCount block="%motor|angle"
|
||||
//% blockId=motorAngle block="%motor|angle"
|
||||
//% weight=70
|
||||
//% group="Sensors"
|
||||
angle(): number {
|
||||
@ -445,7 +445,7 @@ namespace motors {
|
||||
private __setSpeed(speed: number) {
|
||||
syncMotors(this._port, {
|
||||
speed: speed,
|
||||
turnRatio: 0,
|
||||
turnRatio: 100, // same speed
|
||||
useBrake: !!this._brake
|
||||
})
|
||||
}
|
||||
|
@ -19,6 +19,10 @@ namespace sensors {
|
||||
namespace motors {
|
||||
}
|
||||
|
||||
//% labelLineWidth=0
|
||||
namespace behaviors {
|
||||
}
|
||||
|
||||
//% color="#D67923" weight=80 icon="\uf10e"
|
||||
namespace music {
|
||||
|
||||
|
@ -1,16 +1,6 @@
|
||||
{
|
||||
"name": "tests",
|
||||
"description": "A unit test library",
|
||||
"files": [
|
||||
"README.md",
|
||||
"tests.ts",
|
||||
"platformoverrides.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
],
|
||||
"public": true,
|
||||
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/tests",
|
||||
"dependencies": {
|
||||
"core": "file:../core"
|
||||
"core": "file:../ev3"
|
||||
}
|
||||
}
|
@ -44,7 +44,7 @@
|
||||
"webfonts-generator": "^0.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"pxt-common-packages": "0.15.1",
|
||||
"pxt-common-packages": "0.15.3",
|
||||
"pxt-core": "3.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -16,7 +16,8 @@
|
||||
"libs/infrared-sensor",
|
||||
"libs/gyro-sensor",
|
||||
"libs/ev3",
|
||||
"libs/tests"
|
||||
"libs/tests",
|
||||
"libs/behaviors"
|
||||
],
|
||||
"simulator": {
|
||||
"autoRun": true,
|
||||
|
Loading…
Reference in New Issue
Block a user