Advertisement
Rastus22

Robot Code

Oct 17th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #pragma config(StandardModel, "EV3_REMBOT")
  2. //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
  3.  
  4.  
  5.  
  6. /*
  7. This program takes advantage of the multitasking features of RobotC.
  8. A task is very similar to a function except for that it will run in parallel with the other code
  9.  
  10. I will be checking my emails semi-frequently during the IPT period so reply with any issues your having
  11.  
  12. */
  13.  
  14.  
  15.  
  16.  
  17. /*
  18. Setting up variables, sonar and time will be removed later
  19. food is 1000 so I can test other stuff without running out.
  20. All global variables must be declared here
  21. */
  22.  
  23. int food = 1000;
  24. int sonar = getUSDistance(S4);
  25. int time;
  26.  
  27. /* This task is to setup the 1 sec timer to reduce food by 1
  28. AFAIK this shouldn't need changes */
  29.  
  30. task timer()
  31. {
  32.     while(true)
  33.     {
  34.         wait1Msec(1000);
  35.         food = food - 1;
  36.         if (food < 1)
  37.         {
  38.             stopAllTasks();
  39.         }
  40.     }
  41. }
  42.  
  43. /* Checks if it is touching Alien */
  44.  
  45. task touch()
  46. {
  47.     while(true)
  48.     {
  49.         if (getTouchValue(S1) == 1)
  50.         {
  51.             food = food + 10
  52.             /* Play sound here. Idk how to do it so ask someone */
  53.         }
  54.     }
  55. }
  56.  
  57.  
  58. /* Reads colours on the ground. */
  59.  
  60. task colour()
  61. {
  62.     while(true)
  63.     {
  64.         /*
  65.         Change this to something that measures reflected light instead of colour names
  66.         something like getColorHue(nDeviceIndex)
  67.  
  68.         Also change the turnLeft to about 180 degrees.
  69.         */
  70.  
  71.         /*
  72.         if (getColorName(S3) == 'Black')
  73.         {
  74.             turnLeft(1, rotations, 50);
  75.  
  76.         }
  77.  
  78.  
  79.         if (getColorName(S3) == 'White')
  80.         {
  81.             food = food + 5
  82.         }
  83.         */
  84.     }
  85. }
  86.  
  87. /* Main task */
  88.  
  89. task main()
  90. {
  91.     startTask(timer);
  92.     startTask(touch);
  93.    
  94.     /*
  95.     startTask(colour);
  96.  
  97.     Uncomment this when you have fixed up colour
  98.     */
  99.  
  100.  
  101.  
  102.  
  103.     while(true)
  104.         {
  105.  
  106.             /* This is where stuff gets kinda weird, could probably be heavily optimised/changed */
  107.  
  108.  
  109.             /* In the testing software the Sonar would output 250 whenever it couldn't find anything
  110.             You may need to adjust this if that is not the case on the real robot */
  111.  
  112.             if (getUSDistance(S4) == 250)
  113.             {
  114.                 /* Starts timer to ensure it doesn't endlessly spin searching */
  115.                 clearTimer(T1);
  116.  
  117.                 while(getUSDistance(S4) == 250)
  118.                 {
  119.  
  120.                     /* Var is for debug, can be removed safely */
  121.                     time = time1[T1];
  122.  
  123.  
  124.                     /* Checks if it has been two seconds, if it has the robot will aimlessly drive in an attempt to get food */
  125.                     if (time1[T1] > 2000)
  126.                     {
  127.                         clearTimer(T1);
  128.                         motor(motorB) = 50;
  129.                         motor(motorC) = 50;
  130.                         wait1Msec(1000);
  131.                         motor(motorB) = 0;
  132.                         motor(motorC) = 0;
  133.                     }
  134.  
  135.  
  136.                     motor(motorB) = 100;
  137.                 }
  138.  
  139.                 /* This will only run once the robot has found something, drives forward */
  140.  
  141.                 motor(motorB) = 50;
  142.                 motor(motorC) = 50;
  143.  
  144.  
  145.             }
  146.  
  147.  
  148.         }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement