Advertisement
msiemens

main.c

Dec 26th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "asuro.h"
  3.  
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // DEFINES
  6. ////////////////////////////////////////////////////////////////////////////////
  7.  
  8. #define MOTOR_SPEED_MAX 127
  9. #define MOTOR_SPEED 0.5
  10.  
  11. #define BACKWARDS_TIME 1000
  12. #define TURNING_TIME 400
  13.  
  14. #define LINE_NUM_AVG 20
  15. #define LINE_THRESHOLD 70
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // FORWARD DECLARATIONS
  19. ////////////////////////////////////////////////////////////////////////////////
  20.  
  21. // Is a switch pressed?
  22. int IsAnySwitchPressed(void);
  23.  
  24. // Drive forward
  25. void DriveForward(void);
  26.  
  27. // Turn around
  28. void TurnAround(void);
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////
  31. // METHOD DEFINITIONS
  32. ////////////////////////////////////////////////////////////////////////////////
  33.  
  34. //*
  35. int main(void) {
  36.     // Initialization
  37.     Init();
  38.  
  39.     // Declaration of variables
  40.     unsigned int linedata_init[2] = {0, 0};
  41.     unsigned int linedata_tmp[2];
  42.     unsigned int linedata[2];
  43.     unsigned int j;
  44.  
  45.     // Turn on Front LED
  46.     FrontLED(ON);
  47.  
  48.     // Get initial LineData
  49.     for (j = 0; j < LINE_NUM_AVG; j++) {
  50.         LineData(linedata_tmp);
  51.  
  52.         // Sum up all values
  53.         linedata_init[0] += linedata_tmp[0];
  54.         linedata_init[1] += linedata_tmp[1];
  55.     }
  56.  
  57.     linedata_init[0] /= LINE_NUM_AVG;
  58.     linedata_init[1] /= LINE_NUM_AVG;
  59.  
  60.     // DEBUG: Send initial values to PC
  61.     SerPrint("Initial LineData:\r\n");
  62.     SerPrint("Left: ");
  63.     PrintInt(linedata_init[0]);
  64.     SerPrint("\r\n");
  65.     SerPrint("Right: ");
  66.     PrintInt(linedata_init[1]);
  67.     SerPrint("\r\n");
  68.     SerPrint("\r\n");
  69.  
  70.     // Main loop
  71.     while (1) {
  72.         DriveForward();
  73.  
  74.         // Heading towards a cliff, turn around
  75.         LineData(linedata);
  76.         if ((int) linedata_init[0] - (int) linedata[0] > LINE_THRESHOLD
  77.             || (int) linedata_init[1] - (int) linedata[1] > LINE_THRESHOLD) {
  78.             TurnAround();
  79.  
  80.             // DEBUG output
  81.             SerPrint("Left: ");
  82.             PrintInt(linedata[0]);
  83.             SerPrint("\r\n");
  84.  
  85.             SerPrint("Right: ");
  86.             PrintInt(linedata[1]);
  87.             SerPrint("\r\n");
  88.         }
  89.  
  90.         // A switch was pressed, turn around
  91.         if (IsAnySwitchPressed()) {
  92.             TurnAround();
  93.         }
  94.     }
  95.     return 0;
  96. }
  97. // */
  98.  
  99. int IsAnySwitchPressed(void) {
  100.     unsigned char t1, t2;
  101.  
  102.     t1 = PollSwitch();
  103.     t2 = PollSwitch();
  104.  
  105.     if(t1 && t2) {
  106.         return TRUE;
  107.     } else {
  108.         return FALSE;
  109.     }
  110. }
  111.  
  112. void DriveForward(void) {
  113.     SetMotorPower(MOTOR_SPEED_MAX * MOTOR_SPEED, MOTOR_SPEED_MAX * MOTOR_SPEED);
  114. }
  115.  
  116. void TurnAround(void) {
  117.     // Go back a bit
  118.     SetMotorPower(-MOTOR_SPEED_MAX * MOTOR_SPEED, -MOTOR_SPEED_MAX * MOTOR_SPEED);
  119.     Msleep(BACKWARDS_TIME);
  120.  
  121.     // Turn around
  122.     SetMotorPower(-MOTOR_SPEED_MAX * MOTOR_SPEED, MOTOR_SPEED_MAX * MOTOR_SPEED);
  123.     Msleep(TURNING_TIME);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement