hand_control/src/keyboard_cmd.cpp

222 lines
5.2 KiB
C++
Raw Normal View History

2015-05-01 17:38:54 +00:00
#include <termios.h>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <ros/ros.h>
#include <ros/time.h>
2015-05-01 21:22:13 +00:00
#include <locale.h>
#include <ncurses.h>
#include "display.h"
2015-05-01 17:38:54 +00:00
#include <std_msgs/Empty.h>
#include <geometry_msgs/Twist.h>
class Run
{
private:
std_msgs::Empty empty;
ros::NodeHandle n;
ros::Rate loop_rate;
ros::Publisher cmd, pub_takeoff, pub_land, pub_reset;
void land()
{ pub_land.publish(empty); };
void takeoff()
{ pub_takeoff.publish(empty); };
void reset()
{ pub_reset.publish(empty); };
float x_speed, y_speed, z_speed, turn;
2015-05-01 22:00:28 +00:00
boost::shared_ptr<Curses> term;
2015-05-01 19:23:00 +00:00
void print_speed() { ; };
2015-05-01 17:38:54 +00:00
public:
2015-05-01 22:00:28 +00:00
Run(boost::shared_ptr<Curses> terminal) :
term(terminal),
2015-05-01 17:38:54 +00:00
loop_rate(30),
x_speed(0.2),
y_speed(0.3),
z_speed(0.5),
turn(0.5) {
cmd = n.advertise<geometry_msgs::Twist>("/cmd_vel",1);
pub_takeoff = n.advertise<std_msgs::Empty>("/ardrone/takeoff", 1);
pub_land = n.advertise<std_msgs::Empty>("/ardrone/land", 1);
pub_reset = n.advertise<std_msgs::Empty>("/ardrone/reset", 1);
}
void operator()()
{
while (ros::ok())
{
geometry_msgs::Twist::Ptr msg(new geometry_msgs::Twist());
msg->linear.x = msg->linear.y = msg->linear.z =
msg->angular.x = msg->angular.y = msg->angular.z = 0.;
2015-05-01 22:00:28 +00:00
char c = term->getchar();
2015-05-01 17:38:54 +00:00
switch(c)
{
case 'k' :
{// hover
cmd.publish(msg);
break;
}
case 'i' :
{// forward
msg->linear.x = x_speed;
cmd.publish(msg);
break;
}
case ';' :
{// backward
msg->linear.x = -x_speed;
cmd.publish(msg);
break;
}
case 'h' :
{//translate left
msg->linear.y = -y_speed;
cmd.publish(msg);
break;
}
case 'm' :
{//translate right
msg->linear.y = y_speed;
cmd.publish(msg);
break;
}
case 'j' :
{//rotate left
msg->angular.z = turn;
cmd.publish(msg);
break;
}
case 'l' :
{//rotate right
msg->angular.z = -turn;
cmd.publish(msg);
break;
}
case 'u' :
{//turn left
msg->angular.z = turn;
msg->linear.x = x_speed;
cmd.publish(msg);
break;
}
case 'o' :
{//turn right
msg->angular.z = -turn;
msg->linear.x = x_speed;
cmd.publish(msg);
break;
}
case ',' :
{//turn left backward
msg->angular.z = turn;
msg->linear.x = -x_speed;
cmd.publish(msg);
break;
}
case ':' :
{//turn right backward
msg->angular.z = -turn;
msg->linear.x = -x_speed;
cmd.publish(msg);
break;
}
case 'y' :
{//up
msg->linear.z = z_speed;
cmd.publish(msg);
break;
}
case 'n' :
{//down
msg->linear.z = -z_speed;
cmd.publish(msg);
break;
}
case 't' :
{//takeoff
takeoff();
break;
}
case 'b' :
{//land
land();
break;
}
case 'g' :
{//reset
reset();
break;
}
case 'a' :
{// + x_speed
x_speed *= 1.1;
print_speed();
break;
}
case 'w' :
{// - x_speed
x_speed *= 0.9;
print_speed();
break;
}
case 'z' :
{// + y_speed
y_speed *= 1.1;
print_speed();
break;
}
case 'x' :
{// - y_speed
y_speed *= 0.9;
print_speed();
break;
}
case 'e' :
{// + z_speed
z_speed *= 1.1;
print_speed();
break;
}
case 'c' :
{// - z_speed
z_speed *= 0.9;
print_speed();
break;
}
case 'r' :
{// + turn speed
turn *= 1.1;
print_speed();
break;
}
case 'v' :
{// - turn speed
turn *= 0.9;
print_speed();
break;
}
default :
break;
} // switch
ros::spinOnce();
loop_rate.sleep();
} // while
} // run
2015-05-01 19:23:00 +00:00
2015-05-01 17:38:54 +00:00
}; // class
int main(int argc, char** argv)
{
2015-05-01 21:22:13 +00:00
setlocale(LC_ALL, "");
2015-05-01 17:38:54 +00:00
ros::init(argc, argv, "keyboard_cmd");
2015-05-01 22:00:28 +00:00
boost::shared_ptr<Curses> term(new Curses());
Run fun(term);
fun();
2015-05-01 17:38:54 +00:00
return 0;
}