After Laying out the circuit diagram and the connections to the various outputs, we needed small sections of programming routine so that when the programmer came to assemble the final developed program, small sections of code could be copied and pasted in.
Firstly, we had to devise a code to check that the motor were wired up correctly and that it would turn backwards and forward.
Main:
low 1 `Drive the motor forward
high 2
pause 5000 `Pause for 5 seconds
high 1 `Drive the motor backwards
low 2
pause 5000 `Pause for 5 seconds
goto main `loop to start
Once this was working, the code was copied and applied to 2 motors to make sure that they both worked;
This routine was called "goforward" (obviously)
`To Drive Motor Forwards
High 5 `Left Motor Fowards
Low 4 `Left Motor Forwards
High 6 `Right Motor Forwards
low 7 `Right Motor Forwards
(Note the change in Pins, as this code was run on the fully assembled buggy, whereas the first code was run during the lab sessions during the first couple of weeks)
Once we had this code and sub-system working, we were able to play around witht the outputs of the chip to create smaller subsections of programming that would enable the buggy to turn left and right. Here are the devised codes;
turnright:
`To Turn Right
High 5 `Left Motor Fowards
Low 4 `Left Motor Forwards
`Code To Turn right Motor Backwards
Low 6
High 7
return
And the code to turn left;
turnleft:
` To Turn Left
High 6 `Right Motor Forwards
low 7 `Right Motor Forwards
`Code to run Left Motor Backwards
Low 5
High 4
These blocks of text where then substituted in to the Alecs program.
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
Friday, 30 April 2010
Some Initial Programming
A quick bit of programming to see if the buggy was working once constructed:
Sensor Input Program:
main:
READADC 0,B0
READADC 1,B1
READADC 2,B2
Debug
If b0>100 then b3=1
GOTO main:
Motor Test Program:
Main:
High 0 `Run Motor 1(?) Forwards(?)
Low 1
Pause 3000 `Pause for 3000
Low 0
High 2 `Run Motor 2(?) Forwards(?)
Low 3
Pause 3000 `Pause for 3000
Low 2
Goto Main:
Initial Buggy Program Idea:
Main:
Read 0,B0 `Left Sensor written to Bo
Read 1,B1 `Sensor 2 Written to B1
Read 2,B2 `Right. Sensor written to b2
Low 5 Low 4 Low 6 Low 7
Debug `To check working correctly
If B0=1 Then high 5 Low 4
Else If B2=1 Then high 6 low 7
Endif
Goto Main
Sensor Input Program:
main:
READADC 0,B0
READADC 1,B1
READADC 2,B2
Debug
If b0>100 then b3=1
GOTO main:
Motor Test Program:
Main:
High 0 `Run Motor 1(?) Forwards(?)
Low 1
Pause 3000 `Pause for 3000
Low 0
High 2 `Run Motor 2(?) Forwards(?)
Low 3
Pause 3000 `Pause for 3000
Low 2
Goto Main:
Initial Buggy Program Idea:
Main:
Read 0,B0 `Left Sensor written to Bo
Read 1,B1 `Sensor 2 Written to B1
Read 2,B2 `Right. Sensor written to b2
Low 5 Low 4 Low 6 Low 7
Debug `To check working correctly
If B0=1 Then high 5 Low 4
Else If B2=1 Then high 6 low 7
Endif
Goto Main
Labels:
Alec Kingsnorth,
Duncan Scriven,
Luke Hildred,
Programming
Thursday, 29 April 2010
Programming
The final programming had been completed. The main areas and problems associated with programming are as follows.
- How to read the inputs effectively, especially as we were reading in ADC inputs and how to convert them again into a binary form , so case statements could be used. We managed to do this through reading each of the inputs into a memory location . Then using If statements these could be analysed and if over a certain value a 1 , 2 ,or 4, which in binary is 001 , 010, 100 could be added to a separate memory location , in my case b3. E.G if my left sensor sensed the line and went past a certain value then in b3 would be 100, if my right 001 and middle 010 or any combination of theses , like if all sensed the line 111. With this case statements could be used. It is important however after selecting the case to erase all the values in b3 so when the programs loops the values do not keep adding up.
- How to change between modes, symbol mode and line follower. As we used a toggle switch which was connected to input pin 7 , we just read in all the pins to a memory location and if over a certain value (because pin7 if on would effectively have a value of 128, and thus make any values being read into the input pins over 128) then it would be in symbol mode and if under the value it would be in line follower mode.
- Lack of program memory, it was soon realised after creating my program that the computer would not allow it to download onto the chip because the program took up to much memory, essentially my programming for the motors which I put into each case was making the file way to big. The way I combated this was to use subroutines. I made one for forward , turn right and turn left. These could be called by using GOSUB...turnright and the program would return to its original location by use of the command RETURN. This saved duplicating lots of code, and made my file size much smaller.
The finished program was as follows;
main:
b7=pins ;read in all pins to memory
readadc 0,b0 ;reading all adc inputs to memory
readadc 1,b1
readadc 2,b2
if b7 <10>
goto symmode ;else go to symbol mode
normal:
if b2>100 then let B3=B3+1 ;right sensor, p
endif
if b1>100 then let B3=B3+2 ; middle sensor
end if
if b0>100 then let B3=B3+4 ; left sensor
endif
select B3 ; case statements for the different scenarios
case %00000000; both motors off and return to select
case %00000001 ; right motor off
gosub turnright
case %00000010 ; both motors on and return to select
gosub forward
case %00000011 ; Right motor off
gosub turnright
case %00000100 ; Left hand motor off
gosub turnleft
case %00000101 ; Error, carry on
case %00000110 ; Left hand motor off
gosub turnleft
case %00000111 ; both motors on and return to select
gosub goforward
end select
debug
let b3=%00000000
goto main
symmode:
if b2>100 then let B3=B3+1 ;right sensor
endif
if b1>100 then let B3=B3+2 ; middle sensor
end if
if b0>100 then let B3=B3+4 ; left sensor
endif
select b3
case %00000000 ; straight
gosub goforward
case %00000001 ; forward 90 right
gosub turnright
pause 1000
case %00000010 ; straight
gosub goforward
case %00000011 ; forward 90 right
gosub turnright
pause 1000
case %00000100 ; forward 90 left
gosub turnleft
pause 1000
case %00000101 ; Forward pause reverse
case %00000110 ; Forward 90 left
gosub turnleft
pause 1000
case %00000111 ; Forward pause reverse
end select
debug
let b3=%00000000
goto main
turnright: ; subroutines for driving motors
`To Turn Right
High 5 `Left Motor Fowards
Low 4 `Left Motor Forwards
low 6 `Turn Right Motor Off
return
turnleft:
` To Turn Left
High 6 `Right Motor Forwards
low 7 `Right Motor Forwards
low 5 `Tur Left Motor off
return
goforward:
`To Drive Motor Forwards
High 5 `Left Motor Fowards
Low 4 `Left Motor Forwards
High 6 `Right Motor Forwards
low 7 `Right Motor Forwards
return
Tuesday, 23 March 2010
Case Statements
The following flowchart code is being considered; utilising case statements to direct the robot:
Let b0 = pins
let b1 = 000111 & pins
select b1
case = 000000; both motors off and return to select
case = 000001; Right hand motor on and return to select
case = 000010; both motors on and return to select
case = 000011; Right hand motor on and return to select
case = 000100; Left hand motor on and return to select
case = 000101; Error
case = 000110; Left hand motor on and return to select
case = 000111; both motors on and return to select
The individual case (ie how to drive both motors will be developed in a later blog).
Let b0 = pins
let b1 = 000111 & pins
select b1
case = 000000; both motors off and return to select
case = 000001; Right hand motor on and return to select
case = 000010; both motors on and return to select
case = 000011; Right hand motor on and return to select
case = 000100; Left hand motor on and return to select
case = 000101; Error
case = 000110; Left hand motor on and return to select
case = 000111; both motors on and return to select
The individual case (ie how to drive both motors will be developed in a later blog).
Programming limitations
As the PICAXE only has 5 inputs , and one input must be used to switch the mode of the LFR from line following to symbol recognition, the amount of sensors we planned must be reduced. We are now reducing the sensors to 3 . This is beacuse when we read pins (all the input pins) these are neatly together at the end of the byte (bit 0, bit 1 , bit2) relating to (pin 0 , 1 ,2) so will be easy to program, and there will only have to be 8 different cases. Pin 6 or 7 will have to be tied down and one used for the switch. The only down side to this is that the buggy wont be able to adjust so precisely as it could with more sensors.
IF/ELSE Case Statements
As part of the development of the programming we investigated using the following If/Else flowchart. Eventually we decided against it as it is better programming etiquette to use Case Statements to achieve the same aim.
Let b0 = pins
Debug Pins; This command would indicate what the device 'thinks' to the observer
let b1 = 000111 & pins
If b1 = % 00000000; No Action
Else b1 = 000001; Hard right turn
Else b1 = 000010; Continue straight
Else b1 = 000011; Gentle right turn
Else b1 = 000100; Hard left turn
Else b1 = 000101; Error.
Else b1 = 000110; gentle left trun
Else b1 = 000111; Continue straight
Let b0 = pins
Debug Pins; This command would indicate what the device 'thinks' to the observer
let b1 = 000111 & pins
If b1 = % 00000000; No Action
Else b1 = 000001; Hard right turn
Else b1 = 000010; Continue straight
Else b1 = 000011; Gentle right turn
Else b1 = 000100; Hard left turn
Else b1 = 000101; Error.
Else b1 = 000110; gentle left trun
Else b1 = 000111; Continue straight
Subscribe to:
Posts (Atom)