forked from dugbraden/pxt-climate-action-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
166fc36
commit 7cdadfd
Showing
5 changed files
with
283 additions
and
301 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 |
---|---|---|
@@ -1,99 +1,89 @@ | ||
enum Motor { | ||
//% weight=13 color=#ffd43a icon="" block="Motor" | ||
namespace cakLandMotor { | ||
enum Motor { | ||
//% block="left" | ||
LEFT = 0, | ||
//% block="right" | ||
RIGHT = 1 | ||
} | ||
|
||
enum MotorPower { | ||
//% block="on" | ||
ON = 1, | ||
//% block="off" | ||
OFF = 0 | ||
} | ||
} | ||
|
||
//% weight=13 color=#ffd43a icon="" block="Motor" | ||
namespace cakLandMotor { | ||
const PWM_PERIOD = 1000; //totally arbitrary, testing showed no effect | ||
let motorState: MotorPower = MotorPower.ON | ||
|
||
/** | ||
*Turns the left motor at a specified speed | ||
*/ | ||
/** | ||
*Turns the left motor at a specified speed | ||
*/ | ||
//% block | ||
//% blockId=motion_turn_left block="turn left motor at |speed: %speed" | ||
//% speed.min=-100 speed.max=100 | ||
//% weight=60 | ||
export function turnLeft(speed: number): void { | ||
drive(speed, 0); | ||
drive(speed, 0); | ||
} | ||
|
||
/** | ||
*Turns the right motor at a specified speed | ||
*/ | ||
//% block | ||
//% blockId=motion_turn_right block="turn right motor at |speed: %speed" | ||
//% speed.min=-100 speed.max=100 | ||
//% weight=50 | ||
export function turnRight(speed: number): void { | ||
drive(0, speed); | ||
} | ||
/** | ||
*Turns the right motor at a specified speed | ||
*/ | ||
//% block | ||
//% blockId=motion_turn_right block="turn right motor at |speed: %speed" | ||
//% speed.min=-100 speed.max=100 | ||
//% weight=50 | ||
export function turnRight(speed: number): void { | ||
drive(0, speed); | ||
} | ||
|
||
/** | ||
*Stop the motors | ||
*/ | ||
//% block | ||
//% blockId=motion_stop block="stop motors" | ||
//% weight=45 | ||
export function stop(): void { | ||
drive(0, 0); | ||
} | ||
/** | ||
*Stop the motors | ||
*/ | ||
//% block | ||
//% blockId=motion_stop block="stop motors" | ||
//% weight=45 | ||
export function stop(): void { | ||
drive(0, 0); | ||
} | ||
|
||
/** | ||
* Control both wheels in one function. | ||
* Speeds range from -100 to 100. | ||
* Negative speeds go backwards, positive go forwards. | ||
*/ | ||
//% block | ||
//% blockId=motion_drive block="drive |left: %leftWheelSpeed|right: %rightWheelSpeed" | ||
//% leftWheelSpeed.min=-100 leftWheelSpeed.max=100 | ||
//% rightWheelSpeed.min=-100 rightWheelSpeed.max=100 | ||
//% weight=40 | ||
export function drive(leftWheelSpeed: number, rightWheelSpeed: number): void { | ||
motorControl(Motor.LEFT, leftWheelSpeed) | ||
motorControl(Motor.RIGHT, rightWheelSpeed) | ||
} | ||
/** | ||
* Control both wheels in one function. | ||
* Speeds range from -100 to 100. | ||
* Negative speeds go backwards, positive go forwards. | ||
*/ | ||
//% block | ||
//% blockId=motion_drive block="drive |left: %leftWheelSpeed|right: %rightWheelSpeed" | ||
//% leftWheelSpeed.min=-100 leftWheelSpeed.max=100 | ||
//% rightWheelSpeed.min=-100 rightWheelSpeed.max=100 | ||
//% weight=40 | ||
export function drive(leftWheelSpeed: number, rightWheelSpeed: number): void { | ||
motorControl(Motor.LEFT, leftWheelSpeed) | ||
motorControl(Motor.RIGHT, rightWheelSpeed) | ||
} | ||
|
||
/** | ||
* Advanced control of an individual motor. PWM is set to constant value. | ||
*/ | ||
function motorControl(whichMotor: Motor, speed: number): void { | ||
// Pick the motor using some magic values | ||
let [pos, neg] = selectMotor(whichMotor); | ||
/** | ||
* Advanced control of an individual motor. PWM is set to constant value. | ||
*/ | ||
function motorControl(whichMotor: Motor, speed: number): void { | ||
// Pick the motor using some magic values | ||
let [pos, neg] = selectMotor(whichMotor); | ||
|
||
// drive motors | ||
let remappedSpeed = speed * 10; | ||
// forward: power to neg, reverse: power to pos | ||
pins.analogWritePin(neg, 1023 - Math.abs(Math.max(remappedSpeed, 0))); | ||
pins.analogWritePin(pos, 1023 - Math.abs(Math.min(0, remappedSpeed))); | ||
} | ||
// drive motors | ||
let remappedSpeed = speed * 10; | ||
// forward: power to neg, reverse: power to pos | ||
pins.analogWritePin(neg, 1023 - Math.abs(Math.max(remappedSpeed, 0))); | ||
pins.analogWritePin(pos, 1023 - Math.abs(Math.min(0, remappedSpeed))); | ||
} | ||
|
||
function selectMotor(whichMotor: Motor): AnalogPin[] { | ||
let pos : AnalogPin, neg : AnalogPin; | ||
switch (whichMotor) { | ||
case Motor.LEFT: | ||
pos = cakLand.M1_POS; | ||
neg = cakLand.M1_NEG; | ||
break; | ||
case Motor.RIGHT: | ||
pos = cakLand.M2_POS; | ||
neg = cakLand.M2_NEG; | ||
break; | ||
default: | ||
pos = cakLand.M1_POS; | ||
neg = cakLand.M1_NEG; | ||
break; | ||
}; | ||
return [pos, neg]; | ||
} | ||
function selectMotor(whichMotor: Motor): AnalogPin[] { | ||
let pos : AnalogPin, neg : AnalogPin; | ||
switch (whichMotor) { | ||
case Motor.LEFT: | ||
pos = cakLand.M1_POS; | ||
neg = cakLand.M1_NEG; | ||
break; | ||
case Motor.RIGHT: | ||
pos = cakLand.M2_POS; | ||
neg = cakLand.M2_NEG; | ||
break; | ||
default: | ||
pos = cakLand.M1_POS; | ||
neg = cakLand.M1_NEG; | ||
break; | ||
}; | ||
return [pos, neg]; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,75 +1,75 @@ | ||
enum Pump { | ||
//% weight=13 color=#01579b icon="" block="Pump" | ||
namespace cakLandPump { | ||
enum Pump { | ||
//% block="left" | ||
LEFT = 0, | ||
//% block="right" | ||
RIGHT = 1 | ||
} | ||
} | ||
|
||
//% weight=13 color=#01579b icon="" block="Pump" | ||
namespace cakLandPump { | ||
|
||
/** | ||
* Start the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_start block="start %pump pump at speed %speed" | ||
//% speed.min=0 speed.max=100 | ||
//% weight=45 | ||
export function start(pump: Pump, speed: number): void { | ||
pumpControl(pump, speed) | ||
} | ||
/** | ||
* Start the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_start block="start %pump pump at speed %speed" | ||
//% speed.min=0 speed.max=100 | ||
//% weight=45 | ||
export function start(pump: Pump, speed: number): void { | ||
pumpControl(pump, speed) | ||
} | ||
|
||
/** | ||
* Stop the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_stop block="stop %pump pump" | ||
//% weight=45 | ||
export function stop(pump: Pump): void { | ||
pumpControl(pump, 0) | ||
} | ||
/** | ||
* Stop the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_stop block="stop %pump pump" | ||
//% weight=45 | ||
export function stop(pump: Pump): void { | ||
pumpControl(pump, 0) | ||
} | ||
|
||
/** | ||
* Set a pump for a specified time at a specified speed. | ||
*/ | ||
//% block | ||
//% blockId=pump_duration block="run %pump pump at speed %speed for %duration seconds" | ||
//% duration.min=0 duration.max=10 | ||
//% speed.min=0 speed.max=100 | ||
//% weight=45 | ||
export function startDuration(pump: Pump, speed: number, duration: number): void { | ||
start(pump, speed) | ||
basic.pause(duration*1000) | ||
stop(pump) | ||
} | ||
/** | ||
* Set a pump for a specified time at a specified speed. | ||
*/ | ||
//% block | ||
//% blockId=pump_duration block="run %pump pump at speed %speed for %duration seconds" | ||
//% duration.min=0 duration.max=10 | ||
//% speed.min=0 speed.max=100 | ||
//% weight=45 | ||
export function startDuration(pump: Pump, speed: number, duration: number): void { | ||
start(pump, speed) | ||
basic.pause(duration*1000) | ||
stop(pump) | ||
} | ||
|
||
/** | ||
* Advanced control of an individual pump. PWM is set to constant value. | ||
*/ | ||
function pumpControl(whichPump: Pump, speed: number): void { | ||
let pumpSpeed: number | ||
/** | ||
* Advanced control of an individual pump. PWM is set to constant value. | ||
*/ | ||
function pumpControl(whichPump: Pump, speed: number): void { | ||
let pumpSpeed: number | ||
|
||
pumpSpeed = remapSpeed(speed) | ||
pumpSpeed = remapSpeed(speed) | ||
|
||
if (whichPump == Pump.LEFT) { | ||
pins.analogSetPeriod(cakLand.M1_NEG, 1024) | ||
pins.analogWritePin(cakLand.M1_NEG, pumpSpeed) | ||
} else { | ||
pins.analogSetPeriod(cakLand.M2_NEG, 1024) | ||
pins.analogWritePin(cakLand.M2_NEG, pumpSpeed) | ||
} | ||
if (whichPump == Pump.LEFT) { | ||
pins.analogSetPeriod(cakLand.M1_NEG, 1024) | ||
pins.analogWritePin(cakLand.M1_NEG, pumpSpeed) | ||
} else { | ||
pins.analogSetPeriod(cakLand.M2_NEG, 1024) | ||
pins.analogWritePin(cakLand.M2_NEG, pumpSpeed) | ||
} | ||
} | ||
|
||
// Rescale values from 0 - 100 to 0 - 1023 | ||
function remapSpeed(s: number): number { | ||
let returnSpeed: number | ||
if (s <= 0) { | ||
returnSpeed = 0 | ||
} else if (s >= 100) { | ||
returnSpeed = 1023 | ||
} else { | ||
returnSpeed = (23200 + (s * 791)) / 100 | ||
} | ||
return returnSpeed; | ||
// Rescale values from 0 - 100 to 0 - 1023 | ||
function remapSpeed(s: number): number { | ||
let returnSpeed: number | ||
if (s <= 0) { | ||
returnSpeed = 0 | ||
} else if (s >= 100) { | ||
returnSpeed = 1023 | ||
} else { | ||
returnSpeed = (23200 + (s * 791)) / 100 | ||
} | ||
return returnSpeed; | ||
} | ||
} |
Oops, something went wrong.