Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. /**
  2. * @file robot.h
  3. * @brief Robot Class for the Arduino Mega 2650
  4. * @author David Such
  5. */
  6.  
  7. #ifndef robot_h
  8. #define robot_h
  9.  
  10. #include <Arduino.h>
  11.  
  12. class Robot
  13. {
  14. public:
  15. /*
  16. * @brief Class constructor.
  17. */
  18. Robot():
  19. leftMotor(pin_LW_Servo), rightMotor(pin_RW_Servo),
  20. frontSonar(pin_FrontPing_Sensor, pin_FrontPing_Sensor, MAX_DISTANCE),
  21. distanceAverage(MAX_DISTANCE), frontServo(pin_FrontPing_Servo),
  22. synthesizer(pin_EMIC_SerialRx, pin_EMIC_SerialTx), realTimeClock()
  23. {
  24.  
  25. }
  26.  
  27. /*
  28. * @brief Initialize the robot state.
  29. */
  30. void begin()
  31. {
  32. leftMotor.begin();
  33. rightMotor.begin();
  34. frontServo.begin();
  35. synthesizer.begin();
  36. compass.begin();
  37.  
  38. Wire.begin(); // Join I2C bus (address optional for master)
  39.  
  40. synthesizer.speakMessage("AEVA one, start up sequence in progress.");
  41.  
  42. delay(HOLD_OFF_TIME);
  43. startTime = millis();
  44. endTime = millis() + RUN_TIME * 1000;
  45.  
  46. frontServo.scan();
  47. move(AHEAD_SLOW);
  48. delay(2000);
  49.  
  50. sendMessage(address_I2C_Logging, "Master I2C bus is up.");
  51. synthesizer.speakMessage("Start up sequence complete. Awaiting command.");
  52.  
  53. if (realTimeClock.available()) {
  54. sendMessage(address_I2C_Logging, "RTC connected.");
  55. realTimeClock.begin();
  56. realTimeClock.updateMsgArrayWithTime(_msgArray);
  57. sendMessage(address_I2C_Logging, _msgArray);
  58. realTimeClock.updateMsgArrayWithDate(_msgArray);
  59. sendMessage(address_I2C_Logging, _msgArray);
  60. } else {
  61. sendMessage(address_I2C_Logging, "RTC unavailable.");
  62. }
  63.  
  64. if (compass.available()) {
  65. compass.updateMsgArrayWithHeading(_msgArray);
  66. sendMessage(address_I2C_Logging, _msgArray);
  67. } else {
  68. sendMessage(address_I2C_Logging, "Compass unavailable.");
  69. }
  70. }
  71.  
  72. /*
  73. * @brief Move left motor at speed (-500 to 500).
  74. */
  75. void moveLeft(int speed) {
  76. leftMotor.moveAtSpeed(speed);
  77. }
  78.  
  79. /*
  80. * @brief Move right motor at speed (-500 to 500).
  81. */
  82. void moveRight(int speed) {
  83. rightMotor.moveAtSpeed(speed);
  84. }
  85.  
  86. /*
  87. * @brief Move both motors at speed (-500 to 500).
  88. */
  89. void move(int speed) {
  90. leftMotor.moveAtSpeed(speed);
  91. rightMotor.moveAtSpeed(speed);
  92. state = stateMoving;
  93. }
  94.  
  95. /*
  96. * @brief Stop both motors.
  97. */
  98. void stop() {
  99. leftMotor.stop();
  100. rightMotor.stop();
  101. state = stateStopped;
  102. }
  103.  
  104. bool moving()
  105. {
  106. return (state == stateMoving);
  107. }
  108.  
  109. bool turning()
  110. {
  111. return (state == stateTurning);
  112. }
  113.  
  114. bool stopped()
  115. {
  116. return (state == stateStopped);
  117. }
  118.  
  119. bool doneRunning(unsigned long currentTime)
  120. {
  121. return (currentTime >= endTime);
  122. }
  123.  
  124. bool obstacleAhead(unsigned int distance)
  125. {
  126. return (distance <= MIN_DISTANCE);
  127. }
  128.  
  129. bool turn(unsigned long currentTime)
  130. {
  131. if (random(2) == 0) {
  132. moveLeft(BACK_SLOW);
  133. moveRight(AHEAD_SLOW);
  134. }
  135. else {
  136. moveLeft(AHEAD_SLOW);
  137. moveRight(BACK_SLOW);
  138. }
  139. state = stateTurning;
  140. endStateTime = currentTime + random(500, 1000);
  141. }
  142.  
  143. bool doneTurning(unsigned long currentTime, unsigned int distance)
  144. {
  145. if (currentTime >= endStateTime)
  146. return (distance > MIN_DISTANCE);
  147.  
  148. return false;
  149. }
  150.  
  151. /*
  152. * @brief Display text message on the logging OLED.
  153. * Maximum message length is 32 chars including terminating char.
  154. * Flash LED on pin 13 when sending message.
  155. */
  156. void sendMessage(byte address, char *msg)
  157. {
  158. digitalWrite(pin_LED, HIGH);
  159. Wire.beginTransmission(address);
  160. Wire.write(msg);
  161. Wire.endTransmission();
  162. digitalWrite(pin_LED, LOW);
  163. }
  164.  
  165. /*
  166. * @brief Update the state of the robot based on input from sensor and remote control.
  167. * Called repeatedly while the robot is in operation.
  168. */
  169. void run()
  170. {
  171. unsigned long currentTime = millis();
  172. unsigned long elapsedTime = currentTime - startTime;
  173.  
  174. if (stopped())
  175. return;
  176.  
  177. int distance = distanceAverage.add(frontSonar.getDistance());
  178.  
  179. log("state: %d, currentTime: %ul, ", state, currentTime);
  180. log("distance: %d\n", distance);
  181.  
  182. if (doneRunning(currentTime)) {
  183. stop();
  184. log("End of run time");
  185. }
  186. else if (moving()) {
  187. log("Moving, ");
  188. if (obstacleAhead(distance)) {
  189. turn(currentTime);
  190. log("Commencing turn, ");
  191. }
  192. }
  193. else if (turning()) {
  194. if (doneTurning(currentTime, distance)) {
  195. move(AHEAD_SLOW);
  196. log("Turn complete, ");
  197. }
  198. }
  199. }
  200.  
  201. private:
  202. HB25MotorControl leftMotor, rightMotor;
  203. Synthesizer synthesizer;
  204. DistanceSensor frontSonar;
  205. TurretServo frontServo;
  206. MovingAverage <unsigned int, 3> distanceAverage;
  207. RealTimeClock realTimeClock;
  208. Compass compass;
  209.  
  210. enum state_t { stateStopped, stateMoving, stateTurning };
  211. state_t state;
  212. unsigned long startTime, endTime, endStateTime;
  213. char _msgArray[32];
  214. };
  215.  
  216. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement