root / mikrokopter / mikrokopter / src / keyboard_control.cpp @ 98711613
History | View | Annotate | Download (2.5 KB)
1 | 8ecb9700 | Alex Zirbel | /**
|
---|---|---|---|
2 | * @brief A node responsible for keyboard commands which will be sent to
|
||
3 | * the MikroKopter
|
||
4 | *
|
||
5 | * @author CMU Quadrotor Project
|
||
6 | * @author Alex Zirbel
|
||
7 | */
|
||
8 | |||
9 | #include <ros/ros.h> |
||
10 | #include <signal.h> |
||
11 | #include <termios.h> |
||
12 | #include <stdio.h> |
||
13 | dd56aeef | Tom Mullins | #include <ncurses.h> |
14 | 8ecb9700 | Alex Zirbel | |
15 | 493cca7c | Chris Burchhardt | #include "nav_lib.h" |
16 | |||
17 | 8ecb9700 | Alex Zirbel | #define KEYCODE_R 0x43 |
18 | #define KEYCODE_L 0x44 |
||
19 | #define KEYCODE_U 0x41 |
||
20 | #define KEYCODE_D 0x42 |
||
21 | #define KEYCODE_Q 0x71 |
||
22 | |||
23 | class KeyboardControl |
||
24 | { |
||
25 | public:
|
||
26 | KeyboardControl(); |
||
27 | void keyLoop();
|
||
28 | |||
29 | private:
|
||
30 | ros::NodeHandle n; |
||
31 | }; |
||
32 | |||
33 | KeyboardControl::KeyboardControl() |
||
34 | { |
||
35 | } |
||
36 | |||
37 | void KeyboardControl::keyLoop()
|
||
38 | { |
||
39 | 493cca7c | Chris Burchhardt | |
40 | ros::NodeHandle n; |
||
41 | ros::Publisher pub = n.advertise<mikrokopter::Control>("/mikrokopter/req_set_control", 100); |
||
42 | |||
43 | dd56aeef | Tom Mullins | MikrokopterControl control; |
44 | 493cca7c | Chris Burchhardt | |
45 | ros::Rate loop_rate(25);
|
||
46 | |||
47 | 8ecb9700 | Alex Zirbel | char c;
|
48 | |||
49 | // get the console in raw mode
|
||
50 | dd56aeef | Tom Mullins | initscr(); |
51 | timeout(0);
|
||
52 | noecho(); |
||
53 | cbreak(); |
||
54 | 8ecb9700 | Alex Zirbel | |
55 | puts("Reading from keyboard");
|
||
56 | puts("---------------------------");
|
||
57 | puts("Use arrow keys to command the quadrotor.");
|
||
58 | |||
59 | dd56aeef | Tom Mullins | while(ros::ok())
|
60 | 8ecb9700 | Alex Zirbel | { |
61 | // get the next event from the keyboard
|
||
62 | dd56aeef | Tom Mullins | c = getch(); |
63 | 8ecb9700 | Alex Zirbel | |
64 | //ROS_INFO("key press: 0x%02X\n", c);
|
||
65 | //ROS_INFO("key press: %c\n", c);
|
||
66 | |||
67 | switch(c)
|
||
68 | { |
||
69 | case KEYCODE_L:
|
||
70 | case 'a': |
||
71 | 493cca7c | Chris Burchhardt | ROS_INFO("A");
|
72 | dd56aeef | Tom Mullins | control.left(); |
73 | 8ecb9700 | Alex Zirbel | break;
|
74 | case KEYCODE_R:
|
||
75 | case 'd': |
||
76 | 493cca7c | Chris Burchhardt | ROS_INFO("D");
|
77 | dd56aeef | Tom Mullins | control.right(); |
78 | 8ecb9700 | Alex Zirbel | break;
|
79 | case KEYCODE_U:
|
||
80 | case 'w': |
||
81 | 493cca7c | Chris Burchhardt | ROS_INFO("W");
|
82 | dd56aeef | Tom Mullins | control.forward(); |
83 | 8ecb9700 | Alex Zirbel | break;
|
84 | case KEYCODE_D:
|
||
85 | case 's': |
||
86 | 493cca7c | Chris Burchhardt | ROS_INFO("B");
|
87 | dd56aeef | Tom Mullins | control.backward(); |
88 | 8ecb9700 | Alex Zirbel | break;
|
89 | d58012b5 | Chris Burchhardt | case ' ': |
90 | ROS_INFO("space");
|
||
91 | control.level(); |
||
92 | break;
|
||
93 | 8ecb9700 | Alex Zirbel | case 'r': |
94 | ROS_INFO("UP");
|
||
95 | 493cca7c | Chris Burchhardt | //set_throttle(&pub, );
|
96 | 8ecb9700 | Alex Zirbel | break;
|
97 | case 'f': |
||
98 | ROS_INFO("DOWN");
|
||
99 | 493cca7c | Chris Burchhardt | //set_throttle(&pub);
|
100 | 8ecb9700 | Alex Zirbel | break;
|
101 | 0d1f85b5 | Chris Burchhardt | default:
|
102 | control.level(); |
||
103 | 8ecb9700 | Alex Zirbel | } |
104 | |||
105 | dd56aeef | Tom Mullins | control.publish_on(pub); |
106 | ros::spinOnce(); |
||
107 | loop_rate.sleep(); |
||
108 | 8ecb9700 | Alex Zirbel | } |
109 | dd56aeef | Tom Mullins | |
110 | endwin(); |
||
111 | 8ecb9700 | Alex Zirbel | return;
|
112 | } |
||
113 | |||
114 | int main(int argc, char** argv) |
||
115 | { |
||
116 | ros::init(argc, argv, "keyboard_control");
|
||
117 | KeyboardControl teleop_turtle; |
||
118 | |||
119 | // This commands loops infinitely
|
||
120 | teleop_turtle.keyLoop(); |
||
121 | |||
122 | return(0); |
||
123 | } |
||
124 |