Advertisement
2018botball

Pombot v. 3.0

Mar 26th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. // Created on Wed March 25 2015
  2. // Pombot 2015; AKA; goldDIGGER; Written and Built by: The Freshman Team 2018
  3.  
  4. //sensor
  5. #define BUTTON 15
  6. //motors
  7. #define RIGHTWHEEL 3
  8. #define LEFTWHEEL 1
  9. #define SORTER 0
  10. //mini-servos
  11. #define SWITCH 0
  12. #define TAP 0
  13. //servos
  14. #define CLAW 2
  15. #define ARM 1
  16. //channels
  17. #define RED 0
  18. #define GREEN 1
  19.  
  20. //unneeded?
  21. const int SWITCH_FRONT = 1030;
  22. const int SWITCH_BACK = 1;
  23. //arm position
  24. const int ARM_UP = 2047;
  25. const int ARM_MID = 1320;
  26. const int ARM_DOWN = 200;
  27. //claw position
  28. const int CLAW_CLOSE = 900;
  29. const int CLAW_OPEN = 200;
  30. //tapper position
  31. const int TAP_UP = 1450;
  32. const int TAP_DOWN = 700;
  33. //motor position/speed
  34. const int MOTOR_POS_RED = 110;
  35. const int MOTOR_POS_GREEN = 125;
  36. const int MOTOR_SPEED = 135;
  37. //the mutipling factor
  38. const double factor = 0.92;
  39.  
  40. //prepping the robot
  41. void setup();
  42. //drive towards pompoms
  43. void drive_to_first_blob();
  44. //sorting our pompoms
  45. void sort();
  46. //updating the camera when we need it
  47. void camera_four();
  48. //setting positions of the arm and the claw
  49. void set_arm(int pos);
  50. void set_claw(int pos);
  51. //keeping the robot moving straight
  52. void drive_direct(int left_speed, int right_speed);
  53. //moving forward
  54. void drive(int speed, int millis);
  55. //stoping the goldDIGGER from its monetary advancements
  56. void stop();
  57. //stoping the robot when the claw touches a wall
  58. void drive_until_button(int speed);
  59. //raises arm slowly
  60. void set_arm_inc(int pos);
  61. //idk if we are using this but okay
  62. void shake();
  63. //collects all the pompoms in the claw
  64. void collect();
  65. //functions that make the pompoms in the funnel go to the sorter one by one
  66. void tap_funnel();
  67. //functions to sort each color
  68. void redsort();
  69. void greensort();
  70.  
  71. //our code
  72. int main()
  73. {
  74. printf("Pombot, the main goldDIGGER");
  75. setup();
  76. sort();
  77. return 0;
  78. }
  79.  
  80. void setup()
  81. {
  82. //turns on the servos
  83. enable_servo(ARM);
  84. enable_servo(CLAW);
  85. enable_servo(TAP);
  86. //clears all the motor counters
  87. clear_motor_position_counter(RIGHTWHEEL);
  88. clear_motor_position_counter(LEFTWHEEL);
  89. clear_motor_position_counter(SORTER);
  90. //prepares the arm/claw
  91. set_arm(ARM_DOWN);
  92. set_claw(CLAW_OPEN);
  93. //opens the camera
  94. camera_open_at_res(LOW_RES);
  95. }
  96.  
  97. void drive_to_first_blob()
  98. {
  99. //move forward
  100. drive(100, 3000);
  101. //turning slightly, keeps robot running straight
  102. drive_direct(-100, 100);
  103. msleep(700);
  104. //stop function
  105. stop();
  106. //moving backwards
  107. drive(-100, 1000);
  108. //until button
  109. drive_until_button(75);
  110. msleep(200);
  111. //collect function
  112. collect();
  113. }
  114.  
  115. void sort()
  116. {
  117. //creating an interger count
  118. int counter = 0;
  119. //infinite loop
  120. for(;;)
  121. {
  122. //if count is 3
  123. if(counter == 3){
  124. //taps funnel once regardless
  125. tap_funnel();
  126. //resets counter
  127. counter = 0;
  128. }
  129. //activating camera function
  130. camera_four();
  131. //sees red more than green and only sees red
  132. if(get_object_area(RED,0) > get_object_area(GREEN,0) && get_object_area(RED,0) > 100)
  133. {
  134. //perform red sorting
  135. redsort();
  136. msleep(100);
  137. }
  138. //sees green more than red and only sees green
  139. if(get_object_area(GREEN,0) > get_object_area(RED,0) && get_object_area(GREEN,0) > 20)
  140. {
  141. //perform green sorting
  142. greensort();
  143. msleep(100);
  144. }
  145. //error fix version 1
  146. //if it sees both red and green, then it gets rid of the pompom: AKA REDSORT
  147. if(get_object_area(RED,0) > 100 && get_object_area(GREEN,0) > 100)
  148.  
  149. {
  150. //red sort
  151. redsort();
  152. msleep(100);
  153. }
  154. msleep(10);
  155. //after one sorting pattern has been done, increase the counter by one
  156. counter++;
  157. }
  158. }
  159.  
  160. void redsort()
  161. {
  162. //turns the motor on, at a specfic speed to a certain position
  163. mtp(0, MOTOR_SPEED, -1 * MOTOR_POS_RED);
  164. //stops motor from moving at all in its spot
  165. block_motor_done(SORTER);
  166. //clears the motor counter
  167. clear_motor_position_counter(SORTER);
  168. //turns the motor on, at a specfic speed to the opp. position of the first
  169. mtp(0, MOTOR_SPEED, MOTOR_POS_RED);
  170. //stops motor from moving at all in its spot
  171. block_motor_done(SORTER);
  172. //clears the motor counter
  173. clear_motor_position_counter(SORTER);
  174. }
  175.  
  176. void greensort()
  177. {
  178. //turns the motor on, at a specfic speed to a certain position
  179. //change k
  180. mtp(0,MOTOR_SPEED, MOTOR_POS_GREEN);
  181. //stops motor from moving at all in its spot
  182. block_motor_done(SORTER);
  183. //clears the motor counter
  184. clear_motor_position_counter(SORTER);
  185. //turns the motor on, at a specfic speed to the opp. position of the first
  186. mtp(0,MOTOR_SPEED,-1 * MOTOR_POS_GREEN);
  187. //stops motor from moving at all in its spot
  188. block_motor_done(SORTER);
  189. //clears the motor counter
  190. clear_motor_position_counter(SORTER);
  191. }
  192.  
  193. void camera_four()
  194. {
  195. //update the camera 4 times when it's used
  196. camera_update();
  197. camera_update();
  198. camera_update();
  199. camera_update();
  200. }
  201.  
  202. void set_arm(int pos)
  203. {
  204. //setting the arm pos.
  205. set_servo_position(ARM, pos);
  206. }
  207.  
  208. void set_claw(int pos)
  209. {
  210. //setting the claw pos.
  211. set_servo_position(CLAW, pos);
  212. }
  213.  
  214. void set_tapper(int pos)
  215. {
  216. //setting the tapper position
  217. set_servo_position(TAP, pos);
  218. }
  219.  
  220. void drive_direct(int left_speed, int right_speed) {
  221. //stablizing the movement of the robot
  222. motor(RIGHTWHEEL, right_speed);
  223. motor(LEFTWHEEL, factor * left_speed);
  224. }
  225.  
  226. void drive(int speed, int millis)
  227. {
  228. //driving
  229. drive_direct(speed, speed);
  230. msleep(millis);
  231. stop();
  232. }
  233.  
  234. void stop()
  235. {
  236. //stopping the bot
  237. drive_direct(0,0);
  238. }
  239.  
  240. void drive_until_button(int speed)
  241. {
  242. //moving until button is pressed
  243. drive_direct(speed, speed);
  244. while( digital(BUTTON) == 0 ){
  245. }
  246. stop();
  247. }
  248.  
  249. void shake()
  250. {
  251. //Unknown Potentially Used/Unused
  252. set_claw(CLAW_OPEN);
  253. msleep(2000);
  254. int i;
  255. for( i = 0; i < 5; i++ ){
  256. set_claw(CLAW_CLOSE);
  257. msleep(500);
  258. set_claw(CLAW_OPEN);
  259. msleep(500);
  260. }
  261. }
  262. void set_arm_inc(int pos)
  263. {
  264. //moving the arm slowly
  265. const int incre = 30;
  266. int nDiff = (get_servo_position(ARM) - pos) / incre;
  267. int i = incre;
  268. for(; i>=0; i--)
  269. {
  270. set_arm(pos + i * nDiff);
  271. msleep(100);
  272. }
  273. }
  274. void collect()
  275. {
  276. //collecting the pompoms in the claw
  277. set_claw(CLAW_OPEN);
  278. drive_until_button(200);
  279. set_claw(CLAW_CLOSE);
  280. msleep(500);
  281. drive(-100, 500);
  282. set_arm_inc(ARM_UP);
  283. msleep(500);
  284. shake();
  285. }
  286. void tap_funnel()
  287. {
  288. //sorting the pompoms 1by1 within the funnel
  289. set_tapper(TAP_UP);
  290. msleep(200);
  291. set_tapper(TAP_DOWN);
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement