Robotics on the QL 2 - David Buckley
5 October 2006
Updated to 15 February 2007
Last month I introduced my Zero2 Turtle and my QL program to control it. So how do we control a Turtle, in effect a robot vehicle. Well Zero2 was conceived in the days when embeded microcontrollers were not so easy to use as nowadys so Zero2 doesn't use one. The controlling computer (a QL, or any other PC with a serial port, eg a Spectrum) sends bytes at 4800 baud which are received by Zero2 with an onboard 6402 UART, the parallel output of which is routed through some inverters, latches and darlington drivers to control the stepper motor for each wheel and the stepper motor for the pen, as well as the lights and horn.
Figure 1
Figure 1 shows the main components of Zero2 and shows the items mentioned above.
Zero 2 is very easy to program, the serial port (either RS232 or RS423) must be to 4800 baud one stop bit and no parity.
Every function of Zero2 can be controlled and monitored by the computer.
To communicate directly, it must be told which device is to be involved, and what that device is to do. Each device has a numeric address, to which the control data is sent.
Valid addresses are from 0 to 7, though not all addresses have anything
there.
Address Effector device Sensor device 0 Drive motors Line/Edge detectors 1 Pen motor Left bumper 2 Indicators Right bumper 3 Speech 4 Speech/Sound 5 SoundOnce you have identified the address of the device you wish to control, you need to send this number together with the data to the robot. This is sent as one value, The address is in the high nibble and the data in the low nibble, ie multiplying the address by 16 and add the data.
Address = 2 x 16 = 32 Data = 5+ Value sent to port = 37The data to be sent varies for each device, as can be seen by a quick look at the next section. This is because each of the controls in the robot is connected to one of four electronic switches, each of which can be on or off. The data you send decides which switches are on and which are off. The following table shows which switches are on for each of the data values (1 means on, 0 means off):
Data Switch Data Switch 3210 3210 0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111Ie simple binary encoding.
Each motor is controlled by two switches, switches 2 and 3 for the left motor, switches 0 and 1 for the right motor. To make each motor move forwards you need to send instructions in the order 01,00,10,11. By checking the above table, you can see that to move the right motor only, you can send the data as 1,0,2,3. To drive the left motor only, you send 4,0,8,12. To drive both motors forward send 5,0,10,15. To drive the motors backwards send the data in the reverse order. To turn the motors in opposite directions send 6,0,9,15 or 9,0,6,15. This is a lot of numbers, but on closer examination thay are all obtained from the original 1,0,2,3 and 4,0,8,12 by adding the value for the step position of one motor to that for the other. You start at one end for one direction, and the opposite end for the other.
1 0 2 3 ( 3) 2 0 1 3 + 4 0 8 12 (12) 4 0 8 12 ---------- ---------- 5 0 10 15 6 0 9 15Each time you send a data code to address 0, one or both motors will move one step. If the data is out of sequence the motors will not operate properly and will probably just judder. Each step moves the wheel by 0.5mm.
Alternatively to move 4 steps at a time, then by always ending on 15 the codes will remain in sequence. I.e.
forward - 5,0,10,15, backward - 10,0, 5,15, right - 6,0, 9,15, left - 9,0, 6,15.The pen is controlled by the same kind of motor as the wheels, and the same codes are used to control it, except that only switches 0 and 1 are involved. One step of the motor is 1/48 of a complete turn. Normally you will want to move the pen motor half way round each time which is 24 steps, so you would send the full sequence of steps six times. Remember that the pen is at address 1, ie the high nibble of the data=1.
The LEDs and horn are connected to the switches at address 2 as follows;
Green LED Switch 0 Red LED Switch 1 Low Horn Switch 2 High Horn Switch 3To switch on the red LED, for instance, you need to send the 'on' code for switch 1. Unfortunately, if you send just that, you will switch off the other LED and horn if they were on. Every time you send a value to an address it replaces the previous value. You must keep a note of what was previously sent to the address and make allowances when sending fresh data. Assume the low horn and green LED are already on, and you want to turn on the red LED. The last data sent to address 2 was 5 (0101 - see table). The code for red LED on is 2 (0010), Add these numbers to get 7 (0111) which will give you the new data. Add on the address x 16, send it to the robot and on comes the red LED. Now, suppose you want to switch off the green LED. The code for green LED on is 1. Subtract this from the last data value of 7 to get the new data.
Each time a value is sent to a robot address. Zero 2 will send back a code
relating to any sensors connected to that address. Address 0 can have up
to six switches connected to it but the other addresses can only have up
to four each.
Only address 0 has sensors connected, these being the three
line follower sensors, connected to switches 0 to 2 (switch 3 is
always off).
Whenever either drive motor is stepped, the line follower sensors will send back a value from 0 to 7 which can be used to allow Zero 2 to follow a line. A value of 0 means that all the sensors see a dark surface, while 7 means they all see a bright one.
Most BASICs of the time were fairly slow when dealing with the real world, so rapid response were never a problem when programming Zero 2 directly from BASIC. When running the Zero2 Basic Control-program under QPC2 under XP some strange things seem to happen with the Serial port and its buffer and Zero2 runs slow and judders. Quitting and rerunning the program clears the problem as does quitting and restarting QPC2. This never happened on a QL. A very simple program to drive the robot forward 100 steps is given below, assuming you have already set the baud rate:
100 zchan=3 :REMark for serial port to robot seri1 110 baud_rate=4800 120 OPEN #zchan,seri:BAUD baud_rate 130 DIM mtrcmnd(4) 140 mtrcmnd(1) =5: mtrcmnd(2) =0: mtrcmnd(3) =10: mtrcmnd(4) =15 150 FOR dist=1 TO 100/2 :REMark because 4 steps are 2mm 160 FOR step=1 T0 4 170 PRINT #zchan, CHR$( mtrcmnd(step) ); 180 NEXT step 190 NEXT dist 200 END Note the ';' at the end of the print statement to prevent a LF being sent. LF=10 which will upset the sequence of codes sent to the motors. When writing to Zero 2 each device, or device group, has an address which forms the first three bits of the upper nibble. These are allocated as follows. Address Device Data bit allocation 0 Drive Motors D3 D2 D1 D0 left right (port) (starboard) motor motor 1 Pen D3 D2 D1 D0 for future Pen lift allocation motor 2 Indicators D3 D2 D1 D0 Horn Horn Left Right High Low LED LED Tone Tone 3-7 for future for future allocation use In a small Stepper motor the control sequence codes are normally: Binary Decimal 0101 5 1001 9 1010 10 0110 6 0101 5 In Zero2 this is generated from two data lines with two inverters * * I*I* (I=inverted signals) 1 1 3 0101 5 0 1 1 1001 9 0 0 0 giving 1010 10 1 0 2 0110 6 1 1 3 0101 5 Each full motor step results in the robot travelling 0.5mm A two bit data code stream of 1,0,2,3,1,0,2,3,.... will cause either drive motor to move the vehicle forwards. so FD mL mR each wheel moves 2mm, ie move 2mm forward 1 1 0101 5 0 0 0000 0 2 2 1010 10 3 3 1111 15 BK mL mR each wheel moves 2mm, ie move 2mm backward 2 2 1010 10 0 0 0000 0 1 1 0101 5 3 3 1111 15 RT mL mR each wheel moves 2mm, ie rotation of 2deg. 1 2 0110 6 0 0 0000 0 2 1 1001 9 3 3 1111 15 LT mL mR each wheel moves 2mm, ie rotation of 2deg. 2 1 1001 9 0 0 0000 0 1 2 0110 6 3 3 1111 15 RF mL mR only left wheel moves forward 2mm, ie rotation of 1deg. 1 3 0111 7 0 3 0011 3 2 3 1011 11 3 3 1111 15 LF mL mR only right wheel moves forward 2mm, ie rotation of 1deg. 3 1 1101 13 3 0 1100 12 3 2 1110 14 3 3 1111 15 Pen spare pen address=1 0 1 0001 1 +16 17 0 0 0000 0 +16 16 0 2 0010 2 +16 18 0 3 0011 3 +16 19 repeating the sequence 6 times will move the pen cam through 180deg. Indicator Control A data bit high causes that particular function to be ON. eg set horn and right LED off and Left LED on address Indicators required code 2 D3 D2 D1 D0 binary dec. Horn Horn Left Right High Low LED LED Tone Tone 10 0 0 1 0 100010 34 Reading from Zero2 When reading data from the robot, bits zero to three (D0, D1, D2, D3) contain the status data of the sensor device selected by the address of the previous WRITE while bit six (D6) is the wireORed ready line. In addition at address zero, bit 5 (D5) in conjunction with bit 4 (D4) can contain the hole sensor information. Address Device Data bit allocation D4 D3 D2 D1 D0 0 Line follower left centre right 0 Hole detector left right 1 Fenders right back side corner front 2 Fenders left back side corner front Line Follower and Hole Detectors A data bit high indicates a bright surface while a data bit low indicates a dark surface. For D4, D3 a dark surface is synonymous with a hole. Fenders A data bit high indicates an obstacleNext time we will look at the program.