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.
Merge pull request #1 from climate-action-kits/motorfix
Moved DC control to cak-land.ts, fixed PWM(?) bug
- Loading branch information
Showing
4 changed files
with
127 additions
and
183 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,7 +1,42 @@ | ||
//% weight=100 color=#421C52 icon="\uf1b9" | ||
namespace cakLand { | ||
export let M2_NEG: number = DigitalPin.P13 | ||
export let M2_POS: number = DigitalPin.P14 | ||
export let M1_NEG: number = DigitalPin.P15 | ||
export let M1_POS: number = DigitalPin.P16 | ||
// forward: Neg=1023, Pos=0; reverse: Neg=0,Pos=1023 | ||
let M2_NEG: AnalogPin = AnalogPin.P13 | ||
let M2_POS: AnalogPin = AnalogPin.P14 | ||
let M1_NEG: AnalogPin = AnalogPin.P15 | ||
let M1_POS: AnalogPin = AnalogPin.P16 | ||
|
||
export enum BoardSide { | ||
//% block="left" | ||
LEFT, | ||
//% block="right" | ||
RIGHT | ||
} | ||
|
||
function selectPins (side: BoardSide): AnalogPin[] { | ||
let pos: AnalogPin, neg: AnalogPin; | ||
// Climate Action Kits: M2 = Right; M1 = Left | ||
switch (side) { | ||
case BoardSide.LEFT: | ||
neg = M1_NEG; | ||
pos = M1_POS; | ||
break; | ||
case BoardSide.RIGHT: | ||
neg = M2_NEG; | ||
pos = M2_POS; | ||
break; | ||
} | ||
return [pos, neg]; | ||
} | ||
|
||
function percentToAnalog (percent: number): number { | ||
return Math.map( Math.abs( percent ), 0, 100, 0, 1023); | ||
} | ||
|
||
export function powerMotor (side: BoardSide, power: number): void { | ||
let [pos, neg] = selectPins(side); | ||
|
||
pins.analogWritePin(neg, percentToAnalog(Math.max(power, 0))); | ||
pins.analogWritePin(pos, percentToAnalog(Math.min(power, 0))); | ||
} | ||
} |
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
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,39 @@ | ||
/// <reference path="./cak-land.ts" /> | ||
|
||
//% weight=13 color=#01579b icon="" block="Pump" | ||
namespace cakLandPump { | ||
enum Pump { | ||
//% block="left" | ||
LEFT = 0, | ||
//% block="right" | ||
RIGHT = 1 | ||
} | ||
|
||
|
||
/** | ||
* Start the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_start block="start %pump pump at speed %speed" | ||
//% blockId=pump_start block="start %side pump at speed %speed" | ||
//% speed.min=0 speed.max=100 | ||
//% weight=45 | ||
export function start(pump: Pump, speed: number): void { | ||
pumpControl(pump, speed) | ||
export function pumpStart(side: cakLand.BoardSide, speed: number): void { | ||
cakLand.powerMotor(side, speed); | ||
} | ||
|
||
/** | ||
* Stop the pump | ||
*/ | ||
//% block | ||
//% blockId=pump_stop block="stop %pump pump" | ||
//% blockId=pump_stop block="stop %side pump" | ||
//% weight=45 | ||
export function stop(pump: Pump): void { | ||
pumpControl(pump, 0) | ||
export function pumpStop(side: cakLand.BoardSide): void { | ||
cakLand.powerMotor(side, 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" | ||
//% blockId=pump_duration block="run %side 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) | ||
export function startDuration(side: cakLand.BoardSide, speed: number, duration: number): void { | ||
pumpStart(side, 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 | ||
|
||
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) | ||
} | ||
} | ||
|
||
// 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; | ||
pumpStop(side) | ||
} | ||
} |
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