Translations of this page?:

Before running the node, do one of this two things otherwise the robot could crash:

  • Run the script with the Qbo on top of an object, where the wheels don't touch the floor.
  • Use a ssh console with a great wireless signal(If not great there could be lag that make the robot move more than you wish).

Introduction

This is a guide of how to create a app for the Qbo robot. To do this the we recommend you to start by reading the ROS tutorials. It is very important to have basic knowledge about how topics and services works.

Python have been chosen as language for this tutorial because it is so easy to use.

All the steps have to be written in a terminal. So start one before starting or connect to the robot using ssh.

Preparing the files

Because of Linux policy the normal user don't have permissions to modify ROS directories and files. In this example we will develop the node in the qbo_stacks, so we only will update permission of this stack:

chown -R qbobot:qbobot /opt/ros/electric/stacks/qbo_stack/

To start lets go to the directory qbo_stack:

cd /opt/ros/electric/stacks/qbo_stack

To create the work directory we will use roscreate-pkg command. The new node will be used to move the qbo base with the keyboard:

roscreate-pkg qbo_base_manual_control
cd qbo_base_manual_control

Coding

Lets create the src (source code) directory and start coding:

mkdir src
cd src
vi manual_control.py

The code in this case is this:

#!/usr/bin/env python
 
#Import ROS dependencies 
import roslib; roslib.load_manifest('qbo_base_manual_control')
import rospy
 
#Import ROS message to publish movements
from geometry_msgs.msg import Twist
 
#Import other libraries
import time
import sys, tty, termios
 
 
#Publish a message to move the robot 
def move(publisher,linear,ang):
    speed_command=Twist()
    speed_command.linear.x=linear
    speed_command.linear.y=0
    speed_command.linear.z=0
    speed_command.angular.x=0
    speed_command.angular.y=0
    speed_command.angular.z=ang
    publisher.publish(speed_command)
 
 
 
if __name__ == "__main__":
    #Init the node
    rospy.init_node('qbo_base_manual_control')
 
    #Init the publisher
    pub=rospy.Publisher('/cmd_vel', Twist)
 
    #Prpeare the keyboard input
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
 
    #Start the loop to read orders and move the robot
    print "Start"
    while 1:
        #Read from keyboard
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
 
        #Depending of the key the robot will move in diferent direccion
        if ch=='w' or ch=='W': #Forewards
            move(pub,0.3,0)
        elif ch=='s' or ch=='S': #Backwards
            move(pub,-0.3,0)
        elif ch=='a' or ch=='A': #Left
            move(pub,0,1)
        elif ch=='d' or ch=='D': #Right
            move(pub,0,-1)
        else:
            move(pub,0,0) #Stop and quit loop
            break
        #Wait until read other keyboard, because for each message the robot will be move for a second
        time.sleep(0.5)
 
    #Restore Keyboard input config
    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

Preparing to Execute

Ros Dependencies

This step can be done before or after coding. I do it after coding because at start I didn't knew which packages I will need.

This is my manifest file:

<package>
  <description brief="qbo_base_manual_control">

     qbo_base_manual_control

  </description>
  <author>qbobot</author>
  <license>BSD</license>
  <review status="unreviewed" notes=""/>
  <url>http://ros.org/wiki/qbo_base_manual_control</url>
  <depend package="rospy"/>
  <depend package="geometry_msgs"/>
</package>

As you can see I only added the two depend lines.

Execute permission

Python is a script language so we don't need to compile it but to execute the script with rosrun it is needed to add to it execution permission:

chmod +x manual_control.py

Update ROS

To force ROS to read the new node information we have to run this command:

rospack profile

Using

Before running the node, do one of this two things otherwise the robot could crash:

  • Run the script with the Qbo on top of an object, where the wheels don't touch the floor.
  • Use a ssh console with a great wireless signal(If not great there could be lag that make the robot move more than you wish).

To run the new node write this:

rosrun qbo_base_manual_control manual_control.py

Now you can move the robot using:

  • W/w(Forward)
  • S/s(Backward)
  • A/a(Left)
  • D/d(Right)
  • Any other letter will close the program.
software/createfirstapp.txt · Last modified: 2012/12/10 08:23 by sermelo