Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Robots 3.72 KB | None | 0 0
  1.  
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // ROBOTC EV3 remote control demonstration program.
  4. // This program receives messages from the host computer: Forward, Backward, Left and Right.
  5. // It sends back the distance in cm measured by the ultrasonic sensor and the angle in degrees
  6. // measured by the gyro sensor.
  7. ////////////////////////////////////////////////////////////////////////////////
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // Connection scheme:
  11. // motorA: medium motor front lever
  12. // motorB: large motor right wheel
  13. // motorC: large motor left wheel
  14. // motorD: -
  15. // Sensor S1: Touch sensor
  16. // Sensor S2: Gyro sensor
  17. // Sensor S3: Color sensor
  18. // Sensor S4: Ultrasonic sensor
  19. ////////////////////////////////////////////////////////////////////////////////
  20.  
  21.  
  22.  
  23.  
  24.  
  25. #include "EV3Mailbox.c"
  26.  
  27. task main()
  28. {
  29.  
  30.     displayBigTextLine(0, "Started!");
  31.  
  32.     char msgBufIn[MAX_MSG_LENGTH];  // To contain the incoming message.
  33.     char msgBufOut[MAX_MSG_LENGTH];  // To contain the outgoing message
  34.  
  35.     openMailboxIn("EV3_INBOX0");
  36.     openMailboxOut("EV3_OUTBOX0");
  37.  
  38.     while (true)
  39.     {
  40.         // Read input message.
  41.         // readMailboxIn() is non-blocking and returns "" if there is no message.
  42.         readMailboxIn("EV3_INBOX0", msgBufIn);
  43.         if (strcmp(msgBufIn, "Forward") == 0)
  44.         {
  45.             displayBigTextLine(0, "Forward");
  46.             setMotorSync(motorB, motorC, 0, 20);
  47.  
  48.         }
  49.         else if  (strcmp(msgBufIn, "Backward") == 0)
  50.         {
  51.             displayBigTextLine(0, "Backward");
  52.             setMotorSync(motorB, motorC, 0, -20);
  53.         }
  54.         else if (strcmp(msgBufIn, "Left") == 0)
  55.         {
  56.             displayBigTextLine(0, "Left");
  57.             setMotorSpeed(motorC, 0);
  58.             setMotorSpeed(motorB, 20);
  59.         }
  60.         else if (strcmp(msgBufIn, "Right") == 0)
  61.         {
  62.             displayBigTextLine(0, "Right");
  63.             setMotorSpeed(motorB, 0);
  64.             setMotorSpeed(motorC, 20);
  65.         }
  66.  
  67.  
  68.         // Here fill your outgoing message handling.
  69.         // This means sending the distance in cm measured by the ultrasonic sensor
  70.         // and the angle in degrees measured by the gyro sensor.
  71.         // Put both values in msgBufOut.
  72.         // Use the format ?<distance> <angle>" (space between distance and angle) so that it can be read by the host program.
  73.         // Create message string to store the message received.
  74.         char msgBufIn[20];
  75.  
  76.         // Receive message from host computer.
  77.         // readMailboxIn is a library function in EV3Mailbox.c.
  78.         readMailboxIn("EV3_INBOX0", msgBufIn);
  79.  
  80.         // Drive forward if “Forward” is received.
  81.         if (strcmp(msgBufIn, "Forward") == 0)
  82.         {
  83.             displayBigTextLine(0, "Forward");
  84.             setMotorSync(motorB, motorC, 0, 20);
  85.  
  86.         }
  87.         else if  (strcmp(msgBufIn, "Backward") == 0)
  88.         {
  89.             displayBigTextLine(0, "Backward");
  90.             setMotorSync(motorB, motorC, 0, -20);
  91.         }
  92.         else if (strcmp(msgBufIn, "Left") == 0)
  93.         {
  94.             displayBigTextLine(0, "Left");
  95.             setMotorSpeed(motorC, 0);
  96.             setMotorSpeed(motorB, 20);
  97.         }
  98.         else if (strcmp(msgBufIn, "Right") == 0)
  99.         {
  100.             displayBigTextLine(0, "Right");
  101.             setMotorSpeed(motorB, 0);
  102.             setMotorSpeed(motorC, 20);
  103.         }
  104.         // Create message string to store the message to send.
  105.         char msgBufOut[20];
  106.  
  107.         // Measure distance and angle.
  108.         float dist = getUSDistance(S4);
  109.         float angle = getGyroDegrees(S2);
  110.  
  111.         // Fill msgBufOut[20] with the two numbers and space in between.
  112.         // %.1f means 1 digit after the decimal point.
  113.         sprintf(msgBufOut, "%.1f %.1f", dist, angle);
  114.  
  115.         // Send message to host computer.
  116.         // writeMailboxOut is a library function in EV3Mailbox.c.
  117.         writeMailboxOut("EV3_OUTBOX0", msgBufOut);
  118.  
  119.  
  120.  
  121.         writeMailboxOut("EV3_OUTBOX0", msgBufOut);
  122.  
  123.         delay(100);  // Wait 100 ms to give host computer time to react.
  124.     }
  125.     closeMailboxIn("EV3_INBOX0");
  126.     closeMailboxOut("EV3_OUTBOX0");
  127.     return;
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement