The turtlebot is a laptop and a kinect on a roomba and serves as an educational
ID: 3602184 • Letter: T
Question
The turtlebot is a laptop and a kinect on a roomba and serves as an educational platform for ROS.
Challenge: Using ROS and C++, create a node to direct a turtlebot to seek out and then drive into a ball in simulation. This ball-seeking behavior should be off by default and be able to be turned on/off with a std_srvs/SetBool ROS service call. Additionally, the node must output the relative displacement to the object if found as a ROS geometry_msgs/Vector3. The behavior should not contain any pre-scripted motions and should work for any pl within the detection range of the kinect. Name your package "seeker", your displacement topic displacement" and your enable service "enable". We recommend calling your node seeker_node, but you are not bound to only one node or one package. ment of the ball as long asthExplanation / Answer
#include <iostream>
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
class RobotDriver
{
private:
//! The node handle we'll be using
ros::NodeHandle nh_;
//! We will be publishing to the "/base_controller/command" topic to issue commands
ros::Publisher cmd_vel_pub_;
public:
//! ROS node initialization
RobotDriver(ros::NodeHandle &nh)
{
nh_ = nh;
//set up the publisher for the cmd_vel topic
cmd_vel_pub_ = nh_.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
}
//! Loop forever while sending drive commands based on keyboard input
bool driveKeyboard()
{
std::cout << "Type a command and then press enter. "
"Use '+' to move forward, 'l' to turn left, "
"'r' to turn right, '.' to exit. ";
//we will be sending commands of type "twist"
geometry_msgs::Twist base_cmd;
char cmd[50];
while(nh_.ok()){
std::cin.getline(cmd, 50);
if(cmd[0]!='+' && cmd[0]!='l' && cmd[0]!='r' && cmd[0]!='.')
{
std::cout << "unknown command:" << cmd << " ";
continue;
}
base_cmd.linear.x = base_cmd.linear.y = base_cmd.angular.z = 0;
//move forward
if(cmd[0]=='+'){
base_cmd.linear.x = 0.25;
}
//turn left (yaw) and drive forward at the same time
else if(cmd[0]=='l'){
base_cmd.angular.z = 0.75;
base_cmd.linear.x = 0.25;
}
//turn right (yaw) and drive forward at the same time
else if(cmd[0]=='r'){
base_cmd.angular.z = -0.75;
base_cmd.linear.x = 0.25;
}
//quit
else if(cmd[0]=='.'){
break;
}
//publish the assembled command
cmd_vel_pub_.publish(base_cmd);
}
return true;
}
};
int main(int argc, char** argv)
{
//init the ROS node
ros::init(argc, argv, "robot_driver");
ros::NodeHandle nh;
RobotDriver driver(nh);
driver.driveKeyboard();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.