forked from CytronTechnologies/pxt-sumobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.ts
170 lines (147 loc) · 5.77 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
/*******************************************************************************
* 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 Motorchannel eg: SumobitMotorChannel.RightMotor
*/
//% group="DC Motors"
//% weight=98
//% blockGap=8
//% blockId=sumobit_motor_stop
//% block="stop %motor motor"
export function stopMotor(motor: SumobitMotorChannel): void {
switch (motor) {
case SumobitMotorChannel.RightMotor:
sumobit.i2cWrite(REG_ADD_PWM1, 0);
sumobit.i2cWrite(REG_ADD_DIR1, 0);
break;
case SumobitMotorChannel.LeftMotor:
sumobit.i2cWrite(REG_ADD_PWM2, 0);
sumobit.i2cWrite(REG_ADD_DIR2, 0);
break;
case SumobitMotorChannel.Both:
sumobit.i2cWrite(REG_ADD_PWM1, 0);
sumobit.i2cWrite(REG_ADD_DIR1, 0);
sumobit.i2cWrite(REG_ADD_PWM2, 0);
sumobit.i2cWrite(REG_ADD_DIR2, 0);
break;
}
}
/**
* Run the motor forward or backward.
* (Speed = 0-255) (Acceleration = 1-9).
* @param motor Motor channel.
* @param direction Motor direction.
* @param speed Motor speed (PWM). eg: 128
* @param acceleration Acceleration factor (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
//% speed.min=0 speed.max=255
//% acceleration.min=1 acceleration.max=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(REG_ADD_PWM1, speed);
sumobit.i2cWrite(REG_ADD_DIR1, 0);
sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration);
}
else {
sumobit.i2cWrite(REG_ADD_PWM1, speed);
sumobit.i2cWrite(REG_ADD_DIR1, 1);
sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration);
}
break;
case SumobitMotorChannel.LeftMotor:
if (direction == SumobitMotorDirection.Forward) {
sumobit.i2cWrite(REG_ADD_PWM2, speed);
sumobit.i2cWrite(REG_ADD_DIR2, 0);
sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration);
}
else {
sumobit.i2cWrite(REG_ADD_PWM2, speed);
sumobit.i2cWrite(REG_ADD_DIR2, 1);
sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration);
}
break;
case SumobitMotorChannel.Both:
if (direction == SumobitMotorDirection.Forward) {
sumobit.i2cWrite(REG_ADD_PWM1, speed);
sumobit.i2cWrite(REG_ADD_DIR1, 0);
sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration);
sumobit.i2cWrite(REG_ADD_PWM2, speed);
sumobit.i2cWrite(REG_ADD_DIR2, 0);
sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration);
}
else {
sumobit.i2cWrite(REG_ADD_PWM1, speed);
sumobit.i2cWrite(REG_ADD_DIR1, 1);
sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration);
sumobit.i2cWrite(REG_ADD_PWM2, speed);
sumobit.i2cWrite(REG_ADD_DIR2, 1);
sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration);
}
break;
}
}
/**
* Set individual motors speed (speed = -255 to 255, negative value = reverse).
* @param leftSpeed Speed for left motor. eg: 100
* @param rightSpeed Speed for right motor. eg: 100
* @param acceleration Speed for right motor. 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
//% rightSpeed.min=-255 rightSpeed.max=255
//% acceleration.min=1 acceleration.max=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);
}
}