Using joy topic to increase/decrease a variable's value continuously
Hi :)
I've just started learning ROS and want to use it for a project of mine at the university. One of the things I'd like to do is to use a joystick to control a cute little KUKA KR3 CR robot using an inverse kinematics cpp file generated by OpenRAVE. I've looked into the example in the ROS wiki about controlling a turtle with a joystick and got it up and running in no time. My configuration is:
- Host: Debian 7.0 Wheezy 64bit
- VirtualBox Guest: Lubuntu 12.04 with ROS Hydro
- Joystick Logitech Attack 3
I'm using a VirtualBox since 1)installing ROS on Debian proved to be a pain in the butt (at least for me) and 2)I like keeping thing separate so that I can possibly transfer my virtual machine to my working PC once I go to the lab in a couple of days. Here is how things are supposed to work:
- Joy_node publishes a joy message
- My node (let's call it JoyControl_node) has subscribed to the topic and receives joy messages
These messages are processed accordingly and the movement of my stick + pressing some of the buttons generates a Transformation message looking like this:
std_msgs/Header header
float32[3] translationXYZ
float32[3] rotationXYZ
JoyControl_node publishes the Transformation message
...
Taking into account the movement of the stick and also a bunch of buttons (sadly the stick has only 2 DoF, which I can only map to either translation along or rotation around two axes so I have to map some of the translation and rotation to the buttons of the joystick) I increase/decrease/don't change at all a set of translations (X,Y and Z) and rotation (around X, Y and Z). These are supposed to be the Cartesian coordinates of a point including its orientation. Note that I do realize that I have to do some additional checking if the coordinates are plausible but this will be done in another node, which uses the inverse kinematics by OpenRAVE. For now just imagine that the results produced here are useful. ;)
The issue I'm having is that I can't figure out how to make the change (increase/decrease of the value of my 6 variables) continuous. Here is my code (read about the TODO after the code section):
#include <ros/ros.h>
#include <sensor_msgs/Joy.h>
#include <JoyControl_node/Transformation.h>
class TeleopTransformation
{
void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
ros::NodeHandle nh;
float transX, transY, transZ;
float rotX, rotY, rotZ;
ros::Subscriber joy_sub;
ros::Publisher transformation_pub;
public:
TeleopTransformation();
};
TeleopTransformation::TeleopTransformation()
: transX(0), transY(0), transZ(0)
, rotX(0), rotY(0), rotZ(0)
{
joy_sub = nh.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTransformation::joyCallback, this);
transformation_pub = nh.advertise<JoyControl_node::Transformation>("teleop_transformation/point", 1);
}
void TeleopTransformation::joyCallback(const sensor_msgs::Joy::ConstPtr &joy)
{
JoyControl_node::Transformation tmsg;
// ROS_DEBUG_STREAM("axis 0: " << joy->axes[0] << "\naxis 1:" << joy->axes[1] << "\naxis 2:" << joy->axes[2]);
// TODO convert movement of the axes to Cartesian coordinates and angles
// ...
tmsg.translationXYZ[0] = transX;
tmsg.translationXYZ[1] = transY;
tmsg.translationXYZ[2] = transZ;
tmsg.rotationXYZ[0] = rotX;
tmsg.rotationXYZ[1] = rotY;
tmsg.rotationXYZ[2] = rotZ;
transformation_pub.publish(tmsg);
}
int main (int argc, char **argv)
{
ros::init(argc, argv, "teleop_transformation");
TeleopTransformation tt;
ros::spin();
}
Previously at the TODO position in my code I had something like
if(joy->axes[0] < 0) transX -= .5;
else if(joy->axes[0] > 0) transX += .5;
...
However I found out that this leads to a very dull way of handling the data that is passed from the joynode to my JoyControlnode. I have to constantly move my joystick in order to trigger a change in my values. This is of course understandable since the callback function is triggered only when it receives a new message. I'm thinking about implementing some kind of a trigger, which starts a thread that increases/decreases the specific variable's value accordingly. I want to mimic the behaviour of the velocity command in the joystick tutorial, where you simply push the stick of your joystick, hold it in a certain position and the turtle continuously moves in the TurtleSim.
Suggestions, criticism etc. are more than welcome! RB
Asked by rbaleksandar on 2015-03-01 18:24:59 UTC
Comments