Basic use of Robot component

This short article gives you a quick start to using the robot.ocx component to control real or virtual robots from Imagine (or other languages).

If your computer is ready to be used with Robotnacka, the first step is to create a robot object and then connect to robot through the virtual BlueTooth serial port:

  new "oleobject [comname robot name r]
  r'connect "com4
Now you can send commands to the robot as if you were controlling a turtle. Note however, that these commands will not control any turtle on the screen, they only control the robot directly. (However, if you load robot.imt, which contains class RobotTurtle, it will entangle the robot with a turtle on the screen). For example, you can issue the following commands (for a complete list, see below):
  r'pu	r'fd 1000
  r'lt 720     ; corresponds to 90 degrees
  r'pd	r'bk 1000

About steps of robot and turtles
As you see, the lengths given to fd and bk commands are much bigger than those used to control turtle on the screen. The reason for this is simple: for turtles on the screen, one step corresponds to one pixel of the screen. A typical screen would have from about 600 to about 1000 pixels in height. However, the robot can move in very precise steps - about 5 steps per millimeter! That means 1000 steps would still be only some 20 cm.

The robot is not limited to simple turtle-like movements. It can also draw circles, and arcs of almost an arbitrary radius. Drawing arcs is achieved by setting different constant speeds on both robot wheels. For example, the following command:

  r'fdspeed 10 40
will make the robot start moving forward, while its left wheel will be moving with a quarter speed compared to its right wheel.

Math about the circular movements
In our example, the length of the arc followed by the right wheel (l2) will be 4-times greater than the length of the arc followed by the left wheel (l1), because the right wheel is turning 4-times faster, and the distance travelled is proportional to speed. To see what will be the radius of the circle (r) drawn by the robot, we can compute it as follows (the distance of the pen from both wheels is 90 mm):
  l1 = 2π.(r - 90)     
  l2 = 2π.(r + 90)            
  l2 = 4.l1

  2π.(r + 90) = 8π.(r - 90)
       r + 90 = 4r - 360
            r = 150 mm
In a similar fashion, the command:
  r'bkspeed 50 50
will make the robot start moving backwards with half speed, and the command:
  r'ltspeed 10 40
will make Robotnacka draw a circle with a radius 54 mm.

More math about circular movements
In this example, the left wheel of the robot will be following a circle in backward direction, while its right wheel will follow a greater equicentric circle in forward direction. To compute the radius, we combine the three facts: 1) wheels distance is 180 mm, 2) the speed (and thus the distance travelled as well as the circle radius) is in the ratio 1:4, and 3) the pen is in the middle of the wheels, and thus the radius of the circle drawn by the pen added with the radius of the backwards circle will be half the distance of the wheels, 90 mm:
  180 = r1 + r2	   r2 = 4 r1
  180 = 5 r1
  r1  = 36,        r2 = 144	      
  r + r1 = 90      r = 90 - 36 = 54 mm
Finally the command:
  r'rtspeed 0 50
will make the robot turn half the speed backwards around its left wheel, which will remain at the same place.

The speed commands normally execute until you stop the robot by setting a speed 0 on both wheels (fdspeed 0 0). Alternately, you can use the combined commands, where the number of steps to perform is limited (r'fdx, r'bkx, r'ltx, r'rtx) and thus draw circular arcs of a specific length.

When moving the robot around the arena, it would be interesting to know its location. This is not possible with perfect accuracy because no robot in the World could avoid its wheels slipping on the floor surface, and no factory in the World would produce so precise robot that would not loose some angular seconds when moving forward or turning at the spot. Nevertheless, robot component keeps track of the exact estimated location of the robot. You can obtain it using the command pos. To print it to the text screen, say:

  print r'pos 0
It will print both the location (x and y coordinates) and the heading of the robot.

Sometimes it might be suitable to reset the coordinates to [0,0],0, and the command

  r'pos 1
does just that (and returns the previous coordinates as well).

The robot is equipped with 6 sensors, designed to avoid falling off the edge of a table. We can use them also to determine if the floor surface is white or black - for example, the robot can stop when it will reach some geometric figure placed on the floor. The sensor values (which are always 0 and 1 for each sensor - and these bits are packed into one 6-bit number) can be determined by getsensors operation, for example to print the current sensor readings say:

The robot can be programmed to react to a particular sensor configuration using condition command. For example:
  r'condition 0 63 0
will make the robot watch the edge of the table, or:
  r'condition 16 0 16
will make a robot that follows a black line stop when the line ends. To learn what the parameters of this command mean, and about many other commands that the robot component provides, please read the ReadMe.txt file.

Quick robot command reference

fd Nmove forward N steps
bk Nmove backwards N steps
lt αturn left α steps (α / 8 degrees)
rt αturn right α steps (α / 8 degrees)

fdspeed l rset the speeds l, and r on the motors
bkspeed l rset the speeds -l, and -r on the motors
ltspeed l rset the speeds -l, and r on the motors
rtspeed l rset the speeds l, and -r on the motors

fdx l r Nsame as fdspeed, but stop after faster wheel will make N steps
bkx l r Nsame as bkspeed, but stop after faster wheel will make N steps
ltx l r Nsame as ltspeed, but stop after faster wheel will make N steps
rtx l r Nsame as rtspeed, but stop after faster wheel will make N steps

pdpen down
pupen up

getsensorsreturn sensor state
isaliveping the robot
getvoltagedetermine battery voltage level
getactivity1 - in speed cmd, 2 - in position cmd, 3 - idle, 4 - error
isdone1 - idle, 0 - still in position command
alwayswait on|offon means that position commands return only after completion (default off).

drawtext text size fontdraw the text of specified size and font (Borland BGI fonts)
textsize text size fontdetermine the size of the text in steps
pos reset=0|1return current position (and reset if requested)
moveto x ymove robot to specified location

condition xor and orset the sensor condition
satisfieddetermine condition status of the last command

connect portestablish connection to the robot
setcomparam ...set communication settings
getversionfirmware version
getnumrobot installed capabilities

tone frequency durationplay tone

drawcalibdraw calibration image
calibrate ...calibrate robot based on calibration image

gripper commandoperate the gripper (if attached)

server on|offmake robot available on the net
portnumber numused both when connecting to network robot and by server
getportcommunication port where robot is connected
message user msgsend text message to user when networking
password newpswdchange admin password
exclusive on|offstart or stop single-user exclusive mode (when networking)
logoff passwdlogoff another user
user name passwdspecify user and password for login
connectedreturns 1 if robot is connected, otherwise 0
netreport filenamesetup server logfile location
db ...setup database for user registration

userfile filenamesetup userfile location
adduser name pswdadd new user to userfile
deluser namedelete user from userfile
listusersreturn list of users

wait twait until position command completes or t ms
messages on|offallow system messages to be shown in message boxes

logfile filename|offspecify logfile location for replay
replay filenamereplay commands stored in file