Advertisement
Guest User

ArduinoCode

a guest
Apr 6th, 2017
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.39 KB | None | 0 0
  1. /* Used to (hopefully) communicate with both arms and head on our robot */
  2.  
  3.  
  4. #include "Arduino.h"
  5. #include "MeccaBrain.h"
  6. #include <ros.h>
  7. #include <std_msgs/UInt16.h>
  8. #include <std_msgs/Empty.h>
  9. #include <std_msgs/String.h>
  10. #include <std_msgs/MultiArrayLayout.h>
  11. #include <std_msgs/MultiArrayDimension.h>
  12. #include <std_msgs/Int8MultiArray.h>
  13.  
  14. ros::NodeHandle  nh;
  15.  
  16. const int pwnPin1 = 9; //left arm
  17. const int pwnPin2 = 10; //right arm
  18. const int pwnPin3 = 11; //head
  19. MeccaBrain chain1(pwnPin1);
  20. MeccaBrain chain2(pwnPin2);
  21. MeccaBrain headChain(pwnPin3);
  22.  
  23.  
  24.  
  25.  
  26. //Stuff for headpositioning
  27. int servoPin = 11;
  28. MeccaBrain mb = MeccaBrain(servoPin);
  29. byte positions[2] = {0x77, 0x97}; // 0x00 = full clockwise, 0xEF = full counter clockwise
  30. byte currentPositions[2] = {0x77, 0x97};
  31. byte ledColor[] = {0x07,0x00,0x00,0x00}; // r,g,b,fade
  32. int splits = 20;
  33.  
  34.  
  35. //Callback function - when subscriber recieves an "Empty" message, execute the following
  36. void servo_cb( const std_msgs::Empty& cmd_msg){
  37.  
  38.    //Wave-routine for arm one
  39.    /*raiseShoulderTwo();
  40.    delay(500);
  41.    elbow();
  42.    
  43.    //swipeE();
  44.    delay(500);
  45.    
  46.    //This should wave 3 times
  47.    wave();
  48.    delay(500);
  49.    lowerShoulder();
  50.    delay(500);
  51.    
  52.    */
  53.    
  54.    //Wave-routine for arm two
  55.     raiseShoulderTwo();
  56.     delay(500);
  57.     elbowTwo();
  58.     delay(500);
  59.     //This should wave 3 times
  60.     waveTwo();
  61.     delay(500);
  62.     lowerShoulderTwo();
  63.     delay(500);
  64.    
  65.    
  66.     //Moves head
  67.     moveHead();
  68.     delay(500);
  69.    
  70.     waveDone();
  71.     //Toggles the LED  
  72.     digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
  73. }
  74.  
  75. void positionCallback(const std_msgs::Int8MultiArray& position_msg){
  76.   positions[0] = position_msg.data[0];
  77.   positions[1] = position_msg.data[1];
  78.  
  79.   if(positions[0] == 0x77 && positions[1] == 0x97){
  80.      mb.setLEDColor(7,0,0,0);
  81.   }else{
  82.      mb.setLEDColor(0,7,0,0);
  83.   }
  84.  
  85.   mb.setServoPosition(0,position_msg.data[0]);
  86.   mb.setServoPosition(1,position_msg.data[1]);
  87.   mb.communicate();
  88.   /*
  89.   byte diff = abs(positions[0] - currentPositions[0])/20.0;
  90.   int i;
  91.   for(i=0; i<splits; i++){
  92.     mb.setServoPosition(0,currentPositions[0]+(diff*(i+1)));
  93.     mb.communicate();
  94.     delay(20);
  95.   }
  96.  
  97.   currentPositions[0] = position_msg.data[0];
  98.   currentPositions[1] = position_msg.data[1];
  99.   */
  100.  //setInCenterPosition();
  101. }
  102.  
  103. void setInCenterPosition(){
  104.   mb.setServoPosition(0,0x77);
  105.   mb.setServoPosition(1,0x77);
  106.   mb.communicate();
  107. }
  108.  
  109. std_msgs::Empty empty_msg;
  110. ros::Publisher waveDonePub("/wave_done", &empty_msg);
  111.  
  112. ros::Subscriber<std_msgs::Int8MultiArray> sub("/face_location_pos", positionCallback );
  113.  
  114. ros::Subscriber<std_msgs::Empty> armSub("servo", servo_cb);
  115.  
  116. //Used for sweep
  117. int pos = 0;
  118.  
  119. void setup(){
  120.   pinMode(13, OUTPUT);
  121.   nh.initNode();
  122.   setInCenterPosition();
  123.   nh.subscribe(sub);
  124.   nh.subscribe(armSub);
  125.   nh.advertise(waveDonePub);
  126. }
  127.  
  128. void loop(){
  129.   nh.spinOnce();
  130.   delay(1);
  131. }
  132.  
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136.  
  137.  
  138. //Moves the head
  139. void moveHead() {
  140.   int pos = 0;
  141.     //setToLIMAndGetPos(0)
  142.    
  143.     for (pos = 90; pos <= 128; pos += 10) {
  144.       headChain.setServoPosition(1,pos);
  145.       headChain.communicate();
  146.       delay(20);
  147.     }
  148.    
  149.    
  150.     // Why do we have to similar for-loops?
  151.    
  152.      for (pos = 0; pos <= 128; pos += 10) {
  153.       headChain.setServoPosition(1,pos);
  154.       headChain.communicate();
  155.       delay(20);
  156.     }
  157.    
  158.    
  159.    
  160.     //RÖD
  161.     headChain.setServoColor(0, 0xF1);
  162.     headChain.communicate();
  163.     delay(500);
  164. }
  165.  
  166. void waveDone() {
  167.   //std_msgs::Empty empty_msg;
  168.   waveDonePub.publish( &empty_msg);
  169.   delay(100);
  170. }
  171.  
  172.  
  173.  
  174. void swipeE() {
  175.   int pos = 0;
  176.     //setToLIMAndGetPos(0)
  177.    
  178.     for (pos = 0; pos <= 255; pos += 10) {
  179.       chain1.setServoPosition(1,pos);
  180.       chain1.communicate();
  181.       delay(20);
  182.     }
  183.     for (pos = 255; pos >= 0; pos -= 10) {
  184.       chain1.setServoPosition(1,pos);
  185.       chain1.communicate();
  186.       delay(20);
  187.       }
  188.       }
  189.      
  190.  
  191. //Wave-function för arm 1
  192. void raiseShoulder() {
  193.  
  194.     int pos = 0;
  195.     //setToLIMAndGetPos(0)
  196.    
  197.     for (pos = 0; pos <= 128; pos += 10) {
  198.       chain1.setServoPosition(0,pos);
  199.       chain1.communicate();
  200.       delay(20);
  201.     }
  202.  
  203.     //RÖD
  204.     chain1.setServoColor(0, 0xF1);
  205.     chain1.communicate();
  206.     delay(500);
  207. }
  208.  
  209. void elbow() {
  210.  
  211.     int pos = 0;
  212.    
  213.     //chain = c;
  214.     for (pos = setToLIMAndGetPos(1); pos <= 128; pos += 10) {
  215.       chain1.setServoPosition(1, pos);
  216.       chain1.communicate();
  217.       delay(10);
  218.     }
  219.  
  220.     //Någon oklar färg gul
  221.     chain1.setServoColor(1, 0xF3);
  222.     chain1.communicate();
  223.     delay(500);
  224. }
  225.  
  226. void wave() {
  227.  
  228.   int pos = 0;
  229.   int i = 0;
  230.   for (i = 0; i <= 2; i++) {
  231.     for (pos = 128; pos >= 0; pos -= 10) {
  232.       chain1.setServoPosition(2, pos);
  233.       chain1.communicate();
  234.       delay(15);
  235.     }
  236.    
  237.     for (pos = 0; pos <= 128; pos += 10) {
  238.       chain1.setServoPosition(2, pos);
  239.       chain1.communicate();
  240.       delay(15);
  241.     }
  242.     //delay(200);
  243.   }
  244.  
  245.   //Lila
  246.   chain1.setServoColor(2, 0xF5);
  247.   chain1.communicate();
  248.   delay(500);
  249.  
  250. }
  251.  
  252.  
  253. void lowerShoulder() {
  254.  
  255.   int pos1 = 0;
  256.   int pos2 = 0;
  257.   int pos3 = 0;
  258.  
  259.   for (pos1 = setToLIMAndGetPos(2); pos1 <= 128; pos1 += 10) {
  260.      chain1.setServoPosition(2, pos1);
  261.      chain1.communicate();
  262.      delay(20);
  263.   }
  264.   //128
  265.   //setToLIMAndGetPos(1)
  266.   for (pos2 = setToLIMAndGetPos(1); pos2 >= 0; pos2 -= 10) {
  267.      chain1.setServoPosition(1, pos2);
  268.      chain1.communicate();
  269.      delay(20);
  270.   }
  271.  
  272.   //128
  273.   for (pos3 = setToLIMAndGetPos(0); pos3 <= 255; pos3 +=10) {
  274.  
  275.     chain1.setServoPosition(0, pos3);
  276.     chain1.communicate();
  277.     delay(20);
  278.   }
  279.  
  280.   chain1.setServoColor(0, 0xF7);
  281.   chain1.setServoColor(1, 0xF7);
  282.   chain1.setServoColor(2, 0xF7);
  283.   chain1.communicate();
  284.   delay(500);
  285.   //c = c;
  286.   /* Gammal kod
  287.   chain1.setServoPosition(2,128);
  288.   chain1.communicate();
  289.   delay(600);
  290.   chain1.setServoPosition(1,24);
  291.   chain1.communicate();
  292.   delay(600);
  293.   chain1.setServoPosition(0,232);
  294.   chain1.communicate();
  295.   delay(600);
  296.   */
  297. }
  298.  
  299. //Sets servo to LIM , allowing it to return its position. Then calls getServoPosition.
  300. int setToLIMAndGetPos (int servoNum) {
  301.   chain1.setServotoLIM(servoNum);
  302.   int pos = chain1.getServoPosition(servoNum);
  303.   chain1.communicate();
  304.   delay(100);
  305.   return pos;
  306. }
  307.  
  308. //------------------------------------------------------
  309. //Andra armen
  310.  
  311. void raiseShoulderTwo() {
  312.  
  313.     int pos = 255;
  314.     //setToLIMAndGetPos(0)
  315.    
  316.     for (pos = 255; pos >= 128; pos -= 10) {
  317.       chain2.setServoPosition(0,pos);
  318.       chain2.communicate();
  319.       //delay(10);
  320.     }
  321.  
  322.     //RÖD
  323.     chain2.setServoColor(0, 0xF1);
  324.     chain2.communicate();
  325.     delay(500);
  326. }
  327.  
  328.  
  329.  
  330. void elbowTwo() {
  331.  
  332.     int pos = 0;
  333.    
  334.     //chain = c;
  335.     for (pos = setToLIMAndGetPos(1); pos <= 150; pos += 20) {
  336.       chain2.setServoPosition(1, pos);
  337.       chain2.communicate();
  338.       delay(10);
  339.     }
  340.  
  341.     //Någon oklar färg
  342.     chain2.setServoColor(1, 0xF3);
  343.     chain2.communicate();
  344.     delay(500);
  345. }
  346.  
  347. void waveTwo() {
  348.  
  349.   int pos = 255;
  350.   int i = 0;
  351.   //for (i = 0; i <= 1; i++) {
  352.     for (pos = 128; pos <= 255; pos += 20) {
  353.       chain2.setServoPosition(2, pos);
  354.       chain2.communicate();
  355.       delay(10);
  356.     }
  357.    
  358.     for (pos = 255; pos >= 128; pos -= 20) {
  359.       chain2.setServoPosition(2, pos);
  360.       chain2.communicate();
  361.       delay(10);
  362.     }
  363.     //delay(200);
  364.   //}
  365.  
  366.   //Lila
  367.   chain2.setServoColor(2, 0xF5);
  368.   chain2.communicate();
  369.   //delay(500);
  370.  
  371. }
  372.  
  373.  
  374. void lowerShoulderTwo() {
  375.  
  376.   int pos1 = 0; // Servo two
  377.   int pos2 = 0; // Servo 1
  378.   int pos3 = 0; // Servo 0
  379.  
  380.   for (pos1 = setToLIMAndGetPos(2); pos1 >= 128; pos1 -= 20) {
  381.      chain2.setServoPosition(2, pos1);
  382.      chain2.communicate();
  383.      //delay(10);
  384.   }
  385.   //128 change to 255 resp 0 on servo 0 and 1
  386.   //setToLIMAndGetPos(1)
  387.   for (pos2 = 128; pos2 <= 255; pos2 += 20) {
  388.      chain2.setServoPosition(1, pos2);
  389.      chain2.communicate();
  390.      //delay(10);
  391.   }
  392.  
  393.   //128
  394.   for (pos3 = setToLIMAndGetPos(0); pos3 >= 0; pos3 -=20) {
  395.  
  396.     chain2.setServoPosition(0, pos3);
  397.     chain2.communicate();
  398.     //delay(10);
  399.   }
  400.  
  401.   chain2.setServoColor(0, 0xF7);
  402.   chain2.setServoColor(1, 0xF7);
  403.   chain2.setServoColor(2, 0xF7);
  404.   chain2.communicate();
  405.   //delay(500);
  406.  
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement