CS-424, Lab 7
LEGO Mindstorms Robot Control with the PIC18F452
3/13/09, Report Due Date: 3/27/09

In this laboratory you will build a hardware platform for a robot control module (the "PIC-Brick") based on the PIC18F452 QuikFlash and QuikProto boards that can be connected to and control a robot built out of LEGO bricks. In addition you will build a mobile robot from LEGO bricks and attach it to the "PIC-Brick" platform. You will write programs that explore the use of the two-level interrupt system of the PIC microcontroller in order to control the speed of the robot's motors and respond to its touch sensors.

Hardware Design of the "PIC-Brick"

The following diagrams and pictures show what the resulting hardware platform should look like and how it should be assembled. Note that the main objective of this design is to attach a QuikFlash and QuikProto board with battery power packs to a large LEGO brick so that the entire module can be easily integrated with a robot constructed from other LEGO bricks. You will be provided with a kit that contains all the parts necessary to construct the module. Bring a screw driver and a pair of pliers with you to lab so that you can build the platform.



The following pictures indicate how the assembled "PicBrick" control module can be integrated with other LEGO parts to make a working robot:


Motor Control

The motors of the LEGO Mindstorms Robotic Invention System can be interfaced with the PIC18F452 through the use of an H-bridge.  To power the motors it should be noted that the H-bridge utilizes two VCC inputs, one from the Quikboard VCC and the other being a 9-volt AA battery pack.  The H-bridge has two separate enable lines as well as a set of controlling input and output bits so that it can support two motors.  Specifically, a 10K resistor is wired in series with the enable lines of the H-bridge.  No current flows through the motor from the AA battery source when the enable line of the H-bridge is driven low.  To control the specific direction of the motors (counter-clockwise, clockwise, stop), the input bits can be toggled.  A binary signal of 10 sent to a motor through the H-bridge input bits will produce a clockwise motion, a binary signal of 01 a counter-clockwise motion, and a binary signal of 11 a stop.  It is important to note which bit is most/least significant and its corresponding line to the H-bridge.  Using PORTB and PORTC output bits, you can send these signals to control the motion of the robot. Shown below is a schematic diagram showing how the SN754410 Quadruple Half H-Bridge driver integrated circuit needs to be wired on the control module's QuikProto board in order to be able to control two motors on an attached LEGO robot. (The data sheet for the SN754410 is at the following URL: http://manuales.elo.utfsm.cl/datasheet/texas_instruments/analog_design/data/pdf/slrs007b.pdf)


Varying the Speed of Operation of the Motor

At this point it is straightforward to turn the motor on or off.  However, we should be able to do more to the motor than just turn it on (enabled) or off (disabled). We would also like to be able to change its speed.  The technique of Pulse Width modulation (PWM) effectively turns the voltage on and off very rapidly to change the speed of the motors.  The motor will rotate with a speed that is determined by the percent of time the voltage is on (known as the duty cycle).  A pseudo-PWM can be invoked through an interrupt of the PIC18F452.  A low-priority timer interrupt can operate in which the enable lines of the H-bridge are toggled each time the timer expires.  This will pulse the voltage going into the motors, slowing down the robot’s speed.

Experimental Procedure – Part A

Construct a robot from LEGO bricks that has wheels, two of which are driven by LEGO motors, in addition to two bumper arms, each of which causes a touch sensor to be activated whenever the arm strikes an obstacle. Each lab group will be issued a "LEGO Robotics Invention System 2.0" kit that contains all the parts necessary to construct your robot. If you are unfamiliar with LEGO contruction you may want to take a look at Fred Martin's classic article "The Art of LEGO Design" (See the CS-424 notes web page for the URL). You also may want to take a look at the RIS tutorial. to do so:

1. Start the RIS tutorial by double clicking on the "Robotics Invention System 2.0" icon (looks like a mini RCX) on the computer desktop on any of the PCs in the H-1 Lab. Make sure the computer's speaker is turned on and the volume is at an appropriate level (not muted).

2. Select "Run".

3. Make yourself a New User, LOGIN, and <Enter>.

4. Select the "Tour" to see the movie about the RIS.

5. Select "Missions" and "Training Missions" to learn how to use the RIS how to build your first robot -- the Roverbot .

The robot you will be designing and building for this lab should be large enough to support the "PIC-Brick". It should have wheels, two of which are driven by LEGO motors, in addition to two bumper arms, each of which causes a touch sensor to be activated whenever the arm strikes an obstacle. The design is up to you, but the robot should be able to move forward, backward, and turn to the left or to the right. The bumper/touch sensor assembly should enable the robot to detect when it has bumped into an obstacle in front of it on either its right side or its left side. Your robot should be constructed in such a way that it can be easily attached to the "PIC-Brick" control module you assembled in the first part of this lab.

Experimental Procedure – Part B

Wire the H-bridge and LEGO motors to the PIC-Brick's QuikProto board as shown in the schematic above.  You will use bit-4 of Port C (RC4) as an enable line of the H-bridge.  Driving this signal low will disable the H-bridge and corresponding motors.  Bits 2 through 5 of PortB should initialized as output bits, and wired to the control bits of the H-bridge.  Sending binary signals to these bits will control the direction of the motors’ rotation (as mentioned above).  Again, take note of which bit is most significant for each motor and where it is wired.  Follow the schematic to wire the remaining pins of the H-bridge, the LEGO motors lines, ground, and VCCs.

Write a PIC assembly language program that will cause the robot to move in a straight line by pulsing both its motors simutaneously.  The motors should noticeably pulse on/off so that one can detect the change in speed.  To accomplish this, implement a low-priority timer interrupt.  Specifically, enable the microcontroller for two interrupt priority levels.  Then initialize a timer to low-priority and make sure to globally enable both low and high-priority interrupts to CPU.  Write a low-priority interrupt routine that toggles the enable bits of the H-bridge (RC4) each time the timer expires.  Once the timer times out and the routine is called, make sure the timer is reset.  You can alter the length of pulsing by changing the time counter or by using nested loops.  The robot will move forward by sending a binary signal for clockwise motion to the Port B bits.

<see Section 9.3, Peatman for additional material on low-priority  interrupts>

Touch Sensor Control

The touch sensor of the LEGO Mindstorms Robotics Invention System can be interfaced to the PIC18F452 microcontroller through a pull-down resistor circuit (Figure 2).  A 10K resistor is placed between the input switch and ground such that if the circuit is not closed (touch sensor not pressed), the bit connected to the microcontroller will be low.  When the touch sensor is pressed, the circuit is closed and a high signal is delivered to the microcontroller.  Since activating a touch sensor means the robot has collided with an obstacle, the robot will need to interrupt its normal routine and take an avoidance maneuver.  Therefore, the microcontroller must monitor the bits from the touch sensor circuits at all times.  The robot will have two bumpers (touch sensors), so bits 0 and 1 of Port B will be used as an external interrupt for each sensor respectively. The following is a schematic of how the touch sensor should be connected.

PIC External Interrupts

PIC external interrupts on the RB0/INT0, RB1/INT1, and RB2/INT2 pins are edge triggered: either rising, if the corresponding INTEDGx bit is set in the INTCON2 register, or falling if the INTEDGx bit is cleared. When a valid edge appears on the RBx/INTx pin, the corresponding flag bit INTxIF is set. The interrupt can be disabled by clearing the corresponding enable bit INTxIE. Flag bit INTxIF must be cleared in software in the interrupt service routine before re-enabling the interrupt. Interrupt priority for INT1 and INT2 is determined by the value contained in the interrupt priority bits, INT1IP (INTCON3<6> and INT2IP (INTCON3<7>). There is no priority associated with INT0; it is always a high priority source. The INT1 interrupt control bits are in INTCON3 register; those of INT0 are in the INTCON register. As always, when writing an interrupt service routine it is important to save and restore the contents of both WREG and STATUS. Also it is important to properly maintain (reset) interrupt flags -- specifically, to disable all priority interrupts when the high-priority routine is executing, and to re-enable them upon return.

Experimental Procedure – Part C

In this part of the experiment you will interface and program a touch sensor routine for the PIC18F452 microcontroller.  Wire two pull-down resistor circuits (one for each bumper) according to the schematic shown above.  Use bits 0 and 1 from Port B (RB0 & RB1) as external interrupt bits to monitor the touch sensors.

Write a program that will cause the robot to react to the touch sensors.  Specifically, the robot should operate in the same manner as Part B, but back away and turn from obstacles when a touch sensor is activated.  The touch sensor interrupt must have a higher priority than that driving the motor pulses, since it is more important to avoid an obstacle than to continue driving forward.  First, initialize RB0 and RB1 as input bits to the microcontroller.  Then initialize these bits as high priority external interrupts (setting edge sensitivity, clearing flags, and enabling the interrupt source).  Write a high-priority interrupt routine that reacts to the touch sensors, backing away and pivoting left if the right bumper is pressed and pivoting right if the left bumper is pressed.  You can determine which bumper is pressed by checking which bit triggered the interrupt.  This interrupt routine should consist primarily of sending different bit patterns to the motors (stop, clockwise motion, etc.) as well as using a delay loop to control the length of turn / movement.  After returning from this routine the robot should continue its normal course of action (i.e. a forward pulsed motion).

<see Section 9.4 & 9.6, Peatman for additional material on external  interrupts>

Be sure to show the TA your working robot.

As always, in the lab report you should submit circuit diagrams and assembled listings.