-
Notifications
You must be signed in to change notification settings - Fork 2
/
motor.ts
356 lines (293 loc) · 10.4 KB
/
motor.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*******************************************************************************
* Functions for MOTION:BIT servos and motor drivers.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: support@cytron.io
*******************************************************************************/
// I2C slave address for PCA9685.
const PCA9685_I2C_ADDRESS = 0x40;
// PCA9685 Register address.
const REG_ADD_MODE1 = 0x00;
const REG_ADD_LED0_ON_L = 0x06;
const REG_ADD_ALL_LED_ON_L = 0xFA;
const REG_ADD_ALL_LED_OFF_L = 0xFC;
const REG_ADD_PRESCALE = 0xFE;
// PWM frequency.
const PWM_FREQ = 50;
// Motor channel.
// The value equals to the output channel of PCA9685 (For MA).
enum MotionBitMotorChannel {
M1 = 4,
M2 = 6,
M3 = 10,
M4 = 8,
//% block="all"
All = 1000,
};
// Motor direction.
enum MotionBitMotorDirection {
//% block="forward"
Forward = 0,
//% block="backward"
Backward = 1
};
// Servo Channel.
// The value equals to the output channel of PCA9685.
enum MotionBitServoChannel {
S1 = 0,
S2 = 1,
S3 = 2,
S4 = 3,
S5 = 15,
S6 = 14,
S7 = 13,
S8 = 12,
//% block="all"
All = 1000,
};
/**
* Blocks for MOTION:BIT.
*/
//% weight=10 color=#ff8000 icon="\uf085" block="MOTION:BIT"
//% groups=['DC Motors', 'Servos', 'RGB LED', 'External RGB LED']
namespace motionbit {
// Turn off all outputs.
clearAllOutputs();
/**
* Brake the motor
* @param motor Motor channel.
*/
//% group="DC Motors"
//% weight=20
//% blockGap=8
//% blockId=motionbit_brake_motor
//% block="brake motor %motor"
export function brakeMotor(motor: MotionBitMotorChannel): void {
// Initialize the PCA9685 if it's not done yet.
// This helps if the power is turned off while microbit is connected to USB.
initPCA9685(PWM_FREQ);
if (motor == MotionBitMotorChannel.All) {
setPWM(MotionBitMotorChannel.M1, 0);
setPWM(MotionBitMotorChannel.M1 + 1, 0);
setPWM(MotionBitMotorChannel.M2, 0);
setPWM(MotionBitMotorChannel.M2 + 1, 0);
setPWM(MotionBitMotorChannel.M3, 0);
setPWM(MotionBitMotorChannel.M3 + 1, 0);
setPWM(MotionBitMotorChannel.M4, 0);
setPWM(MotionBitMotorChannel.M4 + 1, 0);
} else {
setPWM(motor, 0);
setPWM(motor + 1, 0);
}
}
/**
* Run the motor forward or backward (Speed = 0-255).
* @param motor Motor channel.
* @param direction Motor direction.
* @param speed Motor speed (0-255). eg: 128
*/
//% group="DC Motors"
//% weight=19
//% blockGap=40
//% blockId=motionbit_run_motor
//% block="run motor %motor %direction at speed %speed"
//% speed.min=0 speed.max=255
export function runMotor(motor: MotionBitMotorChannel, direction: MotionBitMotorDirection, speed: number): void {
speed = Math.constrain(speed, 0, 255);
speed = Math.map(speed, 0, 255, 0, 4095);
// Initialize the PCA9685 if it's not done yet.
// This helps if the power is turned off while microbit is connected to USB.
initPCA9685(PWM_FREQ);
if (direction == MotionBitMotorDirection.Forward) {
if (motor == MotionBitMotorChannel.All) {
setPWM(MotionBitMotorChannel.M1, speed);
setPWM(MotionBitMotorChannel.M1 + 1, 0);
setPWM(MotionBitMotorChannel.M2, speed);
setPWM(MotionBitMotorChannel.M2 + 1, 0);
setPWM(MotionBitMotorChannel.M3, speed);
setPWM(MotionBitMotorChannel.M3 + 1, 0);
setPWM(MotionBitMotorChannel.M4, speed);
setPWM(MotionBitMotorChannel.M4 + 1, 0);
} else {
setPWM(motor, speed);
setPWM(motor + 1, 0);
}
}
else{
if (motor == MotionBitMotorChannel.All) {
setPWM(MotionBitMotorChannel.M1, 0);
setPWM(MotionBitMotorChannel.M1 + 1, speed);
setPWM(MotionBitMotorChannel.M2, 0);
setPWM(MotionBitMotorChannel.M2 + 1, speed);
setPWM(MotionBitMotorChannel.M3, 0);
setPWM(MotionBitMotorChannel.M3 + 1, speed);
setPWM(MotionBitMotorChannel.M4, 0);
setPWM(MotionBitMotorChannel.M4 + 1, speed);
} else {
setPWM(motor, 0);
setPWM(motor + 1, speed);
}
}
}
/**
* Disable the servo.
* @param servo Servo channel.
*/
//% group="Servos"
//% weight=18
//% blockGap=8
//% blockId=motionbit_disable_servo
//% block="disable servo %servo"
export function disableServo(servo: MotionBitServoChannel): void {
// Initialize the PCA9685 if it's not done yet.
// This helps if the power is turned off while microbit is connected to USB.
initPCA9685(PWM_FREQ);
if (servo == MotionBitServoChannel.All) {
setServoPulseWidth(MotionBitServoChannel.S1, 0);
setServoPulseWidth(MotionBitServoChannel.S2, 0);
setServoPulseWidth(MotionBitServoChannel.S3, 0);
setServoPulseWidth(MotionBitServoChannel.S4, 0);
setServoPulseWidth(MotionBitServoChannel.S5, 0);
setServoPulseWidth(MotionBitServoChannel.S6, 0);
setServoPulseWidth(MotionBitServoChannel.S7, 0);
setServoPulseWidth(MotionBitServoChannel.S8, 0);
}
else {
setServoPulseWidth(servo, 0);
}
}
/**
* Set the position for servo (0-180 degrees).
* @param servo Servo channel.
* @param position Servo positon. eg: 90
*/
//% group="Servos"
//% weight=17
//% blockGap=40
//% blockId=motionbit_set_servo_position
//% block="set servo %servo position to %position degrees"
//% position.min=0 position.max=180
export function setServoPosition(servo: MotionBitServoChannel, position: number): void {
position = Math.constrain(position, 0, 180);
let pulseWidth = position * 2000 / 180 + 500;
// Initialize the PCA9685 if it's not done yet.
// This helps if the power is turned off while microbit is connected to USB.
initPCA9685(PWM_FREQ);
if (servo == MotionBitServoChannel.All) {
setServoPulseWidth(MotionBitServoChannel.S1, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S2, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S3, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S4, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S5, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S6, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S7, pulseWidth);
setServoPulseWidth(MotionBitServoChannel.S8, pulseWidth);
}
else {
setServoPulseWidth(servo, pulseWidth);
}
}
/**
* I2C write 8-bit data to the register of PCA9685.
* @param register Register address.
* @param data Data to write.
*/
function i2cWrite(register: number, data: number): void {
let buffer = pins.createBuffer(2);
buffer[0] = register;
buffer[1] = data;
pins.i2cWriteBuffer(PCA9685_I2C_ADDRESS, buffer);
}
/**
* I2C write 2 x 16-bit data to the register of PCA9685.
* @param register Register address.
* @param data1 First 16-bit data to write.
* @param data2 Second 16-bit data to write.
*/
function i2cWrite16x2(register: number, data1: number, data2: number): void {
let buffer = pins.createBuffer(5);
buffer[0] = register;
buffer[1] = data1 & 0xff;
buffer[2] = (data1 >> 8) & 0xff;
buffer[3] = data2 & 0xff;
buffer[4] = (data2 >> 8) & 0xff;
pins.i2cWriteBuffer(PCA9685_I2C_ADDRESS, buffer);
}
/**
* I2C read 8-bit data from the register of PCA9685.
* Return the data.
* @param register Register address.
*/
function i2cRead(register: number): number {
pins.i2cWriteNumber(PCA9685_I2C_ADDRESS, register, NumberFormat.UInt8LE, true);
return pins.i2cReadNumber(PCA9685_I2C_ADDRESS, NumberFormat.UInt8LE);
}
/**
* Turn off all outputs of PCA9685.
*/
function clearAllOutputs(): void {
// Initialize the PCA9685 if it's not done yet.
// This helps if the power is turned off while microbit is connected to USB.
initPCA9685(PWM_FREQ);
i2cWrite16x2(REG_ADD_ALL_LED_ON_L, 0x0000, 0x1000);
}
/**
* Initialize and reset the output of PCA9685.
* @param freq PWM frequency.
*/
function initPCA9685(freq: number): void {
// Return if the PCA9685 is already initialized (Default prescale value = 30).
if (i2cRead(REG_ADD_PRESCALE) != 30) {
return;
}
// Enable Register Auto-Increment and go to sleep.
i2cWrite(REG_ADD_MODE1, 0x30);
// Set the prescale.
let prescale = (25000000 / (freq * 4096)) - 1;
i2cWrite(REG_ADD_PRESCALE, prescale);
// Turn off all outputs.
clearAllOutputs();
// Wake up.
i2cWrite(REG_ADD_MODE1, 0x20);
// Wait for oscillator to stabilize.
control.waitMicros(5000);
// Restart.
i2cWrite(REG_ADD_MODE1, 0xA0);
return;
}
/**
* Set the PWM duty cycle of the motor driver.
* @param channel PCA9685 output channel (0-15).
* @param pwm PWM duty cycle (0-4095).
*/
function setPWM(channel: number, pwm: number): void {
// Make sure the channel is valid.
if (channel < 0 || channel > 15) {
return;
}
let regAdd = REG_ADD_LED0_ON_L + (channel * 4);
// Always off.
if (pwm <= 0) {
i2cWrite16x2(regAdd, 0x0000, 0x1000);
}
// Always on.
else if (pwm >= 4095) {
i2cWrite16x2(regAdd, 0x1000, 0x0000);
}
// In between.
else {
i2cWrite16x2(regAdd, 0x0000, pwm);
}
}
/**
* Set the servo pulse width.
* @param channel PCA9685 output channel (0-15).
* @param pulseWidth Servo pulse width (0 or 500 - 2500us).
*/
function setServoPulseWidth(channel: number, pulseWidth: number): void {
// Freq = 50Hz, Period = 20,000 us
let pwm = pulseWidth * 4096 / 20000;
setPWM(channel, pwm);
}
}