Advertisement
Guest User

Appendix

a guest
Mar 31st, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ########### ABOUT THE CODE #############
  2. This is the modified, HARD CODED, script that ultimately performed in the demonstration.
  3.  
  4. It has several features:
  5.   1.) An On/Off switch, for easy handling
  6.   2.) LED lights, to determine the state of the robot. If it is on, the LED is on. If the robot is waiting, LEDs with flash
  7.   3.) Variable speeds when the robot is at varying distance from the wall
  8.   4.) Motor driver support.
  9.   5.) A waving flag at the end. It only waves once per press of the Off/On button.
  10.  
  11. This code executes flawlessly and meets all requirements of the test, completing it in 1min 00sec 10ms.
  12.  
  13. The author of this code is not pleased with the HARD CODED nature of this script
  14. He has thus included the equally valid script below this attached.
  15.  
  16. */
  17.  
  18. // ########### CONSTANTS AND VARIABLES HERE #############
  19.  
  20. #include <Servo.h>
  21.  
  22. Servo madFlexer;
  23.  
  24. const int flex = 7;
  25. int flexNumber = 0;
  26.  
  27. const int trig = 10;
  28. const int echo = 11;
  29.  
  30. const int motorL1 = 3; // Opposite Terminals - Red and Black wires soldered in wrong sense
  31. const int motorL2 = 6;
  32. const int motorR1 = 5;
  33. const int motorR2 = 9;
  34.  
  35. long duration;
  36. float distance;
  37. const int Error = 3;
  38.  
  39.  
  40. bool onOff = false;
  41. const int onSwitch = 13;
  42. const int offSwitch = 12;
  43.  
  44. const int LED = 4;
  45.  
  46.  
  47. long timeStart;
  48.  
  49.  
  50. // ################# FUNCTIONS BELOW ####################
  51.  
  52. int measureDistance(int trig, int echo){
  53.   digitalWrite(trig, LOW);
  54.   delayMicroseconds(2); // clearing the trigger pin
  55.   digitalWrite(trig, HIGH); // sending the pulse
  56.   delayMicroseconds(10); // trig on high for at least 10 us
  57.   digitalWrite(trig, LOW);
  58.   return (pulseIn(echo, HIGH)/2) * 0.0343; // recieving and calulating distance
  59. }
  60.  
  61. // To compensate for differnces in power output for each motor, motors L1 and R1 have been scaled to 90% power
  62.  
  63. void fullSpeed(){
  64.   analogWrite(motorL1, 230);
  65.   analogWrite(motorL2, 255);
  66.   analogWrite(motorR1, 230);
  67.   analogWrite(motorR2, 255);
  68. }
  69.  
  70. void halfSpeed(){
  71.   analogWrite(motorL1, 144);
  72.   analogWrite(motorL2, 127);
  73.   analogWrite(motorR1, 144);
  74.   analogWrite(motorR2, 127);
  75. }
  76.  
  77. void quarterSpeed(){
  78.   analogWrite(motorL1, 64);
  79.   analogWrite(motorL2, 64);
  80.   analogWrite(motorR1, 64);
  81.   analogWrite(motorR2, 64);
  82. }
  83.  
  84. void fullStop(){
  85.   analogWrite(motorL1, 0);
  86.   analogWrite(motorL2, 0);
  87.   analogWrite(motorR1, 0);
  88.   analogWrite(motorR2, 0);
  89. }
  90.  
  91. void madFlex(int flexState){    // This function waves the flag.
  92.   if (flexState <2){
  93.       for (int waves = 0; waves<= 3; waves += 1){
  94.         madFlexer.write(110);
  95.         delay(250);
  96.         madFlexer.write(80);
  97.         delay(250);
  98.       }
  99.       madFlexer.write(180);
  100.       flexNumber += 1;
  101.   }
  102.   else{
  103.     madFlexer.write(180);
  104.   }
  105. }
  106.  
  107. // ################# CODE BEGINS BELOW ###################
  108.  
  109. void setup() {
  110.   pinMode(trig, OUTPUT);
  111.   pinMode(echo, INPUT); // sets ultrasound pins to input and output
  112.   pinMode(motorL1, OUTPUT);
  113.   pinMode(motorL2, OUTPUT);
  114.   pinMode(motorR1, OUTPUT);
  115.   pinMode(motorR2, OUTPUT);
  116.   pinMode(onSwitch, INPUT);
  117.   pinMode(offSwitch, INPUT);
  118.   madFlexer.attach(flex);
  119.   pinMode(LED, OUTPUT);
  120.   Serial.begin(9600);
  121. }
  122.  
  123. void loop() {
  124.   // As we only had 2 momentary switches, we had to make do with this code
  125.   if (digitalRead(onSwitch) == HIGH){   // If the on switch is pressed, it turns on the LED and changes the onOff state
  126.     onOff = true;
  127.     digitalWrite(LED, HIGH);
  128.     timeStart = millis();
  129.   }
  130.   if (digitalRead(offSwitch) == HIGH){  // Similarly, if the off switch is pressed, the opposite happens. It also resets the flex number
  131.     onOff = false;
  132.     digitalWrite(LED, LOW);
  133.     flexNumber = 0;
  134.   }
  135.  
  136.   distance = measureDistance(trig, echo);
  137.   Serial.println("Distance:");
  138.   Serial.println(distance);
  139.   Serial.println(onOff);
  140.   Serial.println("Time: ");
  141.   Serial.println(millis() - timeStart);
  142.  
  143.  
  144.   if (onOff) {    // If the on has been pushed, execute the following.
  145.    
  146.     if (not flexNumber){    // The variable flexNumber ensures this segment is only executed ONCE
  147.       fullSpeed();
  148.       flexNumber += 1;
  149.       delay(3000);    // This makes the robot charge forward for 3 seconds, stopping somewhere in the middle of the course
  150.       fullStop();
  151.       for (int i = 0; i <= 112; i += 1){    // Each iteration takes 0.5 seconds. This is a 56 second countdown timer.
  152.         digitalWrite(LED, LOW);
  153.         delay(250);
  154.         digitalWrite(LED, HIGH);
  155.         delay(250);
  156.         Serial.println("Waiting...");
  157.         }
  158.     }
  159.  
  160.     // This leaves the robot with 60 - 56 - 3 = 1 second to complete the remainder of the course.
  161.    
  162.     if (distance <= (15 + Error)){    // As the robot stops after rolling down a slope, a 10cm braking distance is factored
  163.       fullStop();
  164.       if (millis() - timeStart >= 59900) {
  165.         madFlex(flexNumber);
  166.       }
  167.     }
  168.    
  169.     if (distance > (15 + Error)){     // Reduce speed as approaching wall
  170.       halfSpeed();
  171.     }
  172.    
  173.     if (distance >= (20 + Error)){
  174.       fullSpeed();
  175.     }
  176.   }
  177.  
  178.   else{
  179.     fullStop();
  180.   }
  181. }
  182.  
  183. ####################    ORIGINAL SCRIPT BELOW    #######################
  184.  
  185.  
  186. /* ########### ABOUT THE CODE #############
  187. This piece of code is the original.
  188.  
  189. It has several features:
  190.   1.) An On/Off switch, for easy handling
  191.   2.) LED lights, to determine the state of the robot. If it is on, the LED is on. If the robot is waiting, LEDs with flash
  192.   3.) Variable speeds when the robot is at varying distance from the wall
  193.   4.) Motor driver support.
  194.   5.) A waving flag at the end. It only waves once per press of the Off/On button.
  195.  
  196. This code executes flawlessly and meets all requirements of the test.
  197.  
  198. However, it was not clear to an outsider as to whether the robot has completed the course early or if it just waiting as it waits near the wall.
  199. Hence a modified, HARD CODED, version of this script, included above, was designed to stop and wait in the middle of the course.
  200.  
  201. */
  202.  
  203. // ########### CONSTANTS AND VARIABLES HERE #############
  204.  
  205. #include <Servo.h>
  206.  
  207. Servo madFlexer;
  208.  
  209. const int flex = 7;
  210. int flexNumber = 0;
  211.  
  212. const int trig = 10; // trig on high for at least 10 us
  213. const int echo = 11;
  214.  
  215. const int motorL1 = 3; // Opposite Terminals
  216. const int motorL2 = 6;
  217. const int motorR1 = 5;
  218. const int motorR2 = 9;
  219.  
  220. long duration;
  221. float distance;
  222. const int Error = 2;
  223.  
  224.  
  225. bool onOff = false;
  226. const int onSwitch = 13;
  227. const int offSwitch = 12;
  228.  
  229. const int LED = 4;
  230.  
  231. long timeStart;
  232.  
  233.  
  234. // ################# FUNCTIONS BELOW ####################
  235.  
  236. int measureDistance(int trig, int echo){
  237.   digitalWrite(trig, LOW);
  238.   delayMicroseconds(2); // clearing the trigger pin
  239.   digitalWrite(trig, HIGH); // sending the pulse
  240.   delayMicroseconds(10);
  241.   digitalWrite(trig, LOW);
  242.   return (pulseIn(echo, HIGH)/2) * 0.0343; // recieving and calulating distance
  243. }
  244.  
  245. // To compensate for differnces in power output for each motor, motors L1 and R1 have been scaled to 90% power
  246.  
  247. void fullSpeed(){
  248.   analogWrite(motorL1, 230);
  249.   analogWrite(motorL2, 255);
  250.   analogWrite(motorR1, 230);
  251.   analogWrite(motorR2, 255);
  252. }
  253.  
  254. void halfSpeed(){
  255.   analogWrite(motorL1, 144);
  256.   analogWrite(motorL2, 127);
  257.   analogWrite(motorR1, 144);
  258.   analogWrite(motorR2, 127);
  259. }
  260.  
  261. void quarterSpeed(){
  262.   analogWrite(motorL1, 64);
  263.   analogWrite(motorL2, 64);
  264.   analogWrite(motorR1, 64);
  265.   analogWrite(motorR2, 64);
  266. }
  267.  
  268. void fullStop(){
  269.   analogWrite(motorL1, 0);
  270.   analogWrite(motorL2, 0);
  271.   analogWrite(motorR1, 0);
  272.   analogWrite(motorR2, 0);
  273. }
  274.  
  275. void madFlex(int flexState){
  276.   if (not flexState){
  277.       for (int waves = 0; waves<= 3; waves += 1){
  278.         madFlexer.write(80);
  279.         delay(250);
  280.         madFlexer.write(110);
  281.         delay(250);
  282.       }
  283.       madFlexer.write(180);
  284.       flexNumber += 1;
  285.   }
  286.   else{
  287.     madFlexer.write(0);
  288.   }
  289. }
  290.  
  291. // ################# CODE BEGINS BELOW ###################
  292.  
  293. void setup() {
  294.   pinMode(trig, OUTPUT);
  295.   pinMode(echo, INPUT); // sets ultrasound pins to input and output
  296.   pinMode(motorL1, OUTPUT);
  297.   pinMode(motorL2, OUTPUT);
  298.   pinMode(motorR1, OUTPUT);
  299.   pinMode(motorR2, OUTPUT);
  300.   pinMode(onSwitch, INPUT);
  301.   pinMode(offSwitch, INPUT);
  302.   pinMode(LED, OUTPUT);
  303.  
  304.   madFlexer.attach(7);
  305.  
  306.   Serial.begin(9600);
  307. }
  308.  
  309. void loop() {
  310.  
  311.   if (digitalRead(onSwitch) == HIGH){
  312.     onOff = true;
  313.     digitalWrite(LED, HIGH);
  314.     timeStart = millis();
  315.   }
  316.   if (digitalRead(offSwitch) == HIGH){
  317.     onOff = false;
  318.     digitalWrite(LED, LOW);
  319.     flexNumber = 0;
  320.     madFlexer.write(180);
  321.   }
  322.  
  323.   distance = measureDistance(trig, echo);
  324.  
  325.   Serial.println("Distance: ");
  326.   Serial.println(distance);
  327.   Serial.println(onOff);
  328.   Serial.println("Time: ");
  329.   Serial.println(millis() - timeStart);
  330.  
  331.  
  332.   if (onOff) {
  333.     if (distance <= (5 + Error)){
  334.       fullStop();
  335.       madFlex(flexNumber);    // MAD FLEX HERE
  336.     }
  337.    
  338.     if (distance > (5 + Error)){    // It travels at half speed if it is 5< x <15 cm from the wall
  339.       halfSpeed();
  340.     }
  341.  
  342.     if (distance <= (10+ Error) and (millis() - timeStart) <= 58000){ // This leaves 2 secs to clear 5cm
  343.       fullStop();
  344.       digitalWrite(LED, LOW);
  345.       delay(250);
  346.       digitalWrite(LED, HIGH);
  347.       delay(250);
  348.       Serial.println("Waiting...");
  349.     }
  350.    
  351.     if (distance >= (15 + Error)){
  352.       fullSpeed();
  353.     }
  354.   }
  355.  
  356.   else{
  357.     fullStop();
  358.   }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement