TI RSLK Library 0.2.2
Loading...
Searching...
No Matches
SimpleRSLK.cpp
1#include "SimpleRSLK.h"
2
6
7uint16_t sensorRawValues[LS_NUM_SENSORS];
8uint16_t calibrationValues[LS_NUM_SENSORS];
9uint16_t sensorMinValues[LS_NUM_SENSORS];
10uint16_t sensorMaxValues[LS_NUM_SENSORS];
11uint8_t lineMode;
12
13QTRSensors qtr;
14
16{
19
21
22 bump_sw[0].begin(BP_SW_PIN_0, INPUT_PULLUP);
23 bump_sw[1].begin(BP_SW_PIN_1, INPUT_PULLUP);
24 bump_sw[2].begin(BP_SW_PIN_2, INPUT_PULLUP);
25 bump_sw[3].begin(BP_SW_PIN_3, INPUT_PULLUP);
26 bump_sw[4].begin(BP_SW_PIN_4, INPUT_PULLUP);
27 bump_sw[5].begin(BP_SW_PIN_5, INPUT_PULLUP);
28
29 dst_sensor[0].begin(SHRP_DIST_L_PIN, INPUT_PULLDOWN);
30 dst_sensor[1].begin(SHRP_DIST_C_PIN, INPUT_PULLDOWN);
31 dst_sensor[2].begin(SHRP_DIST_R_PIN, INPUT_PULLDOWN);
32
33 qtr.setTypeRC();
34 qtr.setSensorPins((const uint8_t[]){QTR_7, QTR_6, QTR_5, QTR_4, QTR_3,
35 QTR_2, QTR_1, QTR_0},
39
40 analogReadResolution(14); // ADC defaults to 14-bit resolution
41 clearMinMax(sensorMinValues, sensorMaxValues);
42 lineMode = DARK_LINE;
43}
44
45uint16_t readSharpDist(uint8_t num)
46{
47 if (num < 0 || num >= DST_NUM_SENSORS)
48 return 0;
49
50 return dst_sensor[num].read();
51}
52
53int16_t readSharpDistMM(uint8_t num)
54{
55 if (num < 0 || num >= DST_NUM_SENSORS)
56 return -1;
57
58 return dst_sensor[num].readMM();
59}
60
61float readSharpDistIN(uint8_t num)
62{
63 if (num < 0 || num >= DST_NUM_SENSORS)
64 return -1;
65
66 return dst_sensor[num].readIN();
67}
68
69bool isBumpSwitchPressed(uint8_t num)
70{
71 if (num < 0 || num >= TOTAL_BP_SW)
72 return false;
73
74 if (bump_sw[num].read() == 0) {
75 return true;
76 }
77 return false;
78}
79
81{
82 uint8_t mask = 0;
83 for (int x = 0; x < TOTAL_BP_SW; x++) {
84 if (bump_sw[x].read() == 0) {
85 mask |= (1 << x);
86 }
87 }
88 return mask;
89}
90
91void enableMotor(uint8_t motorNum)
92{
93 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
94 motor[LEFT_MOTOR].enableMotor();
95 }
96
97 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
98 motor[RIGHT_MOTOR].enableMotor();
99 }
100}
101
102void disableMotor(uint8_t motorNum)
103{
104 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
105 motor[LEFT_MOTOR].disableMotor();
106 }
107
108 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
109 motor[RIGHT_MOTOR].disableMotor();
110 }
111}
112
113void pauseMotor(uint8_t motorNum)
114{
115 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
116 motor[LEFT_MOTOR].pauseMotor();
117 }
118
119 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
120 motor[RIGHT_MOTOR].pauseMotor();
121 }
122}
123
124void resumeMotor(uint8_t motorNum)
125{
126 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
127 motor[LEFT_MOTOR].resumeMotor();
128 }
129
130 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
131 motor[RIGHT_MOTOR].resumeMotor();
132 }
133}
134
135void setMotorDirection(uint8_t motorNum, uint8_t direction)
136{
137 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
138 if (direction == 0) {
140 } else if (direction == 1) {
142 }
143 }
144
145 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
146 if (direction == 0) {
148 } else if (direction == 1) {
150 }
151 }
152}
153
154void setMotorSpeed(uint8_t motorNum, uint8_t speed)
155{
156 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
157 motor[LEFT_MOTOR].setSpeed(speed);
158 }
159
160 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
161 motor[RIGHT_MOTOR].setSpeed(speed);
162 }
163}
164
165void setRawMotorSpeed(uint8_t motorNum, uint8_t speed)
166{
167 if (motorNum == LEFT_MOTOR || motorNum == BOTH_MOTORS) {
168 motor[LEFT_MOTOR].setRawSpeed(speed);
169 }
170
171 if (motorNum == RIGHT_MOTOR || motorNum == BOTH_MOTORS) {
172 motor[RIGHT_MOTOR].setRawSpeed(speed);
173 }
174}
175
176void readRawLineSensor(uint16_t *sensorValues)
177{
178 qtr.read(sensorValues, QTRReadMode::OddEven);
179}
180
181void clearMinMax(uint16_t *sensorMin, uint16_t *sensorMax)
182{
183 for (int x = 0; x < LS_NUM_SENSORS; x++) {
184 sensorMin[x] = 9000;
185 sensorMax[x] = 0;
186 }
187}
188
189void calibrateLineSensor(uint8_t mode, uint32_t duration)
190{
191 uint32_t scanTime = millis() + duration;
192
193 lineMode = mode;
194 clearMinMax(sensorMinValues, sensorMaxValues);
195 do {
196 readLineSensor(sensorRawValues);
197 setSensorMinMax(sensorRawValues, sensorMinValues, sensorMaxValues);
198 } while (millis() < scanTime);
199}
200
201void readCalLineSensor(uint16_t *calVal)
202{
203 readLineSensor(sensorRawValues);
204 readCalLineSensor(sensorRawValues, calVal, sensorMinValues,
205 sensorMaxValues, lineMode);
206}
207
208void readCalLineSensor(uint16_t *sensorValues,
209 uint16_t *calVal,
210 uint16_t *sensorMinVal,
211 uint16_t *sensorMaxVal,
212 uint8_t mode)
213{
214 for (int x = 0; x < LS_NUM_SENSORS; x++) {
215 if (mode) {
216 calVal[x] = 0;
217 if (sensorValues[x] < sensorMinVal[x])
218 calVal[x] = map(sensorValues[x], 0, sensorMinVal[x], 1000, 0);
219 } else {
220 calVal[x] = 0;
221 if (sensorValues[x] > sensorMaxVal[x])
222 calVal[x] = map(sensorValues[x], sensorMaxVal[x], 2500, 0, 1000);
223 }
224 }
225}
226
228{
229 readCalLineSensor(calibrationValues);
230 return getLinePosition(calibrationValues, lineMode);
231}
232
233uint32_t getLinePosition(uint16_t *calVal, uint8_t mode)
234{
235 uint32_t avg = 0; // this is for the weighted total
236 uint32_t sum = 0; // this is for the denominator, which is <= 64000
237
238 uint32_t _lastPosition = 0;
239 for (uint8_t i = 1; i <= LS_NUM_SENSORS; i++) {
240 uint16_t value = calVal[i-1];
241
242 // only average in values that are above a noise threshold
243 if (value > 50) {
244 avg += (uint32_t)value * (i * 1000);
245 sum += value;
246 }
247 }
248
249 if (sum > 0)
250 _lastPosition = avg / sum;
251 return _lastPosition;
252}
253
254void setSensorMinMax(uint16_t *sensor, uint16_t *sensorMin, uint16_t *sensorMax)
255{
256 for (int x = 0; x < LS_NUM_SENSORS; x++) {
257 if (sensor[x] < sensorMin[x]) {
258 sensorMin[x] = sensor[x];
259 }
260 if (sensor[x] > sensorMax[x]) {
261 sensorMax[x] = sensor[x];
262 }
263 }
264}
265
266void setupWaitBtn(uint8_t btn)
267{
268 pinMode(btn, INPUT_PULLUP);
269}
270
271void setupLed(uint8_t ledPin)
272{
273 pinMode(ledPin, OUTPUT);
274}
275
276void waitBtnPressed(uint8_t btnPin, int8_t ledPin)
277{
278 uint8_t btnCnt = 0;
279 uint8_t pinVal = HIGH;
280
281 if (ledPin > MAX_PIN_NUMBER)
282 ledPin = 0;
283 /* Turn on led */
284 if (ledPin > 0)
285 digitalWrite(ledPin, pinVal);
286 while (digitalRead(btnPin) == 1) {
287 delay(25);
288 btnCnt++;
289 if (btnCnt == 40) {
290 btnCnt = 0;
291 if (ledPin > 0) {
292 pinVal = !pinVal;
293 digitalWrite(ledPin, pinVal);
294 }
295 }
296 }
297
298 pinVal = LOW;
299 if (ledPin > 0)
300 digitalWrite(ledPin, pinVal);
301
302 /* Wait for a short period to avoid button debounce */
303 delay(50);
304 while (digitalRead(btnPin) == 0)
305 ;
306
307 /* Wait for a short period to avoid button debounce */
308 delay(50);
309}
#define ENCODER_ERB_PIN
Value represents Energia number for Launchpad Pin P5.0.
Definition RSLK_Pins.h:194
#define MOTOR_L_PWM_PIN
Value represents Energia number for Launchpad Pin P2.7.
Definition RSLK_Pins.h:151
#define QTR_4
Value represents Energia number for Launchpad Pin P7.4.
Definition RSLK_Pins.h:82
#define ENCODER_ELB_PIN
Value represents Energia number for Launchpad Pin P5.2.
Definition RSLK_Pins.h:190
#define QTR_2
Value represents Energia number for Launchpad Pin P7.2.
Definition RSLK_Pins.h:74
#define QTR_7
Value represents Energia number for Launchpad Pin P7.7.
Definition RSLK_Pins.h:94
#define SHRP_DIST_R_PIN
Value represents Energia number for Launchpad Pin P9.0.
Definition RSLK_Pins.h:182
#define MOTOR_L_DIR_PIN
Value represents Energia number for Launchpad Pin P5.4.
Definition RSLK_Pins.h:146
#define SHRP_DIST_C_PIN
Value represents Energia number for Launchpad Pin P6.1.
Definition RSLK_Pins.h:178
#define QTR_1
Value represents Energia number for Launchpad Pin P7.1.
Definition RSLK_Pins.h:70
#define BP_SW_PIN_3
Value represents Energia number for Launchpad Pin P4.3.
Definition RSLK_Pins.h:120
#define SHRP_DIST_L_PIN
Value represents Energia number for Launchpad Pin P9.1.
Definition RSLK_Pins.h:174
#define ENCODER_ELA_PIN
Value represents Energia number for Launchpad Pin P10.5.
Definition RSLK_Pins.h:198
#define QTR_EMITTER_PIN_ODD
Value represents Energia number for Launchpad Pin P9.2.
Definition RSLK_Pins.h:102
#define ENCODER_ERA_PIN
Value represents Energia number for Launchpad Pin P10.4.
Definition RSLK_Pins.h:202
#define MOTOR_R_PWM_PIN
Value represents Energia number for Launchpad Pin P2.6.
Definition RSLK_Pins.h:165
#define QTR_6
Value represents Energia number for Launchpad Pin P7.6.
Definition RSLK_Pins.h:90
#define MOTOR_R_SLP_PIN
Value represents Energia number for Launchpad Pin P3.6.
Definition RSLK_Pins.h:156
#define BP_SW_PIN_2
Value represents Energia number for Launchpad Pin P4.5.
Definition RSLK_Pins.h:124
#define BP_SW_PIN_1
Value represents Energia number for Launchpad Pin P4.6.
Definition RSLK_Pins.h:128
#define BP_SW_PIN_5
Value represents Energia number for Launchpad Pin P4.0.
Definition RSLK_Pins.h:112
#define MOTOR_R_DIR_PIN
Value represents Energia number for Launchpad Pin P5.5.
Definition RSLK_Pins.h:160
#define QTR_5
Value represents Energia number for Launchpad Pin P7.5.
Definition RSLK_Pins.h:86
#define QTR_3
Value represents Energia number for Launchpad Pin P7.3.
Definition RSLK_Pins.h:78
#define BP_SW_PIN_0
Value represents Energia number for Launchpad Pin P4.7.
Definition RSLK_Pins.h:132
#define QTR_EMITTER_PIN_EVEN
Value represents Energia number for Launchpad Pin P5.3.
Definition RSLK_Pins.h:98
#define QTR_0
Value represents Energia number for Launchpad Pin P7.0.
Definition RSLK_Pins.h:66
#define BP_SW_PIN_4
Value represents Energia number for Launchpad Pin P4.2.
Definition RSLK_Pins.h:116
#define MOTOR_L_SLP_PIN
Value represents Energia number for Launchpad Pin P3.7.
Definition RSLK_Pins.h:141
Represents a single bump switch.
Definition Bump_Switch.h:13
bool begin(uint8_t pin_num, uint8_t mode=INPUT_PULLUP)
Initialize the bump switch class.
Class for Pololu's Sharp GP2Y0A21YK0F analog distance sensor.
int16_t readMM()
Read the distance from the Sharp distance sensor in millimeters.
bool begin(uint8_t pin_num, uint8_t mode=INPUT_PULLDOWN)
Initialize the distance sensor class.
float readIN()
Read the distance from the Sharp distance sensor in inches.
uint16_t read()
Read the value from distance sensor.
Represents a QTR sensor array.
Definition QTRSensors.h:85
void setTypeRC()
Specifies that the sensors are RC.
Definition QTRSensors.cpp:4
void setEmitterPins(uint8_t oddEmitterPin, uint8_t evenEmitterPin)
Sets separate odd and even emitter control pins for the sensors.
void read(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the raw sensor values into an array.
void setSensorPins(const uint8_t *pins, uint8_t sensorCount)
Sets the sensor pins.
Represents a single motor.
void disableMotor()
Disable motor.
void directionForward()
Set motor's direction to forward.
void pauseMotor()
Pause motor.
void resumeMotor()
Resume motor.
void setRawSpeed(uint8_t speed)
Set motor's raw speed.
void setSpeed(uint8_t speed)
Set motor speed.
bool begin(uint8_t islp_pin, uint8_t idir_pin, uint8_t ipwm_pin)
Initialize the motor and configures important pins.
void enableMotor()
Enable motor.
void directionBackward()
Set motor's direction to backwards.
#define DARK_LINE
Used to specify that the robot is running on a floor lighter than the line.
Definition SimpleRSLK.h:89
uint32_t getLinePosition()
Get line position.
#define TOTAL_BP_SW
Total number of bump switches.
Definition SimpleRSLK.h:44
#define DST_NUM_SENSORS
Total number of distance sensors.
Definition SimpleRSLK.h:24
void enableMotor(uint8_t motorNum)
Enable motor (take it out of sleep)
float readSharpDistIN(uint8_t num)
Read distance sensor value in inches.
void setMotorSpeed(uint8_t motorNum, uint8_t speed)
Set the motor speed.
bool isBumpSwitchPressed(uint8_t num)
Return bump switch status.
void setupRSLK()
Performs a variety of initialization needed for the RSLK.
void clearMinMax(uint16_t *sensorMin, uint16_t *sensorMax)
Provide default values for the sensor's Min and Max arrays.
#define readLineSensor
Read line sensor values.
Definition SimpleRSLK.h:335
void disableMotor(uint8_t motorNum)
Disable motor (puts the motor to sleep)
#define NUM_MOTORS
Total number of motors.
Definition SimpleRSLK.h:29
void setMotorDirection(uint8_t motorNum, uint8_t direction)
Set direction the motor will turn.
uint16_t readSharpDist(uint8_t num)
Read distance sensor value.
void readRawLineSensor(uint16_t *sensorValues)
Read raw line sensor values.
uint8_t getBumpSwitchPressed()
Return mask of bump switch states.
#define LEFT_MOTOR
Can be used to reference the left motor in the below functions.
Definition SimpleRSLK.h:49
void waitBtnPressed(uint8_t btnPin, int8_t ledPin)
Busy wait until user pushes and releases button.
void setupWaitBtn(uint8_t btn)
Configure pin as a wait to release button.
void pauseMotor(uint8_t motorNum)
Pause motor (put the motor to sleep while saving its speed)
void readCalLineSensor(uint16_t *calVal)
Read calibrated line sensor values. Assumes calibration completed.
void setupLed(uint8_t ledPin)
Configure pin that is connected to an led.
#define LS_NUM_SENSORS
Total number of sensors on QTR line sensor.
Definition SimpleRSLK.h:19
void calibrateLineSensor(uint8_t mode, uint32_t duration)
Calibrates line sensor.
#define RIGHT_MOTOR
Can be used to reference the right motor in the below functions.
Definition SimpleRSLK.h:54
#define BOTH_MOTORS
Can be used to reference both motors in the below functions.
Definition SimpleRSLK.h:59
void resumeMotor(uint8_t motorNum)
Resume motor (take the motor out of sleep and resumes its prior speed)
int16_t readSharpDistMM(uint8_t num)
Read distance sensor value in millimeters.
void setSensorMinMax(uint16_t *sensor, uint16_t *sensorMin, uint16_t *sensorMax)
Update line sensor's min and max values array based on current data.