Project

General

Profile

Statistics
| Branch: | Revision:

root / mikrokopter / src / joystick_control.cpp @ fd29b28e

History | View | Annotate | Download (1.46 KB)

1
/**
2
 * @brief A node responsible for reading joystick messages from ros
3
 *        joystick_drivers to control the MikroKopter
4
 *
5
 * @author CMU Quadrotor Project
6
 */
7

    
8
#include <ros/ros.h>
9
#include <sensor_msgs/Joy.h>
10
#include <stdio.h>
11

    
12
#include "nav_lib.h"
13

    
14
class JoystickControl
15
{
16
    public:
17
        JoystickControl();
18
        void joyLoop();
19
        void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
20

    
21
    private:
22
        MikrokopterControl control;
23
        ros::Publisher pub;
24
        ros::NodeHandle n;
25
        ros::Subscriber sub;
26
};
27

    
28
JoystickControl::JoystickControl()
29
{
30
  pub = n.advertise<mikrokopter::Control>("/mikrokopter/req_set_control", 100); 
31
  sub = n.subscribe<sensor_msgs::Joy>("joy", 100,
32
      &JoystickControl::joyCallback, this);
33
}
34

    
35
void JoystickControl::joyLoop()
36
{
37
    ros::Rate loop_rate(25);
38
    while(ros::ok())
39
    {
40
        ros::spinOnce(); // if joyCallback is called, it will be in here
41
        control.publish_on(pub);
42
        loop_rate.sleep();
43
    }
44
    return;
45
}
46

    
47
void JoystickControl::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
48
{
49
    control.velocity_control(joy->axes[1], -joy->axes[0]);
50
    control.set_yaw(-joy->axes[2]);
51
    control.set_thrust((joy->axes[3]+1)/2); ///Adjust thrust to be between [0, 1] instead of [-1, 1]
52
    return;
53
}
54

    
55
int main(int argc, char** argv)
56
{
57
    ros::init(argc, argv, "joystick_control");
58
    JoystickControl joy_control;
59

    
60
    // This commands loops infinitely
61
    joy_control.joyLoop();
62

    
63
    return 0;
64
}