-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
182 lines (171 loc) · 4.73 KB
/
main.ts
File metadata and controls
182 lines (171 loc) · 4.73 KB
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
const board_address = 0x10
/**
* Functions to WuKong multifunctional expansion board by ELECFREAKS Co.,Ltd.
*/
//% block="Drive" blockId="Drive" color=#000000 icon="\uf013"
namespace Drive {
/**
* MotorList
*/
export enum MotorList {
//% block="M1"
M1,
//% block="M2"
M2
}
/**
* ServoList
*/
export enum ServoList {
//% block="S0" enumval=0
S0,
//% block="S1" enumval=1
S1,
//% block="S2" enumval=2
S2,
//% block="S3" enumval=3
S3,
//% block="S4" enumval=4
S4,
//% block="S5" enumval=5
S5,
//% block="S6" enumval=6
S6,
//% block="S7" enumval=7
S7
}
/**
* ServoTypeList
*/
export enum ServoTypeList {
//% block="180°"
_180,
//% block="270°"
_270,
//% block="360°"
_360
}
/**
* TODO: Set the speed of M1 or M2 motor.
* @param motor M1 or M2 motor , eg: MotorList.M1
* @param speed motor speed, eg: 100
*/
//% weight=88
//% blockId=setMotorSpeed block="Set motor %motor speed to %speed"
//% speed.min=-100 speed.max=100
//% subcategory="Motors"
export function setMotorSpeed(motor: MotorList, speed: number): void {
let buf = pins.createBuffer(4);
switch (motor) {
case MotorList.M1:
buf[0] = 0x01;
buf[1] = 0x01;
if (speed < 0) {
buf[1] = 0x02;
speed = speed * -1
}
buf[2] = speed;
buf[3] = 0;
pins.i2cWriteBuffer(board_address, buf);
break;
case MotorList.M2:
buf[0] = 0x02;
buf[1] = 0x01;
if (speed < 0) {
buf[1] = 0x02;
speed = speed * -1
}
buf[2] = speed;
buf[3] = 0;
pins.i2cWriteBuffer(board_address, buf);
break;
default:
break;
}
}
/*
* TODO: Set both of M1 and M2 motors speed.
* @param m1speed M1 motor speed , eg: 100
* @param m2speed M2 motor speed, eg: -100
*/
//% weight=87
//% blockId=setAllMotor block="Set motor M1 speed %m1speed M2 speed %m2speed"
//% m1speed.min=-100 m1speed.max=100
//% m2speed.min=-100 m2speed.max=100
//% subcategory="Motors"
export function setAllMotor(m1speed: number, m2speed: number): void {
setMotorSpeed(MotorList.M1, m1speed)
setMotorSpeed(MotorList.M2, m2speed)
}
/*
* TODO: Stop one of the motors.
* @param motor A motor in the MotorList , eg: MotorList.M1
*/
//% weight=86
//% blockId=stopMotor block="Stop motor %motor"
//% subcategory="Motors"
export function stopMotor(motor: MotorList): void {
setMotorSpeed(motor, 0)
}
/*
* TODO: Stop all motors, including M1 and M2.
*/
//% weight=85
//% blockId=stopAllMotor block="Stop all motors"
//% subcategory="Motors"
export function stopAllMotor(): void {
setMotorSpeed(MotorList.M1, 0)
setMotorSpeed(MotorList.M2, 0)
}
/*
* TODO: Setting the angle of a servo motor.
* @param servo A servo in the ServoList , eg: ServoList.S1
* @param angel Angle of servo motor , eg: 90
*/
//% weight=84
//% blockId=setServoAngle block="Set %servoType servo %servo angle to %angle"
//% angle.min=0 angle.max=360
//% subcategory="Servos"
export function setServoAngle(servoType: ServoTypeList, servo: ServoList, angle: number): void {
let buf = pins.createBuffer(4);
if (servo == 0) {
buf[0] = 0x03;
}
if (servo == 1) {
buf[0] = 0x04;
}
if (servo == 2) {
buf[0] = 0x05;
}
if (servo == 3) {
buf[0] = 0x06;
}
if (servo == 4) {
buf[0] = 0x07;
}
if (servo == 5) {
buf[0] = 0x08;
}
if (servo == 6) {
buf[0] = 0x09;
}
if (servo == 7) {
buf[0] = 0x10;
}
switch (servoType) {
case ServoTypeList._180:
angle = Math.map(angle, 0, 180, 0, 180)
break
case ServoTypeList._270:
angle = Math.map(angle, 0, 270, 0, 180)
break
case ServoTypeList._360:
angle = Math.map(angle, 0, 360, 0, 180)
break
}
buf[1] = angle;
buf[2] = 0;
buf[3] = 0;
pins.i2cWriteBuffer(board_address, buf);
}
}