forked from CytronTechnologies/pxt-sumobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.ts
201 lines (170 loc) · 5.53 KB
/
edge.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
/*******************************************************************************
* Functions for SUMO:BIT edge sensors
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Default pin.
const EDGE_R_PIN = AnalogPin.P0;
const EDGE_L_PIN = AnalogPin.P1;
// Event source.
const EDGE_SENSOR_EVENT_SOURCE = 0x03;
let sumobitRightThreshold: number;
let sumobitLeftThreshold: number;
let sumobitRightEdgeValue: number;
let sumobitLeftEdgeValue: number;
// Edge sensor selection
enum SumobitEdgeSelection {
//% block="right"
Right = 0,
//% block="left"
Left = 1,
//% block="both"
//% blockHidden=true
Both = 1000
}
// Comparison type
enum SumobitCompareType {
//% block=">"
MoreThan = 0,
//% block="<"
LessThan = 1
};
namespace sumobit {
/**
* Read the right edge sensor value (0-1023).
*/
//% group="Edge Sensors"
//% weight=79
//% blockGap=8
//% blockId=sumobit_edge_read_analog_right
//% block="right edge sensor"
//% blockHidden=true
export function readRightEdgeValue(): number {
sumobitRightEdgeValue = pins.analogReadPin(EDGE_R_PIN);
return pins.analogReadPin(EDGE_R_PIN);
}
/**
* Read the left edge sensor value (0-1023).
*/
//% group="Edge Sensors"
//% weight=78
//% blockGap=8
//% blockId=sumobit_edge_read_analog_left
//% block="left edge sensor"
//% blockHidden=true
export function readLeftEdgeValue(): number {
sumobitLeftEdgeValue = pins.analogReadPin(EDGE_L_PIN);
return sumobitLeftEdgeValue;
}
/**
* Return the edge sensor value (0-1023).
*/
//% group="Edge Sensors"
//% weight=77
//% blockGap=8
//% blockId=sumobit_edge_return_value
//% block="%edge edge sensor"
export function fetchEdgeValue(edge: SumobitEdgeSelection): number {
switch (edge) {
case SumobitEdgeSelection.Right:
return readRightEdgeValue();
case SumobitEdgeSelection.Left:
return readLeftEdgeValue();
case SumobitEdgeSelection.Both:
return readRightEdgeValue();
}
}
/**
* Compare the edge sensor value (0-1023) with certain value and return true if condition is meet.
* @param EdgeSide Which side of edge sensors are to compare to. eg: 0
* @param compareType More than or less than. eg: 0
* @param threshold The value to compare with. eg: 512
*/
//% group="Edge Sensors"
//% weight=76
//% blockGap=40
//% blockId=sumobit_edge_compare_user_threshold
//% block="%edge edge sensor %compareType %threshold"
//% threshold.min=0 threshold.max=1023
//% blockHidden=true
export function compareEdgeDefined(edge: SumobitEdgeSelection, compareType: SumobitCompareType, threshold: number,): boolean {
let result = false;
let R = readRightEdgeValue();
let L = readLeftEdgeValue();
switch (edge) {
case SumobitEdgeSelection.Right:
if ((R > threshold && SumobitCompareType.MoreThan) || (R < threshold && SumobitCompareType.LessThan)) {
result = true;
}
break;
case SumobitEdgeSelection.Left:
if ((L > threshold && SumobitCompareType.MoreThan) || (L < threshold && SumobitCompareType.LessThan)) {
result = true;
}
break;
case SumobitEdgeSelection.Both:
if (((R > threshold && L > threshold) && SumobitCompareType.MoreThan) || ((R > threshold && L > threshold) && SumobitCompareType.LessThan)) {
result = true;
}
break;
}
return result;
}
/**
* Edge threshold auto calibration.
*/
//% group="Edge Sensors"
//% weight=75
//% blockGap=8
//% blockId=sumobit_edge_calibrate_threshold
//% block="calibrate edge sensor"
export function calibrateEdgeThreshold(): void {
let startTime1 = control.millis();
while (control.millis() - startTime1 < 1000) {
sumobitRightThreshold = pins.analogReadPin(EDGE_R_PIN)*0.5;
sumobitLeftThreshold = pins.analogReadPin(EDGE_L_PIN)*0.5;
}
}
/**
* Return the calibrated edge threshold in seriel monitor for troubleshooting purpose.
*/
//% group="Edge Sensors"
//% weight=74
//% blockGap=8
//% blockId=sumobit_edge_calibrated_threshold_serial
//% block="calibrated edge threshold"
//% blockHidden=true
export function fetchCalibratedEdgeThreshold(): void {
serial.writeString("Left: ")
serial.writeNumber(sumobitLeftThreshold)
serial.writeString(" Right: ")
serial.writeNumber(sumobitRightThreshold)
serial.writeLine("")
}
/**
* Compare.
*/
//% group="Edge Sensors"
//% weight=73
//% blockGap=8
//% blockId=sumobit_edge_compare_calibrated_value
//% block="%edge sensor detect edge"
export function compareEdgeCalibrated(edge: SumobitEdgeSelection): boolean {
let result = false;
switch (edge) {
case SumobitEdgeSelection.Right:
if (sumobitRightEdgeValue < sumobitRightThreshold) {
result = true;
}
break;
case SumobitEdgeSelection.Left:
if (sumobitLeftEdgeValue < sumobitLeftThreshold) {
result = true;
}
break;
}
return result;
}
}