Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LEGS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
  2.  
  3. /* Used to control the bottom engines */
  4.  
  5. #include "Arduino.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. #include <Wire.h>
  15. #include <MD23.h>
  16.  
  17. MD23 md23;
  18.  
  19. void md23_cb( const std_msgs::Empty& cmd_msg){
  20.   //md23.write(cmd_msg.data); //set servo angle, should be from 0-180  
  21.   //digitalWrite(13, HIGH-digitalRead(13));  //toggle led
  22.   forward();
  23.   fstop();
  24.   delay(1000);
  25.   backward();
  26.   bstop();
  27.   delay(1000);
  28. }
  29.  
  30. ros::Subscriber<std_msgs::Empty> motorSub("MotorControl", md23_cb);
  31.  
  32.  
  33. /*%%%%%%%%%%%%%%% SETUP AND LOOP %%%%%%%%%%%%%%%%%%%%%%%%%*/
  34.  
  35. ros::NodeHandle nh;
  36.  
  37. int incomingByte = 0;   // for incoming serial data
  38.  
  39. void setup(){
  40.  
  41.   Serial.begin(57600);
  42.  
  43.   pinMode(13, OUTPUT);
  44.   nh.initNode();
  45.   //Setup for legs!
  46.   Wire.begin(); // you have to init the i2c bus by yourself
  47.   nh.subscribe(motorSub);
  48.   delay(1000);
  49.    
  50. }
  51.  
  52. void loop(){
  53.   nh.spinOnce();
  54.   delay(300);
  55.   forward();
  56.  
  57.  
  58. }
  59.  
  60. int i = 129;
  61.  
  62. void forward()
  63. {
  64.   //while (1) {
  65.     for (i; i<150; i++)
  66.     {
  67.       md23.setMotor1Speed(i); // slow forwad, first motor
  68.       md23.setMotor2Speed(i);
  69.       //drive(i);
  70.       delay(100);
  71.     }
  72.    
  73.     /*
  74.     for (i; i>128; i--)
  75.     {
  76.       md23.setMotor1Speed(i); // slow forwad, first motor
  77.       md23.setMotor2Speed(i);
  78.       //drive(i);
  79.       delay(100);
  80.     }
  81.     */
  82.    
  83.     while (1){
  84.       md23.setMotor1Speed(150); // slow forwad, first motor
  85.       md23.setMotor2Speed(150);
  86.       delay(100);
  87.     }
  88.      
  89.   //}
  90.  
  91.  
  92. }
  93.  
  94. void backward()
  95. {
  96.    for (i; i>78; i--)
  97.   {
  98.     md23.setMotor1Speed(i); // slow forwad, first motor
  99.     md23.setMotor2Speed(i);
  100.     delay(100);
  101.   }
  102.  
  103. }
  104.  
  105. void fstop()
  106. {
  107.   i = md23.getMotor1Speed();
  108.  
  109.  
  110.   for(i; i>129; i--)
  111.   {
  112.     md23.setMotor1Speed(i); // slow forwad, first motor
  113.     md23.setMotor2Speed(i);
  114.     delay(100);
  115.   }
  116.  
  117.   md23.setMotor1Speed(128); // stop the first motor
  118.   md23.setMotor2Speed(128); // stop the second motor
  119.  
  120. }
  121.  
  122. void bstop()
  123. {
  124.   i = md23.getMotor1Speed();
  125.  
  126.  
  127.   for(i; i<129; i++)
  128.   {
  129.     md23.setMotor1Speed(i); // slow forwad, first motor
  130.     md23.setMotor2Speed(i);
  131.     delay(100);
  132.   }
  133.  
  134.   md23.setMotor1Speed(128); // stop the first motor
  135.   md23.setMotor2Speed(128); // stop the second motor
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement