-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmotorcurrent.ts
More file actions
199 lines (162 loc) · 6.3 KB
/
motorcurrent.ts
File metadata and controls
199 lines (162 loc) · 6.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
* Functions for sumo:bit motor current measurement.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: support@cytron.io
*******************************************************************************/
// Motor channel (without "both")
enum SumobitMotorChannel2 {
//% block="right"
RightMotor = 0,
//% block="left"
LeftMotor = 1,
};
namespace sumobit {
// Mode event source flag.
const MOTORCURRENT_EVENT_SOURCE = 0x02;
// Flag for background function creation
let bgFunctionCreated = false;
// Event type.
let eventType = 0;
// Array for mode value.
let thresholdsArray: number[] = [];
let compareTypesArray: SumobitCompareType[] = [];
let motorArray: SumobitMotorChannel[] = [];
// Array for old compare result.
let oldCompareResult: boolean[] = [];
/**
* Read the right motor current value (2 d.p.).
*/
//% group="Motor Current"
//% weight=59
//% blockGap=8
//% blockId=sumobit_current_read_analog_m1
//% block="right motor current"
//% blockHidden=true
export function readRightMotorCurrentValue(): number {
let highByte: number;
let lowByte: number;
highByte = sumobit.i2cRead(SUMOBIT_REG_ADD_AN1_HIGH);
lowByte = sumobit.i2cRead(SUMOBIT_REG_ADD_AN1_LOW);
return ((highByte << 8) | lowByte) / 100;
}
/**
* Read the left motor current value (2 d.p.).
*/
//% group="Motor Current"
//% weight=58
//% blockGap=8
//% blockId=sumobit_current_read_analog_m2
//% block="left motor current"
//% blockHidden=true
export function readLeftMotorCurrentValue(): number {
let highByte: number;
let lowByte: number;
highByte = sumobit.i2cRead(SUMOBIT_REG_ADD_AN2_HIGH);
lowByte = sumobit.i2cRead(SUMOBIT_REG_ADD_AN2_LOW);
return ((highByte << 8) | lowByte) / 100;
}
/**
* Read motor current value (2 d.p.).
*/
//% group="Motor Current"
//% weight=58
//% blockGap=8
//% blockId=sumobit_current_return_value
//% block="$motor motor current"
//% blockHidden=true
export function fetchMotorCurrentValue(motor: SumobitMotorChannel2): number {
switch (motor) {
case SumobitMotorChannel2.RightMotor:
return readRightMotorCurrentValue();
case SumobitMotorChannel2.LeftMotor:
return readLeftMotorCurrentValue();
}
}
/**
* Compare the motor current value (0.00-20.00) with a threshold value and returns the result (true/false).
*/
//% group="Motor Current"
//% weight=57
//% blockGap=40
//% blockId=sumobit_current_compare_value
//% block="%motor current %compareType %threshold"
//% threshold.min=0.00 threshold.max=20.00
//% blockHidden=true
export function compareCurrent(motor: SumobitMotorChannel, compareType: SumobitCompareType, threshold: number,): boolean {
let result = false;
let a = readRightMotorCurrentValue();
let b = readLeftMotorCurrentValue();
switch (motor) {
case SumobitMotorChannel.RightMotor:
if ((a > threshold && SumobitCompareType.MoreThan) || (a < threshold && SumobitCompareType.LessThan)) {
result = true;
}
break;
case SumobitMotorChannel.LeftMotor:
if ((b > threshold && SumobitCompareType.MoreThan) || (b < threshold && SumobitCompareType.LessThan)) {
result = true;
}
break;
case SumobitMotorChannel.Both:
if (((a > threshold && b > threshold) && SumobitCompareType.MoreThan) || ((a > threshold && b > threshold) && SumobitCompareType.LessThan)) {
result = true;
}
break;
}
return result;
}
/**
* Compare the motor current value with a threshold value and do something when true.
* @param motor right motor, left motor or right and left motors.
* @param compareType More than or less than.
* @param threshold The value to compare with. eg: 7.00
* @param handler Code to run when the event is raised.
*/
//% group="Motor Current"
//% weight=56
//% blockGap=40
//% blockId=sumobit_current_event
//% block="on %motor current %compareType %threshold"
//% threshold.min=0.00 threshold.max=20.00
//% blockHidden=true
export function onEvent(motor: SumobitMotorChannel, compareType: SumobitCompareType, threshold: number, handler: Action): void {
// Use a new event type everytime a new event is create.
eventType++;
// Add the event info to the arrays.
motorArray.push(motor);
compareTypesArray.push(compareType);
thresholdsArray.push(threshold);
// Create a placeholder for the old compare result.
oldCompareResult.push(false);
// Register the event.
control.onEvent(MOTORCURRENT_EVENT_SOURCE, eventType, handler);
// Create a function in background if haven't done so.
// This function will check for pot value and raise the event if the condition is met.
if (bgFunctionCreated == false) {
control.inBackground(function () {
while (true) {
// Loop for all the event created.
for (let i = 0; i < eventType; i++) {
// Check if the condition is met.
if (compareCurrent(motorArray[i], compareTypesArray[i], thresholdsArray[i]) == true) {
// Raise the event if the compare result changed from false to true.
if (oldCompareResult[i] == false) {
control.raiseEvent(MOTORCURRENT_EVENT_SOURCE, i + 1);
}
// Save old compare result.
oldCompareResult[i] = true;
}
else {
// Save old compare result.
oldCompareResult[i] = false;
}
basic.pause(10)
}
}
});
bgFunctionCreated = true;
}
}
}