Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1.  
  2. #include <ros/ros.h>
  3. #include <sensor_msgs/Joy.h>
  4. #include </usr/local/webots/include/controller/c/webots/motor.h>
  5. #include </usr/local/webots/include/controller/c/webots/robot.h>
  6.  
  7.  
  8. #include <webots/Robot.hpp>
  9. #include <webots/Motor.hpp>
  10. #include <webots/DifferentialWheels.hpp>
  11.  
  12. // Controller update rate:
  13. // needs to be a multiple of basicTimeStep, the simulation update rate defined in the Webots world file
  14. #define TIME_STEP 32 // Default is 32
  15.  
  16. // Note:
  17. // printf() is used instead of ROS_INFO, ROS_WARN, etc. because the output goes to the Webots console.
  18. // If you want to use ROS logging, you can change this and set the environment variable in the launch file.
  19.  
  20. // Controller class:
  21. // derives from the same class as used in the Webots Scene Tree: Robot, DifferentialWheels or Supervisor
  22. class WebotsJoystickController : public webots::DifferentialWheels
  23. {
  24.     public:
  25.         // Constructor
  26.         WebotsJoystickController();
  27.  
  28.         // Run control loop
  29.         void run(void);
  30.  
  31.     private:
  32.         enum{JOYPAD_LEFT = 1, JOYPAD_RIGHT, JOYPAD_UP, JOYPAD_DOWN, JOYPAD_STOP};
  33.         unsigned int cmd_;
  34.         double left_speed_;
  35.         double right_speed_;
  36.         double k_speed_;
  37.  
  38.         ros::NodeHandle nh_;
  39.         ros::Subscriber subscriberJoy;
  40.  
  41.         // Callback for ROS messages on '/joy' topic
  42.         void callbackJoy(const sensor_msgs::Joy::ConstPtr& joy);
  43. };
  44.  
  45.  
  46. // Constructor
  47. WebotsJoystickController::WebotsJoystickController() :
  48.   webots::DifferentialWheels(),
  49.   cmd_(JOYPAD_STOP),
  50.   left_speed_(0.0),
  51.   right_speed_(0.0),
  52.   k_speed_(0.0)
  53. {
  54.     // Get robot speed setting from ROS Parameter server
  55.     nh_.param("robot_speed", k_speed_, 30.0);
  56.  
  57.     // Initialize ROS Subscriber
  58.     subscriberJoy = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &WebotsJoystickController::callbackJoy, this);
  59. }
  60.  
  61. // Run controller
  62. void WebotsJoystickController::run(void)
  63. {
  64.     // Helpful info, will be displayed in Webots console
  65.     printf("Joypad connected and running. \n");
  66.  
  67.     printf("Commands: \n");
  68.     printf("  arrow up    = move forward \n");
  69.     printf("  arrow down  = move backward \n");
  70.     printf("  arrow left  = rotate left \n");
  71.     printf("  arrow right = rotate right \n");
  72.     printf("  button 1    = stop \n");
  73.  
  74.     printf("Robot speed: %d \n", (int)k_speed_ );
  75.  
  76.     // Main control loop: runs every TIME_STEP ms
  77.     while(step(TIME_STEP) != -1) {
  78.  
  79.         // Process most recent joystick command  
  80.         switch(cmd_)
  81.         {
  82.             case JOYPAD_UP:
  83.                 left_speed_  = k_speed_;
  84.                 right_speed_ = k_speed_;
  85.                 break;
  86.             case JOYPAD_DOWN:
  87.                 left_speed_  = -k_speed_;
  88.                 right_speed_ = -k_speed_;
  89.                 break;
  90.             case JOYPAD_LEFT:
  91.                 left_speed_  = -k_speed_;
  92.                 right_speed_ = k_speed_;
  93.                 break;
  94.             case JOYPAD_RIGHT:
  95.                 left_speed_  = k_speed_;
  96.                 right_speed_ = -k_speed_;
  97.                 break;
  98.             default:  // case JOYPAD_STOP:
  99.                 left_speed_ = 0.0;
  100.                 right_speed_ = 0.0;
  101.                 break;
  102.         }
  103.  
  104.         // Set speed of differential drive wheels
  105.         setSpeed(left_speed_, right_speed_);
  106.  
  107.         // Check for callbacks
  108.         ros::spinOnce();
  109.     }
  110. }
  111.  
  112. // Callback for ROS messages on '/joy' topic
  113. void WebotsJoystickController::callbackJoy(const sensor_msgs::Joy::ConstPtr& joy) {
  114.  
  115.     // Debug:
  116.     // when joystick button is pressed, display values of all
  117.     // axes/buttons in the Webots console
  118.    
  119.     printf("Axes:");
  120.     for(size_t i=0;i<joy->axes.size();i++) printf(" %g",joy->axes[i]);
  121.     printf(" - Buttons:");
  122.     for(size_t i=0;i<joy->buttons.size();i++) printf(" %d",joy->buttons[i]);
  123.     printf("\n");
  124.    
  125.  
  126.     // The joystick outputs both axis values at once, so lets find the
  127.     // axis with the biggest value and set the matching command state
  128.     float m = 0.0;
  129.     for (size_t i = 0; i < 2; i++)
  130.     {
  131.         if (fabsf(joy->axes[i]) > m)
  132.             m = fabsf(joy->axes[i]);
  133.     }
  134.     if (m > 0.1) {
  135.         if ( joy->axes[0] == m)
  136.             cmd_ = JOYPAD_LEFT;
  137.         else if (-joy->axes[0] == m)
  138.             cmd_ = JOYPAD_RIGHT;
  139.         else if (joy->axes[1] == m)
  140.             cmd_ = JOYPAD_UP;
  141.         else if (-joy->axes[1] == m)  
  142.             cmd_ = JOYPAD_DOWN;
  143.     }
  144.     // If button 1 or button 2 is pressed, stop the robot
  145.     if (joy->buttons[0] || joy->buttons[1])
  146.         cmd_ = JOYPAD_STOP;
  147. }
  148.  
  149. int main(int argc, char** argv)
  150. {
  151.     ros::init(argc, argv, "webots_joy_demo");
  152.  
  153.     WebotsJoystickController *webots_joy_demo = new WebotsJoystickController();
  154.     webots_joy_demo->run();
  155.    
  156.      wb_robot_init();
  157.  
  158.  // initialize motors
  159.  WbDeviceTag wheels[4];
  160.  char wheels_names[4][8] = {
  161.  "wheel1", "wheel2", "wheel3", "wheel4"
  162.  };
  163.  
  164.  
  165.  int i;
  166.  double speed = 0;
  167.  
  168. //Wheel Naming
  169.  for (i=0; i<4 ; i++)
  170.  {
  171.    wheels[i] = wb_robot_get_device(wheels_names[i]);
  172.   }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.   while (wb_robot_step(TIME_STEP) != -1)
  180.   {
  181.  
  182.  wb_motor_set_position(wheels[0], INFINITY);
  183.  wb_motor_set_velocity(wheels[0],speed);
  184.  
  185.   wb_motor_set_position(wheels[1], INFINITY);
  186.  wb_motor_set_velocity(wheels[1],speed);
  187.  
  188.   wb_motor_set_position(wheels[2], INFINITY);
  189.  wb_motor_set_velocity(wheels[2],speed);
  190.  
  191.   wb_motor_set_position(wheels[3], INFINITY);
  192.  wb_motor_set_velocity(wheels[3],speed);
  193.  }
  194.  
  195.  
  196.     delete webots_joy_demo;
  197.     ros::shutdown();
  198.  
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement