# 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.

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.

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:

```  r'getsensors
```
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 N move forward N steps bk N move backwards N steps lt α turn left α steps (α / 8 degrees) rt α turn right α steps (α / 8 degrees) fdspeed l r set the speeds l, and r on the motors bkspeed l r set the speeds -l, and -r on the motors ltspeed l r set the speeds -l, and r on the motors rtspeed l r set the speeds l, and -r on the motors fdx l r N same as fdspeed, but stop after faster wheel will make N steps bkx l r N same as bkspeed, but stop after faster wheel will make N steps ltx l r N same as ltspeed, but stop after faster wheel will make N steps rtx l r N same as rtspeed, but stop after faster wheel will make N steps pd pen down pu pen up getsensors return sensor state isalive ping the robot getvoltage determine battery voltage level getactivity 1 - in speed cmd, 2 - in position cmd, 3 - idle, 4 - error isdone 1 - idle, 0 - still in position command alwayswait on|off on means that position commands return only after completion (default off). drawtext text size font draw the text of specified size and font (Borland BGI fonts) textsize text size font determine the size of the text in steps pos reset=0|1 return current position (and reset if requested) moveto x y move robot to specified location condition xor and or set the sensor condition satisfied determine condition status of the last command connect port establish connection to the robot setcomparam ... set communication settings getversion firmware version getnum robot installed capabilities tone frequency duration play tone drawcalib draw calibration image calibrate ... calibrate robot based on calibration image gripper command operate the gripper (if attached) server on|off make robot available on the net portnumber num used both when connecting to network robot and by server getport communication port where robot is connected message user msg send text message to user when networking password newpswd change admin password exclusive on|off start or stop single-user exclusive mode (when networking) logoff passwd logoff another user user name passwd specify user and password for login connected returns 1 if robot is connected, otherwise 0 netreport filename setup server logfile location db ... setup database for user registration userfile filename setup userfile location adduser name pswd add new user to userfile deluser name delete user from userfile listusers return list of users wait t wait until position command completes or t ms messages on|off allow system messages to be shown in message boxes logfile filename|off specify logfile location for replay replay filename replay commands stored in file