-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pelikhan/master
Jacdac Extension
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Jacdac blocks for the Kitronik motor driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//% deprecated | ||
namespace kitronik_motor_driver { } | ||
|
||
namespace modules { | ||
/** | ||
* Motor 1 client | ||
*/ | ||
//% fixedInstance whenUsed block="kitronik motor1" | ||
export const kitronikMotor1 = new MotorClient("kitronik motor1?dev=self&srvo=0") | ||
|
||
/** | ||
* Motor 2 client | ||
*/ | ||
//% fixedInstance whenUsed block="kitronik motor2" | ||
export const kitronikMotor2 = new MotorClient("kitronik motor2?dev=self&srvo=1") | ||
} | ||
|
||
namespace servers { | ||
class MotorServer extends jacdac.Server { | ||
motor: kitronik_motor_driver.Motors | ||
speed: number | ||
enabled: boolean | ||
|
||
constructor(motor: kitronik_motor_driver.Motors) { | ||
super(jacdac.SRV_MOTOR) | ||
this.motor = motor | ||
this.enabled = false | ||
|
||
kitronik_motor_driver.motorOff(this.motor) | ||
} | ||
|
||
handlePacket(pkt: jacdac.JDPacket) { | ||
this.handleRegValue(pkt, jacdac.MotorReg.Reversible, jacdac.MotorRegPack.Reversible, true) | ||
this.speed = this.handleRegValue(pkt, jacdac.MotorReg.Speed, jacdac.MotorRegPack.Speed, this.speed) | ||
this.enabled = this.handleRegBool(pkt, jacdac.MotorReg.Enabled, this.enabled) | ||
|
||
this.sync() | ||
} | ||
|
||
sync() { | ||
if (!this.enabled) | ||
kitronik_motor_driver.motorOff(this.motor) | ||
else { | ||
const direction = this.speed < 0 ? kitronik_motor_driver.MotorDirection.Reverse | ||
: kitronik_motor_driver.MotorDirection.Forward | ||
kitronik_motor_driver.motorOn(this.motor, direction, Math.abs(this.speed)) | ||
} | ||
} | ||
} | ||
function start() { | ||
jacdac.productIdentifier = 0x31ee311d | ||
jacdac.deviceDescription = "Kitronik Motor Driver" | ||
jacdac.startSelfServers(() => [ | ||
new MotorServer(kitronik_motor_driver.Motors.Motor1), | ||
new MotorServer(kitronik_motor_driver.Motors.Motor2) | ||
]) | ||
} | ||
start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "kitronik-motor-driver-jacdac", | ||
"version": "0.0.16", | ||
"dependencies": { | ||
"core": "*", | ||
"radio": "*", | ||
"microphone": "*", | ||
"pxt-kitronik-motor-driver": "github:KitronikLtd/pxt-kitronik-motor-driver", | ||
"jacdac": "github:microsoft/pxt-jacdac#v0.10.53", | ||
"jacdac-motor": "github:microsoft/pxt-jacdac/motor#v0.10.53" | ||
}, | ||
"files": [ | ||
"main.ts", | ||
"README.md" | ||
], | ||
"testFiles": [ | ||
"test.ts" | ||
], | ||
"targetVersions": { | ||
"target": "4.1.45", | ||
"targetId": "microbit" | ||
}, | ||
"supportedTargets": [ | ||
"microbit" | ||
], | ||
"preferredEditor": "tsprj" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
let speed = 0 | ||
let dv = 5 | ||
forever(() => { | ||
modules.kitronikMotor1.run(speed) | ||
modules.kitronikMotor2.run(- speed) | ||
|
||
speed += dv | ||
if (speed > 100) { | ||
dv = -5 | ||
} else if (speed < -100) { | ||
dv = 5 | ||
} | ||
pause(250) | ||
}) |