-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservolite.ts
157 lines (142 loc) · 5.47 KB
/
servolite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//% weight=100 color=#00A654 icon="\uf21c" block="Servo"
namespace kitronik_servo {
/*some parameters used for controlling the turn and length of the ServoLite board controlled :MOVE mini */
const microSecInASecond = 1000000
let distancePerSec = 100
let numberOfDegreesPerSec = 200
/**
* Drives forwards. Call stop to stop
*/
//% blockId=kitronik_servolite_servos_forward
//% block="drive forward"
export function forward(): void {
pins.servoWritePin(AnalogPin.P1, 0);
pins.servoWritePin(AnalogPin.P2, 180);
}
/**
* Drives backwards. Call stop to stop
*/
//% blockId=kitronik_servolite_servos_backward
//% block="drive dd backward"
export function backward(): void {
pins.servoWritePin(AnalogPin.P1, 180);
pins.servoWritePin(AnalogPin.P2, 0);
}
/**
* Turns left. Call stop to stop
*/
//% blockId=kitronik_servolite_servos_left
//% block="turn left"
export function left(): void {
pins.servoWritePin(AnalogPin.P1, 0);
pins.servoWritePin(AnalogPin.P2, 0);
}
/**
* Turns right. Call ``stop`` to stop
*/
//% blockId=kitronik_servolite_servos_right
//% block="turn right"
export function right(): void {
pins.servoWritePin(AnalogPin.P1, 180);
pins.servoWritePin(AnalogPin.P2, 180);
}
/**
* Stop for 360 servos.
* rather than write 90, which may not stop the servo moving if it is out of trim
* this stops sending servo pulses, which has the same effect.
* On a normal servo this will stop the servo where it is, rather than return it to neutral position.
* It will also not provide any holding force.
*/
//% blockId=kitronik_servolite_servos_stop
//% block="stop"
export function stop(): void {
pins.analogWritePin(AnalogPin.P1, 0);
pins.analogWritePin(AnalogPin.P2, 0);
}
/**
* Sends servos to 'neutral' position.
* On a well trimmed 360 this is stationary, on a normal servo this is 90 degrees.
*/
//% blockId=kitronik_servolite_servos_neutral
//% block="goto neutral position"
export function neutral(): void {
pins.servoWritePin(AnalogPin.P1, 90);
pins.servoWritePin(AnalogPin.P2, 90);
}
/**
* Drives forwards the requested distance and then stops
* @param howFar distance to move
*/
//% blockId=kitronik_servolite_drive_forwards
//% block="drive forwards %howFar|distance"
export function driveForwards(howFar: number): void {
let timeToWait = (howFar * microSecInASecond) / distancePerSec; // calculation done this way round to avoid zero rounding
forward();
control.waitMicros(timeToWait);
stop();
}
/**
* Drives backwards the requested distance and then stops
* @param howFar distance to move
*/
//% blockId=kitronik_servolite_drive_backwards
//% block="drive backwards %howFar|distance"
export function driveBackwards(howFar: number): void {
let timeToWait = (howFar * microSecInASecond) / distancePerSec; // calculation done this way round to avoid zero rounding
backward();
control.waitMicros(timeToWait);
stop();
}
/**
* Turns right through the requested degrees and then stops
* needs NumberOfDegreesPerSec tuned to make accurate, as it uses
* a simple turn, wait, stop method.
* Runs the servos at slower than the right function to reduce wheel slip
* @param deg how far to turn, eg: 90
*/
//% blockId=kitronik_servolite_turn_right
//% block="turn right %deg|degrees"
export function turnRight(deg: number): void {
let timeToWait = (deg * microSecInASecond) / numberOfDegreesPerSec;// calculation done this way round to avoid zero rounding
pins.servoWritePin(AnalogPin.P1, 130);
pins.servoWritePin(AnalogPin.P2, 130);
control.waitMicros(timeToWait);
stop();
}
/**
* Turns left through the requested degrees and then stops
* needs NumberOfDegreesPerSec tuned to make accurate, as it uses
* a simple turn, wait, stop method.
* Runs the servos at slower than the right function to reduce wheel slip
* @param deg how far to turn, eg: 90
*/
//% blockId=kitronik_servolite_turn_left
//% block="turn left %deg|degrees"
export function turnLeft(deg: number): void {
let timeToWait = (deg * microSecInASecond) / numberOfDegreesPerSec;// calculation done this way round to avoid zero rounding
pins.servoWritePin(AnalogPin.P1, 50);
pins.servoWritePin(AnalogPin.P2, 50);
control.waitMicros(timeToWait);
stop()
}
/**
* Allows the setting of the :MOVE mini turn speed.
* This allows tuning for the turn x degrees commands
* @param degPerSec : How many degrees per second the mini does.
*/
//% blockId=kitronik_servolite_set_turn_speed_param
//% block="calibrate turn speed to %DegPerSec|degrees per second"
export function setDegreesPerSecond(degPerSec: number): void {
numberOfDegreesPerSec = degPerSec
}
/**
* Allows the setting of the :MOVE mini forward / reverse speed.
* This allows tuning for the move x distance commands
* @param DegPerSec : How many degrees per second the mini does.
*/
//% blockId=kitronik_servolite_set_movement_speed_param
//% block="calibrate forward speed to %DistPerSec|mm per second"
export function setDistancePerSecond(distPerSec: number): void {
distancePerSec = distPerSec
}
}