SHARE
TWEET

Untitled

a guest Oct 11th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. #include "EV3Mailbox.c"
  21.  
  22. task main()
  23. {
  24.     displayBigTextLine(0, "Started!");
  25.  
  26.     char msgBufIn[MAX_MSG_LENGTH];  // To contain the incoming message.
  27.     char msgBufOut[MAX_MSG_LENGTH];  // To contain the outgoing message
  28.  
  29.     openMailboxIn("EV3_INBOX0");
  30.     openMailboxOut("EV3_OUTBOX0");
  31.  
  32.     while (true)
  33.     {
  34.         // Read input message.
  35.         // readMailboxIn() is non-blocking and returns "" if there is no message.
  36.         readMailboxIn("EV3_INBOX0", msgBufIn);
  37.         if (strcmp(msgBufIn, "Forward") == 0)
  38.         {
  39.             displayBigTextLine(0, "Forward");
  40.             setMotorSync(motorB, motorC, 0, 20);
  41.         }
  42.         else if  (strcmp(msgBufIn, "Backward") == 0)
  43.         {
  44.             displayBigTextLine(0, "Backward");
  45.             setMotorSync(motorB, motorC, 0, -20);
  46.         }
  47.         else if (strcmp(msgBufIn, "Left") == 0)
  48.         {
  49.             displayBigTextLine(0, "Left");
  50.             setMotorSpeed(motorC, 0);
  51.             setMotorSpeed(motorB, 20);
  52.         }
  53.         else if (strcmp(msgBufIn, "Right") == 0)
  54.         {
  55.             displayBigTextLine(0, "Right");
  56.             setMotorSpeed(motorB, 0);
  57.             setMotorSpeed(motorC, 20);
  58.         }
  59.         // Here fill your outgoing message handling.
  60.         // This means sending the distance in cm measured by the ultrasonic sensor
  61.         // and the angle in degrees measured by the gyro sensor.
  62.         // Put both values in msgBufOut.
  63.         // Use the format ?<distance> <angle>" (space between distance and angle) so that it can be read by the host program.
  64.         // Create message string to store the message received.
  65.         char msgBufIn[20];
  66.  
  67.         // Receive message from host computer.
  68.         // readMailboxIn is a library function in EV3Mailbox.c.
  69.         readMailboxIn("EV3_INBOX0", msgBufIn)
  70.         // Create message string to store the message to send.
  71.         char msgBufOut[20];
  72.  
  73.         // Measure distance and angle.
  74.         float dist = getUSDistance(S4);
  75.         float angle = getGyroDegrees(S2);
  76.  
  77.         // Fill msgBufOut[20] with the two numbers and space in between.
  78.         // %.1f means 1 digit after the decimal point.
  79.         sprintf(msgBufOut, "%.1f %.1f", dist, angle);
  80.  
  81.         // Send message to host computer.
  82.         // writeMailboxOut is a library function in EV3Mailbox.c.
  83.         writeMailboxOut("EV3_OUTBOX0", msgBufOut);
  84.  
  85.  
  86.  
  87.         writeMailboxOut("EV3_OUTBOX0", msgBufOut);
  88.  
  89.         delay(100);  // Wait 100 ms to give host computer time to react.
  90.     }
  91.     closeMailboxIn("EV3_INBOX0");
  92.     closeMailboxOut("EV3_OUTBOX0");
  93.     return;
  94.  
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top