Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.09 KB | None | 0 0
  1.  
  2.  
  3. /* Includes ------------------------------------------------------------------*/
  4. #include <Servo.h>    //to define and control servos
  5.  
  6. /* Definitions --------------------------------------------------------------------*/
  7. const long SerialCommSpeed      = 115200;     // Defining Serial Port Communication Speed
  8. const int legCount              = 4;          // Number of Legs
  9. const int nodeCount             = 3;          // Number of Nodes per Leg
  10. const int ServoStartAngle       = 90;         // Default Position of the Servos on Start
  11. const int ServoUpdateFrameRate  = 20;         // Default Servo Update Frame Rate in Milliseconds
  12.  
  13. // Instantiating Servo Library
  14. Servo servo[legCount][nodeCount];
  15.  
  16. // Defining initial position as 0
  17. int servoCurrPosition[legCount][nodeCount] = {
  18.  {0, 0, 0},
  19.  {0, 0, 0},
  20.  {0, 0, 0},
  21.  {0, 0, 0}
  22. };
  23.  
  24. // Assigning GPIO Servo Ports
  25. const int servo_pin[legCount][nodeCount] = {
  26.  {13, 12, 11},    // Back Right Leg
  27.  {10, 9, 8},      // Back Left Leg
  28.  {7, 6, 5},       // Front Right Leg
  29.  {4, 3, 2}        // Front Left Leg
  30. };
  31.  
  32. // Misc Definitions
  33. int servoMaxValue = 180;
  34. int servoMinValue = 0;
  35.  
  36. void setup() {
  37.   // Starting Serial Ports
  38.   Serial.begin(SerialCommSpeed);
  39.   Serial.println("[OTTO]: Serial started at " + String(SerialCommSpeed) + "kbps");
  40.  
  41.   //initialize servos  
  42.   servo_attach();
  43.   Serial.println("[OTTO]: Initialization Complete");  
  44. }
  45.  
  46.  
  47. /* ---------------------------------------------------------------------------
  48.   - Attach the servo library ( methods /etc ) to the Servo Array
  49. ---------------------------------------------------------------------------*/
  50. void servo_attach(void) {
  51.   for (int i = 0; i < legCount; i++) {
  52.     for (int j = 0; j < nodeCount; j++) {
  53.       // Attach the Servos
  54.       servo[i][j].attach(servo_pin[i][j]);
  55.       Serial.println("[OTTO]: Leg: " + String(i) + " - Servo: " + String(j) + " - Successfuly Attached!");
  56.      
  57.       // Move the Servos to Default Start Angle
  58.       servo[i][j].write(ServoStartAngle);
  59.       Serial.println("[OTTO]: Leg: " + String(i) + " - Servo: " + String(j) + " - Set to: " + String(ServoStartAngle));
  60.      
  61.       // Updating the Current Servo Positions
  62.       servoCurrPosition[i][j] = ServoStartAngle;
  63.     }
  64.   }
  65. }
  66.  
  67. /*---------------------------------------------------------------------------
  68.   - loop function
  69. ---------------------------------------------------------------------------*/
  70. void loop() {
  71.   Serial.println("---------------------------------------------------------");
  72.   Serial.println(" MAIN LOOP STARTED ");
  73.   Serial.println("---------------------------------------------------------");
  74.  
  75.   int newServoPos[4][3] = {
  76.     { 130, 180, 90 },
  77.     { 90, 90, 90 },
  78.     { 90, 90, 90 },
  79.     { 90, 90, 90 }
  80.   };
  81.  
  82.   updateServoPos( newServoPos, 1000 );
  83.   Serial.println("---------------------------------------------------------");
  84.   Serial.println(" MAIN LOOP FINISHED... DELAYING ");
  85.   Serial.println("---------------------------------------------------------");
  86.   delay(10000);
  87.   Serial.println(" ");
  88. }
  89.  
  90.  
  91. void debugMap (int mapArray[], int arraySize) {
  92.   int total = 0;
  93.  
  94.   Serial.println("---------------------------------------------------------");
  95.   Serial.println(" Map Debbuger ");
  96.   Serial.println("---------------------------------------------------------");
  97.   for (int x=0; x < arraySize; x += 1) {
  98.     Serial.print(" At position ");
  99.     Serial.print(x);
  100.     Serial.print(" value is ");
  101.     Serial.println(mapArray[x]);
  102.     total += (mapArray[x]);
  103.   }
  104.  
  105.   Serial.println("---------------------------------------------------------");
  106.   Serial.print("TOTAL IS ");
  107.   Serial.println(total);
  108.   Serial.println("---------------------------------------------------------");
  109. }
  110.  
  111. /* ---------------------------------------------------------------------------
  112.  - generateServoLoopValues - This function will calculate how much the servo
  113.  has to move per loop iteration
  114.  --------------------------------------------------------------------------- */
  115. int generateServoMovementMap (int totalDistanceToMove, int loopTimes, int currLoopIteration ) {
  116.  
  117.   float accumulated = 0;
  118.   float movementPerLoop = ((float)totalDistanceToMove / (float)loopTimes);
  119.   int movementMap[loopTimes];
  120.  
  121.   for (int loopNo = 0; loopNo < loopTimes; loopNo += 1) {
  122.    
  123.     // Check if the movementPerLoop is bigger than 1
  124.     if (movementPerLoop > 1) {
  125.         // Converting the float movementPerLoop into an integer since
  126.         // the servo cannot move in float angles.
  127.         int realDistanceToMove  = (int)movementPerLoop;
  128.        
  129.         // Check if there's any left over to be saved
  130.         float leftOver = (movementPerLoop - (realDistanceToMove));
  131.  
  132.         // If there's any left over, we should accumulate it
  133.         if (leftOver != 0) {
  134.           accumulated += leftOver;
  135.         }
  136.  
  137.         movementMap[loopNo] = realDistanceToMove;
  138.      
  139.     // Check if the movementPerLoop is smaller than 1
  140.     } else if (movementPerLoop < -1) {
  141.         // Converting the float movementPerLoop into an integer since
  142.         // the servo cannot move in float angles.
  143.         int realDistanceToMove  = (int)movementPerLoop;
  144.        
  145.         // Check if there's any left over to be saved
  146.         float leftOver = (movementPerLoop - (realDistanceToMove));
  147.  
  148.         // If there's any left over, we should accumulate it
  149.         if (leftOver != 0) {
  150.           accumulated += leftOver;
  151.         }
  152.  
  153.         movementMap[loopNo] = realDistanceToMove;
  154.  
  155.     // movementPerLoop is to small to move, we should simple store
  156.     } else {
  157.       accumulated += movementPerLoop;
  158.       movementMap[loopNo] = 0;
  159.     }
  160.    
  161.     // Check if there is any value bigger than 1 in accumulated
  162.     if (round(accumulated*10)/10 >= 1) {
  163.       accumulated -= 1;  
  164.       movementMap[loopNo] += 1;
  165.      
  166.     // Check if there is any value smaller than -1 in accumulated  
  167.     } else if (round(accumulated*10)/10 <= -1) {
  168.       accumulated += 1;
  169.       movementMap[loopNo] += -1;
  170.     }
  171.    
  172.   }
  173.  
  174. //  debugMap(movementMap, loopTimes);
  175.   return movementMap[currLoopIteration];
  176. }
  177.  
  178. /* ---------------------------------------------------------------------------
  179.  - UpdateServoPos
  180.  --------------------------------------------------------------------------- */
  181. void updateServoPos( int servoDesiredPosition[legCount][nodeCount], int executionTime ) {
  182.  
  183.   // Calculating how many loops will happen
  184.   const int loopTimes = abs( executionTime / ServoUpdateFrameRate );
  185.  
  186.   // Executing servo movements
  187.   for (int loopNo = 0; loopNo < loopTimes; loopNo += 1) {
  188.    
  189.     for (int leg = 0; leg < legCount; leg += 1) {
  190.       for (int node = 0; node < nodeCount; node += 1) {
  191.  
  192.         if (servoDesiredPosition[leg][node] != servoCurrPosition[leg][node]) {
  193.           // Servo needs to move, let's calculate how much to move
  194.           int totalDistanceToMove = servoDesiredPosition[leg][node] - servoCurrPosition[leg][node];
  195.          
  196.           // Ensuring that the totalDistanceToMove doesn't exceed servoMaxValue
  197.           if ((servoCurrPosition[leg][node] + totalDistanceToMove) > servoMaxValue) {
  198.             totalDistanceToMove = (servoMaxValue - servoCurrPosition[leg][node]);
  199.          
  200.           // Ensuring that the totalDistanceTomove doesn't exceed servoMinValue
  201.           } else if ((servoCurrPosition[leg][node] + totalDistanceToMove) < servoMinValue) {
  202.             totalDistanceToMove = -(servoMinValue + servoCurrPosition[leg][node]);
  203.           }
  204.  
  205.           // Getting the amount of the servo should move on this loop iteration
  206.           int movement = generateServoMovementMap( totalDistanceToMove, loopTimes, loopNo );
  207.          
  208.           // Checking if it's necessary to move the servo
  209.           if (movement != 0) {
  210.             Serial.print("Moving servo [");
  211.             Serial.print(leg);
  212.             Serial.print("][");
  213.             Serial.print(node);
  214.             Serial.print("] from ");
  215.             Serial.print(servoCurrPosition[leg][node]);
  216.             Serial.print(" to ");
  217.             Serial.println(movement);
  218.           }
  219.         }
  220.        
  221.       }
  222.     }
  223.  
  224.   }
  225.  
  226.   return;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement