TI RSLK Library  0.2.0
SimpleRSLK.cpp
1 #include "SimpleRSLK.h"
2 
3 GP2Y0A21_Sensor dst_sensor[3];
4 Romi_Motor_Power motor[2];
5 Bump_Switch bump_sw[6];
6 uint16_t calMin[LS_NUM_SENSORS], calMax[LS_NUM_SENSORS];
7 
8 QTRSensors qtr;
9 
10 void setupRSLK() {
13 
15 
16  bump_sw[0].begin(BP_SW_PIN_0, INPUT_PULLUP);
17  bump_sw[1].begin(BP_SW_PIN_1, INPUT_PULLUP);
18  bump_sw[2].begin(BP_SW_PIN_2, INPUT_PULLUP);
19  bump_sw[3].begin(BP_SW_PIN_3, INPUT_PULLUP);
20  bump_sw[4].begin(BP_SW_PIN_4, INPUT_PULLUP);
21  bump_sw[5].begin(BP_SW_PIN_5, INPUT_PULLUP);
22 
23  dst_sensor[0].begin(SHRP_DIST_L_PIN,INPUT_PULLDOWN);
24  dst_sensor[1].begin(SHRP_DIST_C_PIN,INPUT_PULLDOWN);
25  dst_sensor[2].begin(SHRP_DIST_R_PIN,INPUT_PULLDOWN);
26 
27  qtr.setTypeRC();
28  qtr.setSensorPins((const uint8_t[]){QTR_7, QTR_6, QTR_5, QTR_4, QTR_3, QTR_2, QTR_1, QTR_0}, LS_NUM_SENSORS);
31 
32  for (uint8_t x = 0; x < LS_NUM_SENSORS; x++)
33  {
34  calMin[x] = 5000;
35  calMax[x] = 0;
36  }
37 
38 }
39 
40 uint16_t readSharpDist(uint8_t num) {
41  if(num < 0 || num > 3)
42  return 0;
43 
44  return dst_sensor[num].read();
45 }
46 
47 bool isBumpSwitchPressed(uint8_t num) {
48  if(num < 0 || num > 5)
49  return false;
50 
51  if(bump_sw[num].read() == 0) {
52  return true;
53  }
54  else
55  return false;
56 }
57 void enableMotor(uint8_t motorNum) {
58  if(motorNum == 0 || motorNum == 2)
59  {
60  motor[0].enableMotor();
61  }
62 
63  if(motorNum == 1 || motorNum == 2)
64  {
65  motor[1].enableMotor();
66  }
67 
68 }
69 
70 
71 void disableMotor(uint8_t motorNum) {
72  if(motorNum == 0 || motorNum == 2)
73  {
74  motor[0].disableMotor();
75  }
76 
77  if(motorNum == 1 || motorNum == 2)
78  {
79  motor[1].disableMotor();
80  }
81 
82 }
83 
84 void pauseMotor(uint8_t motorNum) {
85  if(motorNum == 0 || motorNum == 2)
86  {
87  motor[0].pauseMotor();
88  }
89 
90  if(motorNum == 1 || motorNum == 2)
91  {
92  motor[1].pauseMotor();
93  }
94 }
95 
96 void resumeMotor(uint8_t motorNum) {
97  if(motorNum == 0 || motorNum == 2)
98  {
99  motor[0].resumeMotor();
100  }
101 
102  if(motorNum == 1 || motorNum == 2)
103  {
104  motor[1].resumeMotor();
105  }
106 }
107 
108 void setMotorDirection(uint8_t motorNum,uint8_t direction) {
109 
110  if(motorNum == 0 || motorNum == 2)
111  {
112  if(direction == 0) {
113  motor[0].directionForward();
114  } else if(direction == 1) {
115  motor[0].directionBackward();
116  }
117  }
118 
119  if(motorNum == 1 || motorNum == 2)
120  {
121  if(direction == 0) {
122  motor[1].directionForward();
123  } else if(direction == 1) {
124  motor[1].directionBackward();
125  }
126  }
127 }
128 
129 void setMotorSpeed(uint8_t motorNum, uint8_t speed) {
130  if(motorNum == 0 || motorNum == 2)
131  {
132  motor[0].setSpeed(speed);
133  }
134 
135  if(motorNum == 1 || motorNum == 2)
136  {
137  motor[1].setSpeed(speed);
138  }
139 }
140 
141 void setRawMotorSpeed(uint8_t motorNum, uint8_t speed) {
142  if(motorNum == 0 || motorNum == 2)
143  {
144  motor[0].setRawSpeed(speed);
145  }
146 
147  if(motorNum == 1 || motorNum == 2)
148  {
149  motor[1].setRawSpeed(speed);
150  }
151 }
152 
153 void readLineSensor(uint16_t* sensorValues) {
154  qtr.read(sensorValues,QTRReadMode::OddEven);
155 }
156 
157 void clearMinMax(uint16_t *sensorMin,uint16_t *sensorMax) {
158  for(int x = 0;x<LS_NUM_SENSORS;x++)
159  {
160  sensorMin[x] = 9000;
161  sensorMax[x] = 0;
162  }
163 }
164 
165 
166 void readCalLineSensor(uint16_t* sensorValues,
167  uint16_t* calVal,
168  uint16_t *sensorMinVal,
169  uint16_t *sensorMaxVal,
170  uint8_t mode)
171 {
172  for(int x = 0;x<LS_NUM_SENSORS;x++)
173  {
174  if(mode)
175  {
176  calVal[x] = 0;
177  if(sensorValues[x] > sensorMaxVal[x])
178  calVal[x] = map(sensorValues[x],sensorMaxVal[x],2500,0,1000);
179  } else {
180  calVal[x] = 0;
181  if(sensorValues[x] < sensorMinVal[x])
182  calVal[x] = map(sensorValues[x],0,sensorMinVal[x],1000,0);
183  }
184  }
185 }
186 
187 uint32_t getLinePosition(uint16_t* calVal, uint8_t mode)
188 {
189 
190  uint32_t avg = 0; // this is for the weighted total
191  uint32_t sum = 0; // this is for the denominator, which is <= 64000
192 
193  uint32_t _lastPosition;
194  for (uint8_t i = 0; i < LS_NUM_SENSORS; i++)
195  {
196  uint16_t value = calVal[i];
197 
198  // only average in values that are above a noise threshold
199  if (value > 50)
200  {
201  avg += (uint32_t)value * (i * 1000);
202  sum += value;
203  }
204  }
205 
206  _lastPosition = avg / sum;
207  return _lastPosition;
208 }
209 
210 void setSensorMinMax(uint16_t *sensor,uint16_t *sensorMin,uint16_t *sensorMax) {
211  for(int x = 0;x<LS_NUM_SENSORS;x++)
212  {
213  if(sensor[x] < sensorMin[x])
214  {
215  sensorMin[x] = sensor[x];
216  }
217  if(sensor[x] > sensorMax[x])
218  {
219  sensorMax[x] = sensor[x];
220  }
221  }
222 }
223 
224 void setupWaitBtn(uint8_t btn) {
225  pinMode(btn, INPUT_PULLUP);
226 }
227 
228 void waitBtnPressed(uint8_t btn) {
229  while(digitalRead(btn) == 1);
230  Serial.println("Button pressed");
231  delay(50);
232  while(digitalRead(btn) == 0);
233  delay(50);
234 }
setMotorDirection
void setMotorDirection(uint8_t motorNum, uint8_t direction)
Set direction the motor will turn.
Definition: SimpleRSLK.cpp:108
BP_SW_PIN_5
#define BP_SW_PIN_5
Value represents Energia number for Launchpad Pin P4.7.
Definition: RSLK_Pins.h:132
QTRSensors
Represents a QTR sensor array.
Definition: QTRSensors.h:81
Romi_Motor_Power::directionForward
void directionForward()
Set motor's direction to forward.
Definition: Romi_Motor_Power.cpp:33
Romi_Motor_Power::setSpeed
void setSpeed(uint8_t speed)
Set motor speed.
Definition: Romi_Motor_Power.cpp:50
ENCODER_ERB_PIN
#define ENCODER_ERB_PIN
Value represents Energia number for Launchpad Pin P5.0.
Definition: RSLK_Pins.h:194
Romi_Motor_Power::enableMotor
void enableMotor()
Enable motor.
Definition: Romi_Motor_Power.cpp:29
QTR_0
#define QTR_0
Value represents Energia number for Launchpad Pin P7.0.
Definition: RSLK_Pins.h:65
QTRSensors::setEmitterPins
void setEmitterPins(uint8_t oddEmitterPin, uint8_t evenEmitterPin)
Sets separate odd and even emitter control pins for the sensors.
Definition: QTRSensors.cpp:66
readSharpDist
uint16_t readSharpDist(uint8_t num)
Read distance sensor value.
Definition: SimpleRSLK.cpp:40
isBumpSwitchPressed
bool isBumpSwitchPressed(uint8_t num)
Return bump switch status.
Definition: SimpleRSLK.cpp:47
Romi_Motor_Power::resumeMotor
void resumeMotor()
Resume motor.
Definition: Romi_Motor_Power.cpp:45
QTR_EMITTER_PIN_ODD
#define QTR_EMITTER_PIN_ODD
Value represents Energia number for Launchpad Pin P9.2.
Definition: RSLK_Pins.h:101
QTR_2
#define QTR_2
Value represents Energia number for Launchpad Pin P7.2.
Definition: RSLK_Pins.h:73
GP2Y0A21_Sensor::begin
bool begin(uint8_t pin_num, uint8_t mode=INPUT_PULLDOWN)
Initialize the distance sensor class.
Definition: GP2Y0A21_Sensor.cpp:9
QTR_EMITTER_PIN_EVEN
#define QTR_EMITTER_PIN_EVEN
Value represents Energia number for Launchpad Pin P5.3.
Definition: RSLK_Pins.h:97
Bump_Switch::begin
bool begin(uint8_t pin_num, uint8_t mode=INPUT_PULLUP)
Initialize the bump switch class.
Definition: Bump_Switch.cpp:10
SHRP_DIST_L_PIN
#define SHRP_DIST_L_PIN
Value represents Energia number for Launchpad Pin P9.1.
Definition: RSLK_Pins.h:174
MOTOR_R_SLP_PIN
#define MOTOR_R_SLP_PIN
Value represents Energia number for Launchpad Pin P3.6.
Definition: RSLK_Pins.h:156
QTRSensors::setSensorPins
void setSensorPins(const uint8_t *pins, uint8_t sensorCount)
Sets the sensor pins.
Definition: QTRSensors.cpp:16
QTR_6
#define QTR_6
Value represents Energia number for Launchpad Pin P7.6.
Definition: RSLK_Pins.h:89
QTRSensors::setTypeRC
void setTypeRC()
Specifies that the sensors are RC.
Definition: QTRSensors.cpp:4
ENCODER_ELB_PIN
#define ENCODER_ELB_PIN
Value represents Energia number for Launchpad Pin P5.2.
Definition: RSLK_Pins.h:190
readLineSensor
void readLineSensor(uint16_t *sensor)
Read line sensor values.
Definition: SimpleRSLK.cpp:153
clearMinMax
void clearMinMax(uint16_t *sensorMin, uint16_t *sensorMax)
Provide default values for the sensor's Min and Max arrays.
Definition: SimpleRSLK.cpp:157
QTR_7
#define QTR_7
Value represents Energia number for Launchpad Pin P7.7.
Definition: RSLK_Pins.h:93
MOTOR_L_SLP_PIN
#define MOTOR_L_SLP_PIN
Value represents Energia number for Launchpad Pin P3.7.
Definition: RSLK_Pins.h:141
GP2Y0A21_Sensor
Class for Pololu's Sharp GP2Y0A21YK0F analog distance sensor.
Definition: GP2Y0A21_Sensor.h:10
getLinePosition
uint32_t getLinePosition(uint16_t *calVal, uint8_t mode)
Get line position.
Definition: SimpleRSLK.cpp:187
ENCODER_ERA_PIN
#define ENCODER_ERA_PIN
Value represents Energia number for Launchpad Pin P10.4.
Definition: RSLK_Pins.h:202
SHRP_DIST_R_PIN
#define SHRP_DIST_R_PIN
Value represents Energia number for Launchpad Pin P9.0.
Definition: RSLK_Pins.h:182
BP_SW_PIN_2
#define BP_SW_PIN_2
Value represents Energia number for Launchpad Pin P4.3.
Definition: RSLK_Pins.h:119
Romi_Motor_Power::begin
bool begin(uint8_t islp_pin, uint8_t idir_pin, uint8_t ipwm_pin)
Initialize the motor and configures important pins.
Definition: Romi_Motor_Power.cpp:12
BP_SW_PIN_4
#define BP_SW_PIN_4
Value represents Energia number for Launchpad Pin P4.6.
Definition: RSLK_Pins.h:127
Bump_Switch
Represents a single bump switch.
Definition: Bump_Switch.h:12
setupEncoder
void setupEncoder(uint8_t ela_pin, uint8_t elb_pin, uint8_t era_pin, uint8_t erb_pin)
Initialize the wheel encoder.
Definition: Encoder.cpp:32
setupRSLK
void setupRSLK()
Performs a variety of initialization needed for the RSLK.
Definition: SimpleRSLK.cpp:10
Romi_Motor_Power::directionBackward
void directionBackward()
Set motor's direction to backwards.
Definition: Romi_Motor_Power.cpp:37
MOTOR_L_DIR_PIN
#define MOTOR_L_DIR_PIN
Value represents Energia number for Launchpad Pin P5.4.
Definition: RSLK_Pins.h:146
disableMotor
void disableMotor(uint8_t motorNum)
Disable motor (puts the motor to sleep)
Definition: SimpleRSLK.cpp:71
setMotorSpeed
void setMotorSpeed(uint8_t motorNum, uint8_t speed)
Set the motor speed.
Definition: SimpleRSLK.cpp:129
setupWaitBtn
void setupWaitBtn(uint8_t btn)
Configure pin as a wait to release button.
Definition: SimpleRSLK.cpp:224
MOTOR_R_PWM_PIN
#define MOTOR_R_PWM_PIN
Value represents Energia number for Launchpad Pin P2.6.
Definition: RSLK_Pins.h:165
QTR_4
#define QTR_4
Value represents Energia number for Launchpad Pin P7.4.
Definition: RSLK_Pins.h:81
resumeMotor
void resumeMotor(uint8_t motorNum)
Resume motor (take the motor out of sleep and resumes its prior speed)
Definition: SimpleRSLK.cpp:96
GP2Y0A21_Sensor::read
uint16_t read()
Read the value from distance sensor.
Definition: GP2Y0A21_Sensor.cpp:16
setSensorMinMax
void setSensorMinMax(uint16_t *sensor, uint16_t *sensorMin, uint16_t *sensorMax)
Update line sensor's min and max values array based on current data.
Definition: SimpleRSLK.cpp:210
BP_SW_PIN_0
#define BP_SW_PIN_0
Value represents Energia number for Launchpad Pin P4.0.
Definition: RSLK_Pins.h:111
Romi_Motor_Power::pauseMotor
void pauseMotor()
Pause motor.
Definition: Romi_Motor_Power.cpp:41
BP_SW_PIN_3
#define BP_SW_PIN_3
Value represents Energia number for Launchpad Pin P4.5.
Definition: RSLK_Pins.h:123
Romi_Motor_Power
Represents a single motor.
Definition: Romi_Motor_Power.h:15
QTR_1
#define QTR_1
Value represents Energia number for Launchpad Pin P7.1.
Definition: RSLK_Pins.h:69
QTRSensors::read
void read(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the raw sensor values into an array.
Definition: QTRSensors.cpp:407
Romi_Motor_Power::disableMotor
void disableMotor()
Disable motor.
Definition: Romi_Motor_Power.cpp:24
ENCODER_ELA_PIN
#define ENCODER_ELA_PIN
Value represents Energia number for Launchpad Pin P10.5.
Definition: RSLK_Pins.h:198
QTR_3
#define QTR_3
Value represents Energia number for Launchpad Pin P7.3.
Definition: RSLK_Pins.h:77
MOTOR_R_DIR_PIN
#define MOTOR_R_DIR_PIN
Value represents Energia number for Launchpad Pin P5.5.
Definition: RSLK_Pins.h:160
waitBtnPressed
void waitBtnPressed(uint8_t btn)
Busy wait until user pushes and releases button.
Definition: SimpleRSLK.cpp:228
Romi_Motor_Power::setRawSpeed
void setRawSpeed(uint8_t speed)
Set motor's raw speed.
Definition: Romi_Motor_Power.cpp:55
BP_SW_PIN_1
#define BP_SW_PIN_1
Value represents Energia number for Launchpad Pin P4.2.
Definition: RSLK_Pins.h:115
SHRP_DIST_C_PIN
#define SHRP_DIST_C_PIN
Value represents Energia number for Launchpad Pin P6.1.
Definition: RSLK_Pins.h:178
pauseMotor
void pauseMotor(uint8_t motorNum)
Pause motor (put the motor to sleep while saving its speed)
Definition: SimpleRSLK.cpp:84
SimpleRSLK.h
LS_NUM_SENSORS
#define LS_NUM_SENSORS
Total number of sensors on QTR line sensor.
Definition: SimpleRSLK.h:16
QTRReadMode::OddEven
readCalLineSensor
void readCalLineSensor(uint16_t *sensor, uint16_t *calVal, uint16_t *sensorMin, uint16_t *sensorMax, uint8_t mode)
Update sensor's min and max values array based on current data.
Definition: SimpleRSLK.cpp:166
QTR_5
#define QTR_5
Value represents Energia number for Launchpad Pin P7.5.
Definition: RSLK_Pins.h:85
BOTH_MOTORS
#define BOTH_MOTORS
Can be used to reference both motors in the below functions.
Definition: SimpleRSLK.h:33
enableMotor
void enableMotor(uint8_t motorNum)
Enable motor (take it out of sleep)
Definition: SimpleRSLK.cpp:57
MOTOR_L_PWM_PIN
#define MOTOR_L_PWM_PIN
Value represents Energia number for Launchpad Pin P2.7.
Definition: RSLK_Pins.h:151