Home    Worksheets    Subroutines   

Worksheet 104:
Running in a straight line

In this worksheet you will learn how to adjust the speeds of the motors to steer a straight line.

You'll end up with a small piece of program to be re-used when you need to send the buggy forwards in a straight line.  This is subroutine " fwd ".

1) Start up the PICAXE Programming Editor program:  click here
 
  - Make sure that the following options are selected (in View / Options...)
  - PICAXE-18x   (Mode tab)

  - Text-mode Colour syntax   (Editor tab)
  - Compiler-Enhanced   (Editor tab)
  - You should see the BASIC page. (if not, Click the Basic button.. )
 

2) Get a Copy of the subroutine "swonoff":
Click  here to go to the subroutines page.
Use the Copy to clipboard button to copy the subroutine, and then paste it into Programming Editor.
(Use the Back button to return to this page.)

3) Copy the following BASIC program in Programming Editor:      
It is supposed to wait for the right hand switch to be pressed and then go forwards for 10 seconds.
 Copy using the "copy" button below and paste into Programming Editor.
Put this main program above the subroutine, so it's at the start of the program.
 
'---------------------------------------.
main:  gosub swonoff         '          ^
'--------------------------- '          ^
       let pins=%01010000    ' forwards ^
       pause 10000           ' wait 10s ^
       let pins=0            ' stop     ^
       goto main             ' loop -->-^
'------------------------------
  Flowchart
 
This program goes forward for 10 seconds each time the switch is pressed and released
4) Download the program to the buggy:
  - connect up the download lead to the socket at the back of the buggy.
  - switch on the buggy,
  - Click on PICAXE/Run or click the button. (or use F5)

The PIC chip is now running the program.
Run the Buggy on the baseboard along one of the straight paths marked.
Press the microswitch and release it to see the action.
 

5) Evaluation: Did it go in a straight line?
- Yes : well you're lucky! - most buggies don't.
            You will be able to use subroutine fwd from the subroutines page
            with no steering correction.
- No:  you'll have to adjust the program to straighten things up
          Question: what do NASA do when a spaceship is off course? (click here for answer)


6) Solution 1: Here's an alternative program with a spin in the middle to correct the course:
 
'----------------------------------------------
main:  
      gosub swonoff         '              ^
'---------------------------               ^
      let pins=%01010000    ' forwards     ^
      pause 5000            ' 5 seconds    ^

      let pins=%YYYY0000    ' spin left or right
      pause 500             ' 1/2 second   ^

      let pins=%01010000    ' forwards     ^
      pause 5000            ' 5 seconds    ^

      let pins=0            ' stop         ^
      goto main             ' loop -->-->-->
'---------------------------
                                             
   Flowchart
This program 
- goes forwards for 5 seconds, 

- then spins (right or left) a little bit, 

- then goes forward for another 5s
.
 
Set YYYY for a left or right spin to suit your buggy
 
The "pause 500" controls the amount of time spent spinning.

Copy this to Programming Editor. 
You'll have to change the "
let pins=%YYYY0000" to either a spin left or a spin right
  - according to what your buggy needs.

When You've worked out the pins pattern to use,
 record it in the "Straight line (fwd)" section of your Buggy Journal.

Download this program to see how it affects the buggy's movement.

7) Adjustment: Does the program now go exactly on the path you want?
- Yes :  Go on to the next step
- No:  Adjust the timing. (click here for details)

8) Refinements:  make the corrections smaller and more frequent
Now reduce the delays so that instead of 1 big correction every 10 seconds,
 - it does little ones -  one every second:

 
'-------------------------------------------
main:
       gosub swonoff        '              ^
'---------------------------               ^
       let pins=%01010000   ' forwards     ^
       pause 1000           ' 1 seconds    ^

       let pins=%YYYY0000   ' spin left/right
       pause 100            ' 0.1 second   ^

       let pins=0           ' stop         ^
       goto main            ' loop -->-->-->
'---------------------------------
 
This version goes forward for only 1 second each time the switch is pressed and released.

Keep pressing the switch to see the full path.

9) Final Version:  make the corrections happen 10 times a second!
Divide the pauses by 10 again so that corrections are made very rapidly
  - this removes all the corners and makes it a smooth path.
    (click here is you're having problems with the small pause value)

I've changed the final loop so that you don't have to keep pressing the microswitch to make it move.
You should end up with this:

 
'-------------------------------'
main:    gosub swonoff          '              
'-------------------------------               

moveloop:
      '--------------------------------
      let pins=%01010000   ' forwards      ^
      pause 100            ' 0.1 seconds   ^

      let pins=%YYYY0000   ' spin left/right
      pause 5              ' 0.05 seconds  ^

      let pins=0           ' stop          ^
      goto moveloop        ' loop >-->-->--^
'------------------------------------------'
   
This version goes forward steadily after the switch is pressed and released.

It corrects the path 10 times a second

  (click here is to see why we need to correct 10 times a second)

Download this version to your Buggy.
Keep adjust the timing (pause), and downloading, until it goes in a smooth straight line.
It should follow the path exactly across the baseboard.

When You've worked out the pause value,  ( It may be 2.5  or 3.5 etc)
 record it in the "Straight line (fwd)" section of your Buggy Journal.
Make sure the pins pattern in your journal (for the spin) is up-to-date.


10) In future worksheets:
We'll be using subroutine "fwd" to move the buggy forwards a short distance in a straight line.

Click here to see the flowchart on the Subroutines page.

We using Subroutines for 3 reasons:
1 - to avoid having to re-type the same bits of program,
2 - to gather together bits of program that do a useful job,
3 - to simplify the main program and make it easier to understand.


In BASIC, we put the subroutines at the end of the program.
 

 

Assessment :
Before  you've finished this worksheet, you need to show the teacher your work.
Make sure you've filled in the "fwd" section of your Buggy Journal.


1) Have your Buggy journal ready, so that the teacher can mark your work.

 

Content Scl Version 7/6/07