-
Notifications
You must be signed in to change notification settings - Fork 1
/
motor.ts
176 lines (154 loc) · 6.55 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
/*******************************************************************************
* Functions for SUMO:BIT motor control
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Motor direction.
enum SumobitMotorDirection {
//% block="forward"
Forward = 0,
//% block="backward"
Backward = 1
};
// Motor channel.
enum SumobitMotorChannel {
//% block="right"
RightMotor = 0,
//% block="left"
LeftMotor = 1,
//% block="both"
Both = 1000,
};
namespace sumobit {
/**
* Stop motor
* @param motor The motor to control. eg: SumobitMotorChannel.Both
*/
//% group="DC Motors"
//% weight=98
//% blockGap=8
//% blockId=sumobit_motor_stop
//% block="stop %motor motor"
//% motor.defl=SumobitMotorChannel.Both
export function stopMotor(motor: SumobitMotorChannel): void {
switch (motor) {
case SumobitMotorChannel.RightMotor:
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 0);
break;
case SumobitMotorChannel.LeftMotor:
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 0);
break;
case SumobitMotorChannel.Both:
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 0);
break;
}
}
/**
* Run the motor forward or backward (Speed = 0-255) (Acceleration = 1-9).
* @param motor The motor to control. eg: SumobitMotorChannel.Both
* @param direction The direction of rotation (Forward or Backward). eg: SumobitMotorDirection.Forward
* @param speed The Motor speed (PWM). eg: 120
* @param acceleration Acceleration factor, higher value results in a faster rate of speed change (1-9). eg: 9
*/
//% group="DC Motors"
//% weight=99
//% blockGap=8
//% blockId=sumobit_motor_run
//% accel.fieldOptions.label="Acceleration Factor"
//% block="run %motor motor %direction at %speed speed || with %acceleration acceleration factor"
//% inlineInputMode=inline
//% motor.defl=SumobitMotorChannel.Both
//% direction.defl=SumobitMotorDirection.Forward
//% speed.min=0 speed.max=255
//% speed.defl=120
//% acceleration.min=1 acceleration.max=9
//% acceleration.defl=9
//% expandableArgumentMode="enabled"
export function runMotor(motor: SumobitMotorChannel, direction: SumobitMotorDirection, speed: number, acceleration: number = 9): void {
speed = sumobit.limit(speed, 0, 255);
acceleration = sumobit.limit(acceleration, 1, 9);
switch (motor) {
case SumobitMotorChannel.RightMotor:
if (direction == SumobitMotorDirection.Forward) {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL1, acceleration);
}
else {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 1);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL1, acceleration);
}
break;
case SumobitMotorChannel.LeftMotor:
if (direction == SumobitMotorDirection.Forward) {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL2, acceleration);
}
else {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 1);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL2, acceleration);
}
break;
case SumobitMotorChannel.Both:
if (direction == SumobitMotorDirection.Forward) {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL1, acceleration);
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL2, acceleration);
}
else {
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM1, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR1, 1);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL1, acceleration);
sumobit.i2cWrite(SUMOBIT_REG_ADD_PWM2, speed);
sumobit.i2cWrite(SUMOBIT_REG_ADD_DIR2, 1);
sumobit.i2cWrite(SUMOBIT_REG_ADD_ACCEL2, acceleration);
}
break;
}
}
/**
* Set the speed and direction of both motors independently (speed = -255 to 255, negative value = reverse).
* @param leftSpeed The desired speed of the left motor. eg: 120
* @param rightSpeed The desired speed of the right motor. eg: 120
* @param acceleration Acceleration factor, higher value results in a faster rate of speed change (1-9). eg: 9
*/
//% group="DC Motors"
//% weight=97
//% blockGap=8
//% blockId=sumobit_motor_set_speed
//% block="set motors speed: left %leftSpeed right %rightSpeed || acceleration %acceleration "
//% leftSpeed.min=-255 leftSpeed.max=255
//% leftSpeed.defl=120
//% rightSpeed.min=-255 rightSpeed.max=255
//% rightSpeed.defl=120
//% acceleration.min=1 acceleration.max=9
//% acceleration.defl=9
//% expandableArgumentMode="enabled"
export function setMotorsSpeed(leftSpeed: number, rightSpeed: number, acceleration: number = 9): void {
let leftDir = SumobitMotorDirection.Forward;
let rightDir = SumobitMotorDirection.Forward;
if (leftSpeed < 0) {
leftSpeed = -leftSpeed;
leftDir = SumobitMotorDirection.Backward;
}
if (rightSpeed < 0) {
rightSpeed = -rightSpeed;
rightDir = SumobitMotorDirection.Backward;
}
sumobit.runMotor(0, rightDir, rightSpeed, acceleration);
sumobit.runMotor(1, leftDir, leftSpeed, acceleration);
}
}