Showing posts with label PIC Project. Show all posts
Showing posts with label PIC Project. Show all posts

Servomotor-based mobile robot control


Project description:
 Mobile robots are used in many industrial, commercial, research, and hobby applications. This project is about the control of a mobile robot using servomotors. The robot used in this project is the base of a popular mobile robot known as Boe Bot, developed by Parallax (www.parallax.com and www.stampinclass.com). The basic robot is controlled from a Basic Stamp controller (Trademark of Parallax Inc.). The robot base and electronic circuit have been modified by the author so that the robot can be used with PIC microcontrollers
( Figure 1). The robot consists of two side drive wheels and a caster wheel at the back. The drive wheels are connected to servomotors. A breadboard is placed on the robot base for the electronic control circuit. The robot is driven from a 9V battery, and a 78L05-type voltage regulator is used to obtain _5V to supply power to the microcontroller circuit. In this project programs are developed to move the robot forward, backward, and to turn left and right.

Figure 1 Robot used in the project

Hardware:
The circuit diagram of the project is shown in Figure 2. In this project a PIC16F84 microcontroller is used and the microcontroller is operated with a 4 MHz crystal. Operating the servomotor As described in Section 4.7 the servomotors used in robotic applications are modified servos where the motor can rotate in either direction continuously by applying pulses to the servomotor. In a modified servomotor typically a pulse with a width of 1.3 ms rotates the motor clockwise at full speed. A pulse with a width of 1.7 ms rotates the motor anti-clockwise, and a pulse with a width of 1.5 ms stops the motor. Figure 3 shows typical pulses used to drive modified servomotors. The pulse required to operate a servomotor can very easily be obtained using the PULSOUT statement of the PicBasic and PicBasic Pro compilers. When a 4 MHz crystal is used, the time interval of PULSOUT is in units of 10_s. For example, the following PicBasic statement generates a pulse with a width of 1.3 ms from bit 0 of PortB (1.3 ms _ 1300_s and 1300/10 _ 130):
PULSOUT 0, 130


Figure 2 Circuit diagram

Servomotors are used to drive the left wheel and the right wheel. A servomotor has three leads: power supply, ground, and the signal pin. Left servomotor is connected to bit 0 of PORTB (RB0), and right servomotor is connected to bit 1 of PORTB (RB1). Although some servomotors can operate with _5V supply, most servomotors require 6–9V to operate. Similarly, the following PicBasic statement generates a pulse with a width of 1.7 ms from bit 1 of PORTB:
PULSOUT 1, 170
   A single pulse rotates the servomotor by a small amount. For a continuous rotation we have to
apply the pulses continuously. In most applications a loop is formed in software and pulses are
sent to the servomotor continuously. A delay is inserted between each pulse. The duration of this
delay determines the speed of the motor and about 20 ms is most commonly used value. The following PicBasic (or PicBasic Pro) code shows how a servomotor connected to port RB0
can be rotated clockwise continuously:

Loop: PULSOUT 0, 130                                             ‘ Send a pulse
PAUSE 20                                                                  ‘ Wait 20 ms
GOTO Loop                                                                ‘ Repeat

Similarly, the following PicBasic (or PicBasic Pro) code shows how a servomotor connected to
port RB1 can be rotated anti-clockwise continuously:

Loop: PULSOUT 1, 170                                                ‘ Send a pulse
PAUSE 20                                                                      ‘ Wait 20 ms
GOTO Loop                                                                    ‘ Repeat


Figure 5.147 Pulses used to drive modified servomotors

You can experiment by varying the pulse width and the delay to see how the speed of the motor
changes.

Forward movement
Assuming that two side wheels are connected to servomotors, the robot moves forward when
·         Left wheel rotates anti-clockwise
·         Right wheel rotates clockwise
In this project, the left servomotor is connected to port pin RB0 and right servomotor is connected to port pin RB1. The following PicBasic (or PicBasic Pro) code can then be used to move the robot forward:

Forward:           
 PULSOUT 0, 170                                    ‘ Left wheel anti-clockwise
PULSOUT 1, 130                                     ‘ Right wheel clockwise
PAUSE 20                                                 ‘ Wait 20 ms
GOTO Forward                                           ‘ Repeat
Backward movement
Assuming that the two side wheels are connected to servomotors, the robot moves backward when
·         Left wheel rotates clockwise
·         Right wheel rotates anti-clockwise
In this project, the left servomotor is connected to port pin RB0 and right servomotor is connected to port pin RB1. The following PicBasic (or PicBasic Pro) code can then be used to move the robot backward:

Backward:
 PULSOUT 0, 130                                          ‘ Left wheel clockwise
PULSOUT 1, 170                                          ‘ Right wheel anti-clockwise
PAUSE 20                                                      ‘ Wait 20 ms
GOTO Backward                                           ‘ Repeat

Moving the robot for required amount of time

                               The code given above moves the robot forward or backward continuously. There are applications where we may want to mode the robot only required amount of time. For example, we may want to move the robot forward for 5 s, then stop for 3 s, and then move backward for 2 s.

We can adjust the movement time by using a FOR loop. The following code shows how we can
move the robot forward using a FOR loop:

FOR J _ 1 TO M
PULSOUT 0, 170
PULSOUT 1, 130
PULSOUT 20
NEXT J

In this code variable M determines the number of times the loop is executed. Ignoring the small time taken by the FOR and the NEXT statements, the time taken to execute only one iteration of the FOR loop can be determined approximately as

FOR J _ 1 TO M                                                            
PULSOUT 0, 170
PULSOUT 1, 130
PULSOUT 20
NEXT J
Thus, if the robot is required to move for T seconds (1000 x T ms) forward or backward, the value of M to be used in the FOR loop can be calculated as follows:
M  x 1000 x T/23
An example is given below.

Example 1
A mobile robot is controlled with two servomotors as shown in Figure 2. Write a PicBasic
program which will perform the following operations:
·         Move the robot forward for 4 s
·         Wait for 5 s
·         Move the robot backward for 3 s
·         Stop

Solution 1
The first action is to move the robot forward for 4 s. Thus, the value of M is M  x 4000/23 x 174
Then the robot is required to stop for 5 s and then move backward for 3 s. The value of M for this
movement is M x 3000/23 x 130 The program is very simple and consists of only a few lines.
PicBasic program for this example is given in Figure 4.

 ‘*************************************************************************
 ‘ ROBOT CONTROL
‘ ===============
 ‘ In this project a mobile robot is controlled. The robot has two side wheels and
‘ a back caster wheel. Side wheels are connected to servomotors as follows:
§  Left wheel RB0
§  Right wheel RB1
 ‘ In this project the robot moves as follows:
·          Move the robot forward for 4 seconds
·          Wait for 5 seconds
·          Move the robot backward for 3 seconds
·          Stop
 ‘ A PIC16F84 type microcontroller is used with a 4 MHz crystal
‘*************************************************************************

Figure 4(Continued)

 ‘ Symbols
Symbol PORTB = $06 ‘ PORTB address
Symbol TRISB = $86 ‘ TRISB address
Symbol J = B0
POKE TRISB, 0 ‘ PORTB is output
‘ Move the robot forward for 4 seconds
FOR J = 1 TO 174
PULSOUT 0, 170
PULSOUT 1, 130
PAUSE 20
NEXT
‘ Wait for 5 seconds
PAUSE 5000
‘ Move the robot backward for 3 seconds
FOR J = 1 TO 130
PULSOUT 0, 130
PULSOUT 1, 170
PAUSE 20
NEXT J
END ‘ End of program
Figure 4 PicBasic program for Example 1

Measuring the speed of the robot
The speed of the robot can easily be measured by moving it for a known amount of time and measuring the distance moved during this time. The speed is then given by Speed  x distance/time In this project the robot moved forward for 10 s and the distance moved was 210 cm. Thus, the speed of the robot is 210/10 x 21 cm/s.

Once we know the speed, we can move the robot forward or backward by any required amount. For example, to move the robot forward by 85 cm, the required time is approximately given by Time x distance/speed x 85/21 x 4 Thus, the servomotors should be operated for 4 s. The value of loop-count M is then approximately given by M  x 4000/23 x 174.
The required PicBasic code is
FOR J _ 1 TO 174
PULSOUT 0, 170
PULSOUT 1, 130
PULSOUT 20
NEXT J

Turning left and right
Several techniques can be used to turn the robot left or right. One technique is to stop the servomotor on the side where we wish to turn. For example, we can turn right by stopping the right servo and turning the left servo anti-clockwise. Another technique of turning a robot smoothly involves rotating both servos in the same direction and this is the technique we shall be using here. For example,
To turn RIGHT:
·         Rotate left wheel anti-clockwise
·         Rotate right wheel anti-clockwise
To turn LEFT:
·         Rotate left wheel clockwise
·         Rotate right wheel clockwise
The problem here is how many pulses to send to the servomotors so that the robot turns a complete 90° angle. This is something which can be found by trial and error. The following PicBasic code rotates the robot right where the angle of rotation depends on
variable R:

Turn_right:
    
FOR J _ 1 TO R
PULSOUT 0, 170                                           ‘ Left wheel anti-clockwise
PULSOUT 1, 170                                           ‘ Right wheel anti-clockwise
PAUSE 20                                                      ‘ Wait 20 ms
NEXT J
Similarly, the following code rotates the robot left where the angle of rotation depends on variable R:

Turn_left:

FOR J _ 1 TO R
PULSOUT 0, 130                                                            ‘ Left wheel clockwise
PULSOUT 1, 130                                                            ‘ Right wheel clockwise
PAUSE 20                                                                       ‘ Wait 20 ms
NEXT J
It was found by experimentation that when R [r5] is equal to 13 the robot turns by about 90°. An
example is given below.

Example 2
A mobile robot is controlled with two servomotors as shown in Figure 2, and a pen is attached to the front of the robot with the tip of the pen touching the floor. Write a PicBasic program which will move the robot as follows: Move the robot forward for 5 s
·         Wait for 2 s
·         Turn right
·         Move the robot forward for 3 s
·         Stop

Solution 2
The first action is to move the robot forward for 5 s. Thus, the value of M is
M  x  5000/23 x 217. Then the robot is required to stop for 2 s and then turn right and move backward for 3 s. The value of M for this movement is M x 3000/23 x 130
PicBasic program for this example is given in Figure 5.
 ‘**************************************************************************
 ‘ ROBOT CONTROL
‘ ===============
 ‘ In this project a mobile robot is controlled. The robot has two side wheels and
‘ a back caster wheel. Side wheels are connected to servomotors as follows:
§  Left wheel RB0
§  Right wheel RB1
 ‘ In this project the robot moves as follows:
§  Move the robot forward for 4 seconds
§   Wait for 2 seconds
§   Turn right
§   Move the robot forward for 3 seconds
§   Stop
‘ A PIC16F84 type microcontroller is used with a 4 MHz crystal
 ‘***********************************************************************
 ‘ Symbols
Symbol PORTB = $06                                             ‘ PORTB address
Symbol TRISB = $86                                               ‘ TRISB address
Symbol J = B0
POKE TRISB, 0                                                       ‘ PORTB is output
Figure 5 (Continued)
 ‘ Move the robot forward for 4 seconds
FOR J = 1 TO 217
PULSOUT 0, 170
PULSOUT 1, 130
PAUSE 20
NEXT J
 ‘ Wait for 2 seconds
PAUSE 2000
 ‘ Turn right
FOR J = 1 TO 13
PULSOUT 0, 170
PULSOUT 1, 170
PAUSE 20
NEXT J
 ‘ Move the robot forward for 3 seconds
FOR J = 1 TO 130
PULSOUT 0, 170
PULSOUT 1, 130
PAUSE 20
NEXT J
END ‘ End of program
Figure 5 PicBasic program for Example 2

Right-left scrolling LEDs


Project description:
 In this project, 8 LEDs are connected to PORT B of a PIC microcontroller. Also a push-button switch is connected to bit 0 of PORT A using a pull-up resistor.. When the switch is pressed the LEDs scroll to the right.

Hardware:
The circuit diagram of the project is shown in Figure 1. The circuit in this project additionally a switch is connected to bit 0 of PORTA to control the direction of scrolling. A PIC16F627 model PIC micro controller is used and the micro controller is operated from its 4 MHz internal clock. The LEDs are connected to 8 pins of PORT B using 330_ current-limiting resistors. An external reset button is connected to MCLR input of the microcontroller.

Figure 1 Circuit diagram

Flow diagram:
The flow diagram of the project is shown in Figure 2. At the beginning of the program the I/O direction is specified. A byte variable called Cnt is used as the loop variable. The program consists of an indefinite loop and at the beginning of the loop the switch is tested. If the switch is logic 1 (i.e. switch is not pressed) then the scrolling is to the left and if the switch is pressed the switch is at logic 0 and scrolling is to the right. A 250 ms delay is used between each output.

Software: Pic Basic
The software for PicBasic language is given in Figure 3. At the beginning of the program PORTA, PORTB, TRISA, TRISB, and CMCON register addresses are defined. TRISA is set to 1 so that bit 0 of PORTA is configured as an input port. Similarly, TRISB is cleared to 0 so that all bits of PORTB are configured as outputs. Push-button switch is connected to bit 0 of PORTA (RA0). Normally this pin is pulled high to logic 1 by using a resistor. When the switch is pressed the pin goes down to logic 0. PORTA pins on the PIC16F627 microcontroller have dual functions and they can either be used as analog comparator inputs, or as digital I/O ports. CMCON register is used to control the function of these pins. Setting CMCON to 7
configures PORTA pins as digital I/O ports. Inside the LOOP, the value of Cnt is sent to PORTB and the PEEK instruction is used to read the switch setting. “Bit0” refers to bit 0 of variable “B0” which is where the switch is connected. When the switch is pressed the program jumps to label PRESSED where the LEDs are scrolled right. When the switch is not pressed the LEDs are scrolled left. This loop is repeated forever with 250 ms delay between each output. Set port directions

Figure 2 Flow diagram

‘*************************************************************************
 ‘ RIGHT-LEFT SCROLLING LEDS
‘ =========================
 ‘ 8 LEDs are connected to PORTB of a PIC microcontroller. This program
‘ scrolls the LEDs to the right or left depending on a switch setting. The switch
‘ is connected to bit 0 of PORT A. If the switch is not pressed the switch
‘ output is at logic 1 and the LEDs scroll to the left. When the switch is
‘ pressed the LEDs scroll to the right. A 250ms delay is used between each
‘ output.
 ‘*************************************************************************

 ‘ SYMBOLS
Symbol TRISA = $85                                              ‘ TRISA address
Symbol TRISB = $86                                              ‘ TRISB address
Symbol PORTA = $05                                             ‘ PORTA address
Symbol PORTB = $06                                            ‘ PORTB address
Symbol CMCON = $1F                                          ‘ CMCON address
Symbol Cnt = B1                                                    ‘ Cnt is a byte variable
Symbol Switch = B0                                                 ‘ Switch is a byte variable
‘ START OF MAIN PROGRAM
POKE CMCON, 7                                                  ‘ RA0-RA3 are digital I/O
POKE TRISA, 1                                                    ‘ Set PORTA bit 0 as input
POKE TRISB, 0                                                    ‘ Set all PORTB pins as outputs
INIT:
CNT = 1                                                                ‘ Initialise Cnt to 1

Figure 3 (Continued)

PicBasic Pro
The software for PicBasic Pro language is given in Figure 4. The PicBasic Pro program is easier to understand. At the beginning of the program TRISB is cleared to 0 to make all PORTB pins as outputs. Also, TRISA is set to 1 so that bit 0 of PORTA is configured as input. CMCON
register is set to 7 to configure PORTA pins as digital I/O. The switch setting is then checked using an IF statement. When the switch is pressed bit 0 of PORTA goes to logic 0 and the program scrolls the LEDs to right. When the switch is not pressed bit 0 of PORTA is at logic
1 and the program scrolls the LEDs to the left.
LOOP:
POKE PORTB, Cnt                                                         ‘ Send Cnt to PORTB
PAUSE 250                                                                      ‘ Wait 250ms
PEEK PORTA, Switch                                                    ‘ Read switch setting
IF Bit0 = 0 THEN PRESSED                                           ‘ If switch is pressed
IF Cnt = 128 THEN INIT
Cnt = Cnt * 2                                                                     ‘ Shift Cnt left
GOTO LOOP
PRESSED:                                                                        ‘ Switch is pressed
IF Cnt = 1 THEN NXT
Cnt = Cnt / 2
GOTO LOOP
NXT:
Cnt = 128
GOTO LOOP
END                                                                                      ‘ End of program

Figure 3 PicBasic program

‘**************************************************************************
 ‘ RIGHT-LEFT SHIFTING LEDS
‘ ========================
 ‘ 8 LEDs are connected to PORTB of a PIC16F627 microcontroller.
‘ This program scrolls the LEDs right or left depending on the mode of a
‘ push-button switch. When the switch is not pressed LEDs are scrolled left.
‘ When the switch is pressed, LEDs are scrolled right. A 250ms delay
‘ is used between each output.

‘*************************************************************************
 ‘ DEFINITIONS
Cnt VAR Byte                                                            ‘ Declare Cnt as a Byte variable
 ‘ START OF MAIN PROGRAM
CMCON _ 7                                                               ‘ Set PORTA as digital I/O
TRISA _ 1                                                                  ‘ Set RA0 as input
TRISB _ 0                                                                  ‘ Set PORTB pins as outputs
INIT:
Cnt _ 1                                                                       ‘ Initialise Cnt to 1
LOOP:
PORTB _ Cnt                                                           ‘ Send Cnt to PORTB
PAUSE 250                                                               ‘ Wait 250ms
IF PORTA.0 _ 0 THEN
IF Cnt _ 1 THEN Cnt _ 128: GOTO LOOP
Cnt = Cnt >> 1                                                               ‘ Shift right
GOTO LOOP
ELSE
IF Cnt _ 128 THEN INIT
Cnt _ Cnt << 1                                                                ‘ Shift left
GOTO LOOP
ENDIF
END                                                                                  ‘ End of program

Figure 4 PicBasic Pro program 
 
Copyright Electronics Projects And Details All Rights Reserved
ProSense theme created by Dosh Dosh and The Wrong Advices.
Blogerized by Alat Recording Studio Rekaman.