Program description

What does will it do?

This program will run the CHRP robot as a line follower. After completing the previous programming assignments, you know enough about the CHRP board and how to program it to make a fully autonomous, line-following robot.

CHRP Robot program

This activity will guide you through making a program that will allow a CHRP-based robot to follow a black line (typically electrical tape on a white or light-coloured floor).

The CHRP board is designed to use two downward-facing light sensors (phototransistors Q1 and Q2, illuminated by LED12 — see the schematic diagram for details) to follow a line using a simple digital control scheme. The program will make decisions about which motor to turn on or off by sensing the light reflected from the floor, or the tape, as either light or dark. While this control scheme does successfully follow the line, it typically results in jerky movements as the robot corrects its course.

An easy to build, inexpensive and simple robot platform can be put together using DC motors and 1/2" MDF.

CHRPbot-topCHRPbot-axle-detail

What you should know before starting

Getting prepared

Make sure that your motor driver and motors are tested and working. Use the Output program to determine the values that need to be written to the motor driver to make the motors run forward, reverse, left and right. Having the left and right directions work by stopping one motor and running the other works better than reversing the direction of one motor (since running motors in opposite directions stops all forward travel).

Also, be sure that the floor LED (LED12) and the phototransistors (Q1 and Q2) have been installed, aligned and tested. Use the InputA program to verify that your phototransistors work and are properly aligned. Test your robot on a tape line to check alignment. As the robot os moved across the line, from one side to the other, one light sensor should 'see' the line first, then both, then the other light sensor, and finally, none of the sensors see the line. Check that this sequence works from left to right and right to left across the line (and acts symmetrically, independent of the direction of motion). If the transition sequence is asymmetrical, adjust the position or aim of either LED12 or the Q1 and Q2.

Thinking about the program

Before you write any code, think about the possible input states resulting from using two light sensors (2^2 = 4 states). It is important to consider exactly what the robot should be doing in response to each possible input state.

State 1 - On the line

If both line sensors sense the line, the robot should go forward.

State 2 - On the right edge of the line

When the left sensor is still on the line, but the right sensor is off the line, the robot should turn left.

State 3 - On the left edge of the line

Similar, but opposite to state 2, the robot should now turn right.

State 4 - Entirely off the line

If both light sensors do not sense the line, a number of courses of action could be taken:

  1. Stop. This would be the simplest course of action, but doesn't make for a very effective line-following robot.
  2. Reverse. This is another simple solution that assumes the line must be somewhere behind the robot and by reversing, the robot should be able to continue along the path.
  3. Do the opposite. A slightly more complex course of action that makes the assumption that reversing the last mode of travel before leaving the line should return the robot to the line. It's more complex because it needs to know the last state — what the robot was last doing before it left the line.
  4. Freedom! Line? Who cares about the line? The robot is free to wander forever (or, at least until its batteries run out). Unfortunately, this solution does not make an effective line follower since it makes no attempt to get back on the line.
  5. Search. This is the most complicated solution. It would have the robot use a circular or geometric search pattern to look for the line. This would work best if the robot could adjust the speed or amount of travel of each wheel so precise turns could be made.

Practically speaking, reverse is the best option since is quite effective at returning to the line and works with a minimal amount of code.

Create the program

Start with the InputA program, since it's already set up to get input from the phototransistors on PORTA. You'll need to modify the program to perform a sequence of conditional checks. The flowchart in the following photo shows one possible solution.

robotflowchart

Translating the flowchart into working code results in a program like this one:

		
;CHRPbot		v3.1	January 23, 2013
;===============================================================================
;Description:	Demonstrates a simple line-following robot using the CHRP 3.0.

;Configure MPLAB and the microcontroller.

	include	"p16f886.inc"		;Include processor definitions

	__config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
	__config _CONFIG2, _WRT_OFF & _BOR40V

;Set hardware equates.

Q1				equ		0		;PORTA bit position of right phototransistor (Q1)
Q2				equ		1		;PORTA bit position of left phototransistor (Q2)
LED12			equ		7		;PORTA bit position of phototransistor LED (LED12)

;Start the program at the reset vector.

				org	00h				;Reset vector - start of program memory

				clrf	PORTA		;Clear all port outputs before configuring
				clrf	PORTB		;port TRIS registers. Clearing RA4 turns on
				clrf	PORTC		;the Run LED when TRISA is initialized.

				goto	initPorts	;Jump to initialize routine

				org	05h				;Continue program after the interrupt vector

initPorts       ;Configures PORTA and PORTB for digital I/O.

				banksel	ANSEL		;Switch register banks
				clrf	ANSEL		;Set all PORTA pins to digital
				clrf	ANSELH		;Set all PORTB pins to digital
				movlw	01010111b	;Enable Port B pull-ups, TMR0 internal
				movwf	OPTION_REG	;clock, and 256 prescaler
				banksel	TRISA		;Switch register banks
				movlw	00101111b	;Set piezo and LED pins as outputs and
				movwf	TRISA		;all other PORTA pins as inputs
				clrf	TRISB		;Set all PORTB pins as outputs for LEDs
				banksel	PORTA		;Return to register bank 0

main			bsf		PORTA,LED12	;Turn on LED12 for the phototransistors

checkQ1			btfss	PORTA,Q1	;Check if Q1 sees the line
				goto	checkQ2		;If not, check Q2

checkQ2Line		btfss	PORTA,Q2	;If Q1 sees the line, see if Q2 sees the line
				goto	right		;If not, turn right
				goto	forward		;If both Q1 and Q2 see line, go forward
				
checkQ2			btfss	PORTA,Q2	;Check if Q2 sees the line
				goto	reverse		;If not, reverse
				goto	left		;If Q2 sees the line, turn left

forward			movlw	00000110b	;Set forward motor direction
				movwf	PORTB		;Send it to motors
				goto	checkQ1		;Do it again

reverse			movlw	00001001b	;Set reverse motor direction
				movwf	PORTB		;Send it to motors
				goto	checkQ1		;Do it again

left			movlw	00000100b	;Set left motor direction
				movwf	PORTB		;Send it to motors
				goto	checkQ1		;Do it again

right			movlw	00000010b	;Set right motor direction
				movwf	PORTB		;Send it to motors
				goto	checkQ1		;Do it again

				end
		

Test your knowledge

  1. What logic level do you expect when a phototransistor is positioned over the black tape line? Why?

Apply your skills

  1. Using your programming skills and knowledge of inputs, bit test operations, and outputs, create a program that enables your CHRP-based robot to follow a black tape line.
  2. Bonus: Program additional features or functions into your robot. Some ideas include:
    • pushbutton start/stop - set the robot on the line and have it start moving after you press one button and stop after pressing the button again (or a different button)
    • light show - cycle a light pattern on the 4 most significant bits of PORTB, while the robot is travelling (change the pattern as the robot changes direction for extra skill points)
    • obstacle avoidance - add bumpers or sensors to your robot to allow it to wander around without a line
    • programmable robot - use some of the buttons to program a sequence of steps into the robot and then press a button to have it repeat the pattern back
    • IR remote control - install an IR demodulator and control your robot using a TV remote control
    • analogue line follower - sense the light level using analogue inputs and modulate the motor outputs to have the robot travel more smoothly along the line
    • light/dark seeking robot - light seeking works particularly well for obstacle avoidance or solar charging, while dark seeking mimics a scared creature
    • battery sensing - determine the battery level and change behaviour as the power supply drains
    • walking robot - because wheels are so 'Dalek'. Hook up servo-controlled legs to your CHRP board