How to Program the 2004 Robot Controller from Innovation First - PowerPoint PPT Presentation

1 / 89
About This Presentation
Title:

How to Program the 2004 Robot Controller from Innovation First

Description:

How to Program the 2004 Robot Controller from Innovation First – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 90
Provided by: ghsrob
Category:

less

Transcript and Presenter's Notes

Title: How to Program the 2004 Robot Controller from Innovation First


1
How to Program the 2004 Robot Controller from
Innovation First
  • For new C programmers and non-technical types

2
Topics
  • CMU Robot Builder Review
  • Whats New in 2004?
  • C Concepts
  • Default Code Explained
  • Modifying the Default Code
  • Advanced Topics

3
From EDU and CMU to FRC
  • You may already know everything you need!

4
EDU RC vs. FRC
  • Have you already practiced programming the 2004
    EDU Robot Controller?
  • The process is nearly identical!
  • Only differences are
  • FRC has more and different I/O
  • FRC works with IFI radio modems and Operator
    Interface (just like last year)

5
Carnegie-Mellons Robot Builder
  • The basic process of programming your EDU RC was
    covered in CMUs tutorial called Robot Builder.
  • Its on the web at http//www.rec.ri.cmu.edu/edu
    cation/edubot/2004_content/index.htm and also
    included on a CD-ROM in your 2004 FIRST kits.

6
Quick Review Getting Started
  • IMPORTANT Download the instructions (Microchip
    C-Bot Installation Guide ) from IFIs website.
  • While youre there, get the FRC RC Default Code.
    Be sure to unZip it after downloading. Put it in
    your c\mcc18\ directory.
  • There is also a newer version of IFI Loader than
    the one on the C-Bot CD. Download and install
    it. (Make sure you un-install any previous
    versions that you have!)

http//www.innovationfirst.com/
firstrobotics/documentation.htm
7
Quick Review Install C-Bot
  • Follow instructions from IFI!
  • Use C-Bot CD to install
  • Step 1 MPLAB IDE
  • Step 2 C18 Compiler
  • Skip Step 3 and install newer version of IFI
    Loader from web site.

8
Quick Review Write and compile your code
  • Start MPLAB IDE
  • File -gt Open Workspace
  • Select the .mcw file from the Default Code you
    downloaded.
  • Double-click user_routines.c in project window.
  • Scroll down and edit Default_Routine().
  • Project -gt Make
  • Verify BUILD SUCCEEDED

9
Quick and Dirty Code Modification
pwm01 p1_y pwm02 p2_y pwm03
p3_y pwm04 p4_y pwm05 p1_x
pwm06 p2_x pwm07 p3_x pwm08
p4_x pwm09 p1_wheel pwm10 p2_wheel
pwm11 p3_wheel pwm12 p4_wheel
pwm01 p1_y pwm02 p1_x pwm03
p3_y pwm04 p4_y pwm05 p1_x
pwm06 p2_x pwm07 p3_x pwm08
p4_x pwm09 p1_wheel pwm10 p2_wheel
pwm11 p3_wheel pwm12 p4_wheel
pwm01 p1_y pwm02 255 - p1_x
pwm03 p3_y pwm04 p4_y pwm05
p1_x pwm06 p2_x pwm07 p3_x
pwm08 p4_x pwm09 p1_wheel pwm10
p2_wheel pwm11 p3_wheel pwm12
p4_wheel
10
Quick Review Download code to your Robot
Controller
  • Power up RC
  • Connect serial cable from PC to PROGRAM port.
  • Start IFI_Loader.
  • Select appropriate COM port.
  • Browse to find the .hex file you just compiled.
  • Click DOWNLOAD.

11
Summary of Programming Process
  • Download docs, code, and app from
    innovationfirst.com
  • Install apps from C-Bot CD.
  • Run MPLAB, edit code, and compile it.
  • Run IFI Loader to transfer .hex file to your FRC

12
Whats New in 2004?
  • Whats gone?
  • Whats the same?
  • Whats transformed?
  • Whats added?

13
Whats gone?
  • Not much!
  • Limitation of executing code only every 25ms is
    gone!
  • No team number DIP switches on the RC.
  • No fuses.

2003
2004
14
Whats the same?
  • Same form-factor.
  • Still use serial port to download code.
  • Same OI and radios
  • Same info from OI, with same variable names.
  • Same number of relay and PWM outputs.
  • Same Default Loop.
  • Default Code has nearly same functionality.
  • Direct access to the digital I/O pins.
  • No native floating point math.
  • No built in trig functions.

15
Whats transformed?
OLD
NEW
  • PBASIC
  • Aliases
  • SERIN
  • SEROUT
  • Analog byte variable
  • Digital inputs only
  • Seven 8-bit analog inputs
  • D-sub connectors
  • C (and optional Assembly)
  • Macros
  • GetData()
  • PutData()
  • Get_Analog_Value() function
  • Digital I/O (user chooses)
  • Sixteen 10-bit analog inputs
  • Vertical pin header strips

16
Whats added?
  • Faster processor (10 MIPS vs. 0.01 MIPS)
  • More variable space (2K SRAM)
  • More program space (32K FLASH)
  • Extra serial port
  • Interrupts
  • Timers
  • Direct control of the hardware
  • Battery backup for RC
  • Can update four of the PWMs faster

17
HIGHLIGHTS Whats new?
  • C programming
  • Much beefier processor
  • More and better hardware
  • Advanced features

18
C Concepts
  • The very basics

19
Why C?
  • Modular
  • Powerful
  • Industry standard
  • Fast and efficient

20
One project, many files
  • .c -Source Files
  • .h -Header Files
  • .lib -Library Files
  • .lkr -Linker Scripts
  • Edit the .c and .h files
  • Compile
  • Link
  • ? FrcCode.hex

21
Variables
  • PBASIC
  • p1_x VAR byte Port 1, X-axis on Joystick
  • wheel_count VAR byte number of wheel
    revolutions
  • range 0 to 255
  • C
  • unsigned char wheel_count /number wheel
    revolutions/

22
Variables
  • unsigned char wheel_count / range 0 to 255
    /
  • char wheel_count / range -128 to
    127 /
  • unsigned int wheel_count / range 0 to 65535
    /
  • int wheel_count / range -32768 to
    32767 /

/ This is a comment. It is ignored by the
compiler. It ends here. /
23
C is case-sensitive
  • unsigned char wheel_count
  • (is not the same as)
  • unsigned char Wheel_count

24
Initialize Variables
  • unsigned char wheel_count 200

25
Functions (aka subroutines)
  • PBASIC
  • GOSUB Double_Number

C Double_Number()
three_doubled Double_Number(3)
26
Sample Function
  • unsigned int Doubled_Number (unsigned int
    num2double)
  • unsigned int result
  • result 2 num2double
  • return result

27
Sample Function
Sample Function
  • unsigned int Doubled_Number (unsigned int
    num2double)
  • unsigned int result
  • result 2 num2double
  • return result

28
Simple Function
  • void Do_Nothing(void)
  • return

29
Function Prototypes
unsigned int Double_Number (unsigned int
num2double) void Do_Nothing (void)
30
Macros (as aliases)
  • PBASIC
  • p4_sw_aux2 VAR oi_swB.bit7 'Aux input
  • C
  • define p4_sw_aux2 rxdata.oi_swB_byte.bitselect.b
    it7 / Aux input/

31
Pre-Processor Note
  • The symbol denotes a pre-processor statement
    that is evaluated before the compiler takes over.
  • include ifi_aliases.h

32
Macros (as constants)
  • PBASIC
  • COMC CON 3
  • C
  • define FIELD_WIDTH 125
  • define FIELD_LENGTH 300
  • define FIELD_PERIMETER FIELD_WIDTH2
    FIELD_LENGTH2

Use ours in ifi_aliases.h Put yours in
user_routines.h
33
Assignments
  • PBASIC
  • relay3_fwd p3_sw_trig
  • C
  • relay3_fwd p3_sw_trig

34
Comparison and Logical Operators
  • PBASIC
  • , -, , /
  • , ,
  • gt, gt, lt, lt
  • , ltgt
  • C
  • , -, , /
  • , ,
  • gt, gt, lt, lt
  • , !

35
Conditionals if-then vs. if-else
  • PBASIC (if-then)
  • if (CONDITION) then (ADDRESS)
  • If CONDITION is true then the program will jump
    to the section of the program labeled with
    ADDRESS.
  • C (if-else)
  • if (CONDITION 1)
  • DO THIS
  • else if (CONDITION 2)
  • DO THIS
  • else
  • DO THIS BY DEFAULT

36
Looping while
  • The while loop repeats a statement until the test
    at the top proves false.

count 0 while(count lt 7) count count
1
while(condition is TRUE) execute this
block
37
Looping while
  • The while loop can also be used for waiting.

while(!rc_dig_in01) / wait here /
38
Looping while
  • This is an infinite loop.

while(1) / wait here forever /
39
Debug statements printf
  • PBASIC
  • DEBUG PWM01 ,DEC pwm01, CR
  • C
  • printf(PWM01 d\n,(int)pwm01)

Output PWM01 127
40
Thats all the C knowledge you really need!
41
Macros (as aliases)
  • PBASIC
  • p4_sw_aux2 VAR oi_swB.bit7 'Aux input
  • C
  • define p4_sw_aux2 rxdata.oi_swB_byte.bitselect.b
    it7 / Aux input/

42
Pre-Processor Note
  • The symbol denotes a pre-processor statement
    that is evaluated before the compiler takes over.
  • include ifi_aliases.h

43
Macros (as constants)
  • PBASIC
  • COMC CON 3
  • C
  • define FIELD_WIDTH 125
  • define FIELD_LENGTH 300
  • define FIELD_PERIMETER FIELD_WIDTH2
    FIELD_LENGTH2

Use ours in ifi_aliases.h Put yours in
user_routines.h
44
Assignments
  • PBASIC
  • relay3_fwd p3_sw_trig
  • C
  • relay3_fwd p3_sw_trig

45
Comparison and Logical Operators
  • PBASIC
  • , -, , /
  • , ,
  • gt, gt, lt, lt
  • , ltgt
  • C
  • , -, , /
  • , ,
  • gt, gt, lt, lt
  • , !

46
Conditionals if-then vs. if-else
  • PBASIC (if-then)
  • if (CONDITION) then (ADDRESS)
  • If CONDITION is true then the program will jump
    to the section of the program labeled with
    ADDRESS.
  • C (if-else)
  • if (CONDITION 1)
  • DO THIS
  • else if (CONDITION 2)
  • DO THIS
  • else
  • DO THIS BY DEFAULT

47
Looping while
  • The while loop repeats a statement until the test
    at the top proves false.

count 0 while(count lt 7) count count
1
while(condition is TRUE) execute this
block
48
Looping while
  • The while loop can also be used for waiting.

while(!rc_dig_in01) / wait here /
49
Looping while
  • This is an infinite loop.

while(1) / wait here forever /
50
Debug statements printf
  • PBASIC
  • DEBUG PWM01 ,DEC pwm01, CR
  • C
  • printf(PWM01 d\n,(int)pwm01)

Output PWM01 127
51
Thats all the C knowledge you really need!
52
Default_Routine()- PWM Mapping
/
FUNCTION NAME
Default_Routine PURPOSE Performs the
default mappings of inputs to outputs. CALLED
FROM this file, Process_Data_From_Master_uP
routine ARGUMENTS none RETURNS
void ---------------------------------------------
---------------------------------/ void
Default_Routine(void) /---------- Analog
Inputs (Joysticks) to PWM Outputs-----------------
------ ----------------------------------------
---------------------------------- This
maps the joystick axes to specific PWM outputs.
/ pwm01 p1_y pwm02 p2_y pwm03
p3_y pwm04 p4_y pwm05 p1_x
pwm06 p2_x pwm07 p3_x pwm08
p4_x pwm09 p1_wheel pwm10 p2_wheel
pwm11 p3_wheel pwm12 p4_wheel
ifi_aliases.h contains all of these macro
definitions and more
unsigned char Range 0 to 255
53
Default_Routine()- 1 Joystick Drive
/---------- 1 Joystick Drive -------------------
--------------------------- -------------------
--------------------------------------------------
----- This code mixes the Y and X axes on
Port 1 to allow one joystick drive.
Joystick forward Robot forward Joystick
backward Robot backward Joystick right
Robot rotates right Joystick left
Robot rotates left Connect the right drive
motors to PWM13 and/or PWM14 on the RC.
Connect the left drive motors to PWM15 and/or
PWM16 on the RC. / pwm13 pwm14
Limit_Mix(2000 p1_y p1_x - 127) pwm15
pwm16 Limit_Mix(2000 p1_y - p1_x 127)
54
Default_Routine()- Buttons to Relays
/---------- Buttons to Relays-------------------
--------------------------- -------------------
--------------------------------------------------
----- This default code maps the joystick
buttons to specific relay outputs. Relays
1 and 2 use limit switches to stop the movement
in one direction. The used below is the C
symbol for AND
/ relay1_fwd p1_sw_trig rc_dig_in01 /
FWD only if switch1 is not closed. /
relay1_rev p1_sw_top rc_dig_in02 / REV
only if switch2 is not closed. / relay2_fwd
p2_sw_trig rc_dig_in03 / FWD only if switch3
is not closed. / relay2_rev p2_sw_top
rc_dig_in04 / REV only if switch4 is not
closed. / relay3_fwd p3_sw_trig
relay3_rev p3_sw_top relay4_fwd
p4_sw_trig relay4_rev p4_sw_top
relay5_fwd p1_sw_aux1 relay5_rev
p1_sw_aux2 relay6_fwd p3_sw_aux1
relay6_rev p3_sw_aux2 relay7_fwd
p4_sw_aux1 relay7_rev p4_sw_aux2
relay8_fwd !rc_dig_in18 / Power pump only if
pressure switch is off. / relay8_rev 0
ifi_aliases.h lists these aliases
55
Default_Routine()- Limit Switches
/---------- PWM outputs Limited by Limit
Switches ---------/ Limit_Switch_Max(!rc_di
g_in05, pwm03) Limit_Switch_Min(!rc_dig_in06,
pwm03) Limit_Switch_Max(!rc_dig_in07,
pwm04) Limit_Switch_Min(!rc_dig_in08,
pwm04) Limit_Switch_Max(!rc_dig_in09,
pwm09) Limit_Switch_Min(!rc_dig_in10,
pwm09) Limit_Switch_Max(!rc_dig_in11,
pwm10) Limit_Switch_Min(!rc_dig_in12,
pwm10) Limit_Switch_Max(!rc_dig_in13,
pwm11) Limit_Switch_Min(!rc_dig_in14,
pwm11) Limit_Switch_Max(!rc_dig_in15,
pwm12) Limit_Switch_Min(!rc_dig_in16,
pwm12)
void Limit_Switch_Max(unsigned char switch_state,
unsigned char input_value) if (switch_state
CLOSED) if(input_value gt 127)
input_value 127
56
Default_Routine()- Feedback LEDs I
/---------- ROBOT FEEDBACK LEDs-----------------
-------------------------------
-------------------------------------------------
----------------------------- This section
drives the "ROBOT FEEDBACK" lights on the
Operator Interface. The lights are green
for joystick forward and red for joystick
reverse. These may be changed for any use
that the user desires.
/ if (user_display_mode 0) / User Mode is
Off / / Check position of Port 1
Joystick / if (p1_y gt 0 p1_y lt 56)
/ Joystick is in full
reverse position / Pwm1_green 0 /
Turn PWM1 green LED - OFF / Pwm1_red 1
/ Turn PWM1 red LED - ON /
else if (p1_y gt 125 p1_y lt 129)
/ Joystick is in neutral position
/ Pwm1_green 1 / Turn PWM1 green
LED - ON / Pwm1_red 1 / Turn
PWM1 red LED - ON / else if (p1_y gt
216 p1_y lt 255) /
Joystick is in full forward position/
Pwm1_green 1 / Turn PWM1 green LED - ON
/ Pwm1_red 0 / Turn PWM1 red
LED - OFF / else
/ In either forward or reverse position
/ Pwm1_green 0 / Turn PWM1 green
LED - OFF / Pwm1_red 0 / Turn
PWM1 red LED - OFF / /END Check
position of Port 1 Joystick
57
Default_Routine()- Feedback LEDs II
/ This drives the Relay 1 and Relay 2 "Robot
Feedback" lights on the OI. / Relay1_green
relay1_fwd / LED is ON when Relay 1 is FWD
/ Relay1_red relay1_rev / LED is
ON when Relay 1 is REV / Relay2_green
relay2_fwd / LED is ON when Relay 2 is FWD
/ Relay2_red relay2_rev / LED is
ON when Relay 2 is REV /   Switch1_LED
!(int)rc_dig_in01 Switch2_LED
!(int)rc_dig_in02 Switch3_LED
!(int)rc_dig_in03 / (user_display_mode
0) (User Mode is Off) / else / User
Mode is On - displays data in OI 4-digit
display/ User_Mode_byte
backup_voltage10 / so that decimal doesn't get
truncated. / / END
Default_Routine() /
58
Places to modify
in user_routines.c
  • Add variables
  • Initialization
  • Modify Default_Routine()
  • Add to slow loop
  • Add to fast loop
  • Add to Autonomous code

in user_routines_fast.c
59
Advanced Topics
  • For further study

60
When youre ready for the next challenge, try
these features
  • Autonomous Mode
  • Timers
  • Interrupts
  • Generate_Pwms()

61
For more information
  • References
  • Microchip docs on C-Bot CD-ROM
  • C18 C Compiler Users Guide
  • PIC18F8520 Data Sheet
  • IFI docs from innovationfirst.com
  • 2004 Programming Reference Guide
  • Full-Size RC Default Software Reference Guide
  • FAQ
  • Web sites to learn C
  • Google C Programming Course
  • Books to learn C
  • C For Dummies
  • Learn C In XX Days/Hours
  • The C Programming Language (2nd ed.)
  • -Brian W. Kernighan Dennis Ritchie
  • Peer assistance chiefdelphi.com

62
(No Transcript)
63
Introduction
  • Provides an introduction to the art and
    science of programming robots to operate in the
    autonomous mode. In typical competitions of the
    past, the robot was required to operate without
    the influence of the human drivers for the first
    15 seconds in a two-minute contest. It is
    anticipated that this will be true again for the
    upcoming competition. 
  • There are literally hundreds and hundreds of
    configurations and programming styles to
    accomplish this 15 second operation. This
    handout will present what I hope to be several
    possibilities for the novice programming teams.
    Options are considered and presented in a simple
    to more challenging configuration.
  • Blind autonomous, memorized movements and line
    tracking will be considered. Use of encoders,
    inertial instruments for dead reckoning the
    robots location or IR technology for
    triangulating will not be considered in this
    handout.

64
Basic Robot
  • Figure 1 illustrates a simple chain drive system
    on a chassis. The signal pwm01 controls the left
    side of the robot and pwm02 controls the right
    side of the robot. It is assumed that a pwm
    value of 254 will cause all wheels to move in the
    forward direction and that a value of 0 will
    result in all wheels turning backwards. A pwm
    value of 127 will result in no movement.
  • Three light sensors for detecting tape are
    labeled s_left, s_middle and s_right.
  • Figure 1 Simple Robot

65
Autonomous Code in Default Program
  • The default autonomouse code is listed below in
    Listing 1. This function User_Autonomous_Code()
    will be called every 26.2 mSec. Any code written
    should be placed in the space between the two
    lines that read GetData(rxdata) and
    PutData(txdata). (BOLD Print)
  • NOTE The Operator Interface panel should always
    be placed into Disabled mode and then the Robot
    Controller should be reset each time the
    following auto modes are tested. This results in
    critical variables being initialized. Dont
    forget this or you may get some whacky results.
  • void User_Autonomous_Code(void)
  • while (autonomous_mode) / DO NOT CHANGE! /
  • if (statusflag.NEW_SPI_DATA) / 26.2ms
    loop area /
  • Getdata(rxdata) / DO NOT DELETE, or
    you will be stuck here forever! /
  •  
  • / Add your own autonomous code here. /
  •  
  • Putdata(txdata) / DO NOT DELETE, or
    you will get no PWM outputs! /

66
Program Listing 1 Default Autonomous Mode
  • The code in Listing 1 is a valid autonomous mode
    without any additional code. This auto mode does
    absolutely nothing. Granted, it is not as
    exciting to watch, but on occasions there times
    when the best thing to do may be to do absolutely
    nothing for 15 seconds.
  • Driving Straight
  • Remember, any code listed in any further listings
    should be placed between the GetData() and the
    PutData() lines in the autonomous function listed
    in Listing 1.
  •  
  • pwm01 160
  • pwm02 160

67
Program Listing 2 Driving Straight
  • During the entire 15 second duration, the code in
    Listing 2 will drive the robot straight ahead.
    This assumes all things being equal such as motor
    characteristics, drive system, etc.
  • The problem with this mode of operation is that
    should the robot collide with an obstacle or
    another robot, the controller will continue to
    drive the motors for 15 seconds. However, if
    there are no obstacles, then this robot should
    keep on driving.
  • Increasing or Decreasing the Distance Traveled in
    a Straight Line
  • If the robot goes too far using the code in
    Listing 2, then reduce the pwm values from 160 to
    something smaller such as 150. It will be
    necessary to experiment with these values.
  • If the robot does not go far enough then the pwm
    values can be increased to 170. Again
    experimentation with different values are key.
  • The driving surface must also be taken into
    consideration. Programming and testing on a hard
    surface will usually produce different results
    then driving on carpet. It may be necessary to
    increase the magnitude of the pwm values for
    operation on a carpet.

68
More Precise Timing
  • The auto code function is called by the
    controller every 26.2 mS. That is 1 / .0262
    seconds or 38.17 times each second. For the
    purposes of this handout, it will be said that
    the auto function is called 38 times each second.
    It will be called 19 times in one-half second.
    With this in mind, it is possible to write the
    following code to allow the robot to drive
    straight for 3 seconds and then stop and wait for
    manual operation to begin. NOTE 3 seconds x 38
    loops/second 114 loops.
  • Remember to put this code between the PutData()
    and GetData() Also, any other code added from a
    previous listing in this handout should be
    deleted so it will not interfere.
  • //place this line before while
    (autonomous_mode).
  • static unsigned int t
  • //place this after Getdata(rxdata)
  • t
  • if (t lt 114)
  • pwm01 160
  • pwm02 160
  • else
  • pwm01 127
  • pwm02 127

69
Program Listing 3 Drive Straight for 3 Seconds
and Stop
  • A static variable is created named t. Each
    time the function is called (every 26.2 mS) t is
    incremented by a value of 1. As long as the
    value is less than 114 (corresponds to 3 seconds)
    values of 160 will be sent to both variable speed
    controllers. 
  • When the value of t is 114 or greater, the
    motors both stop.
  • Remember, the keyword static is provided so
    that the function will not forget the previous
    t value. Without the keyword static the robot
    would drive forward for the entire time in
    autonomous.
  • Go Forward, Stop, Go Backward, Stop
  •  
  • Imagine it is necessary to go forward for 3
    seconds, stop for 9 seconds and go in reverse for
    an additional 3 seconds.
  •  
  • First, lets figure out how many loops correspond
    to these time marks. See Table 1.
  • Time
  • Variable t
  • 0 seconds
  • 0
  • 3 seconds
  • 114 (3 seconds x 38 loops/second)
  • 12 seconds (3 seconds 9 seconds)
  • 456 (12 seconds x 38 loops/second)
  • 15 seconds (12 seconds 3 seconds)
  • 570 (15 seconds x 38 loops/second

70
Table 1 Variable Values
  • //place this line before while
    (autonomous_mode).
  • static unsigned int t 
  • //place this after Getdata(rxdata)
  • t
  • if (t lt 114) // between 0 and 3 seconds, go
    forward
  • pwm01 160
  • pwm02 160
  • else if (t lt 456) //between 114 and 456, stop
  • pwm01 127
  • pwm02 127
  • else if (t lt 570)//between 456 and 570, go
    backwards
  • pwm01 94
  • pwm02 94

71
Program Listing 4 Drive Straight, Stop and Go
Backwards
  • As long as the value of t is less than 114, the
    motors will drive forward. After the t value is
    114 to less than 456, the motors will stop. When
    the t value is 456 to less than 570 the motors
    will drive backwards, After this 15 seconds, the
    motors will stop.
  •  
  • Now technically, the code should never be
    executed in the else loop. However, for safety
    and completeness, I am including it. Also, the
    if and else if code above could be rewritten to
    include AND . Remember, there are lots of
    ways to do this. I am trying to provide a nice
    clean layout for newbies.
  •  

72
Executing a Left Turn
  • Imagine the robot must perform a sharp left turn
    from a stopped position. Through
    experimentation, you determine that the left
    motor will be driven backwards using a pwm value
    of 95 and that the right motor will be driven
    forward using a pwm value of 150. Additionally,
    it is determine that it takes 2.5 seconds to turn
    exactly 90 degrees. After the robot completes
    the turn, the robot will stop and wait the
    remaining time.
  •  
  • First, lets figure out how many loops correspond
    to these time marks.
  •  
  • Time
  • Variable t TimeVariable t
  • 0 seconds 02.5 seconds95 (2.5 seconds x 38
    loops/second)15 seconds (2.5 seconds 12.5
    seconds)570 (15 seconds x 38 loops/second

73
Table 2 Variable Values
  • //place this line before while
    (autonomous_mode).
  • static unsigned int t
  •  
  • //place this after Getdata(rxdata)
  • t
  •  
  • if (t lt 95) // between 0 and 2.5 seconds, go
    forward
  • pwm01 95 //left motor goes in reverse
  • pwm02 150 //right motor goes forward
  • else if (t lt 570)//between 95 and 570, stop
  • pwm01 127
  • pwm02 127
  • else // 570 or greater, stop
  • pwm01 127

74
Program Listing 5 Turning Left from a Stopped
Position
  • In Listing 5, the else if (t lt 570) code is not
    necessary. However it is included here for
    completeness. 
  • Completing a Complex Manuever
  • Figure 2 Drive Path
  • Refer to Figure 2. This drawing represents a
    drive path that the robot must execute in
    autonomous mode. The bold black lines represents
    reflective tape on the course. However the tape
    will not be used in this example. It simply
    indicates the desired robot path.
  • For this example, the robot will follow the time
    and pwm values listed in Table 3.
  • Time (Seconds)
  • Variable loops
  • pwm01 (left motor)
  • pwm02 (right motor)
  • 0 (turn right)
  • 0
  • 95
  • 150
  • 2.5 (go straight)
  • 95
  • 150
  • 150
  • 7.5 (turn right 2.5 5)
  • 285

75
Table 3 Drive Path Values
  • Important!!! The following code requires the
    motors to shift directions immediately. This is
    generally bad on the system as it will generate
    all sorts of spikes on the electrical system. It
    is recommended to slow the motors for a moment
    and then shift directions.
  • //place this line before while
    (autonomous_mode).
  • static unsigned int t
  •  
  • //place this after Getdata(rxdata)
  • t
  •  
  • if (t lt 95) //turn left for 2.5 seconds
  • pwm01 95 //left motor goes in reverse
  • pwm02 150 //right motor goes forward
  • else if (t lt 285) //go straight for 5 seconds
  • pwm01 150
  • pwm02 150
  • else if (t lt 380) //turn right for 2.5 seconds

76
Program Listing 6 Turning Left, Go Straight,
Turn Right and Stop
  • Line Tracking Problem
  •  
  • For this example, ensure three light sensors are
    connected to the following digital inputs in
    Table 4.
  • s_left
  • rc_dig_in01
  • s_middle
  • rc_dig_in02
  • s_right
  • rc_dig_in03
  •  
  • Table 4 Light Sensor Connection to RC Digital
    Inputs
  •  
  • Three sensors are mounted next to each other and
    perpendicular to the line path. The sensors are
    assumed to start on a straight line and the robot
    starts by moving forward. (Refer to Figure 1 for
    sensor placement). It is also assumed that the
    sensors are close enough that the reflective tape
    will not be hidden between sensors. One sensor
    must be made at all times. For example if the
    tape is 2 wide, then the center of the sensor
  •  
  • Figure 3 will be used for explaining some basic
    line following concepts. The robot will start at
    the position indicated by Position A. In this
    position, the robot is straddling the line and
    only the middle sensor is on. The outer sensors
    are off.

77
(No Transcript)
78
Figure 3 Line Sensors
  • Starting at position A, imagine the robot is
    moving straight up. In positions A and B only the
    middle sensor is being made. At position C, both
    the left and middle sensors are being made. The
    robot continues to move straight to position D in
    which only the left sensor is made. Eventually,
    as the robot travels, none of the sensors will be
    made.
  •  
  • Regarding positions A and B, the robot believes
    it is still centered over the line. The robot
    should drive straight.
  •  
  • Position C suggests that the line is starting to
    move to the left because both sensors (L and M)
    are on. The response of the robot should be to
    steer to the right.
  •  
  • Position D suggests that the line has move
    somewhat significantly to the left because only
    the L sensor is on and th middle sensor is no
    longer sensing the line. This generally indicates
    that the robot is even further off course. The
    correction to this condition should be a greater
    turn to the right.
  • Position E is really challenging. What should
    the robot do when no sensors are made? Should
    the robot back up until a line is detected?
    Should it drive in circles until a line is
    detected or should it simply stop to prevent a
    collision and risking possible damage to the
    robot.
  •  
  • If the history of the robots movement along the
    path is known, it is possible to make a better
    decision. In the example above in Figure 3, the
    robot needs to turn to the left quite sharply
    until a sensor is made.
  •  
  • Program Listing 7 provides code for all
    positions, left and right side of the line with
    the exception of position E. This will be covered
    in subsequent listings.
  •  
  • The following pwm values in Table 5 will be used
    for each of the desired operations. Again, these
    values should be adjusted by experimenting with
    required speeds and floor surface for each robot.
    NOTE Reversing motor direction when driving in
    a forward direction to minimize stress upon the
    motors and the drive system.

79
(No Transcript)
80
Table 5 PWM Values for Various Operations
  •  
  • No timing variable will be used to count loops.
    This robot will respond only to sensors. If no
    sensors are made this robot will stop. 1 means
    the sensor is made. 0 means the sensor is off.
  •  
  • This code has worked very well for following
    straight lines. It can be tweaked (changing pwm
    value) to allow for smoother line following.
  •  
  • //place this line before while
    (autonomous_mode).
  • unsigned char s_left rc_dig_in01
  • unsigned char s_middle rc_dig_in02
  • unsigned char s_right rc_dig_in03
  •  
  • //place this after Getdata(rxdata)
  • if (s_left 0 s_middle 1 s_right 0)
    //middle //sensor on, go straight
  • pwm01160 //left motor
  • pwm02160 //right motor
  • else if (s_left 1 s_middle 1 s_right
    0) // turn //right
  • pwm01160 //left motor

81
Continued
  • else if (s_left 0 s_middle 1 s_right
    1) // turn //left
  • pwm01127 //left motor
  • pwm02160 //right motor
  • else if (s_left 0 s_middle 0 s_right
    1) // turn //hard left
  • pwm01127 //left motor
  • pwm02180 //right motor
  • else //stop
  • pwm01127 //left motor
  • pwm02127 //right motor

82
Program Listing 7 - All Sensor Conditions Except
All Off
  • Note, in actual tests with robots, a value of 30
    had to be added to the the values 160 and 180 to
    yield 190 and 210 in order for this code to work.
  • Now there are numerous possible outcomes of using
    the above program in 1000 different robots.
    Robots will respond differently. Depending upon
    the motors used, the motors should be balanced,
    the amount of torque to get started is different
    then the amount of torque to continue moving.
    The above code is not guarantee to work as
    written. The values will most likely need
    correcting.
  • Line Tracking No Sensors are Made
  • In order to figure out the correct action based
    upon no sensors being made it important to know
    what the last sensor condition was before they
    all went out.
  •  
  • Binary weighted values will be used to store the
    the entire sensor configuration into a single
    variable. The left sensor, if on, will be
    multiplied by 4. The middle snesor, if on, will
    be multipled by 2 and the right sensor, if on,
    will be multiplied by 1.

83
(No Transcript)
84
Table 6 Binary Weight Values for Sensors
  •  
  • The s_last values are from 0 to 7. They are
    calculated by adding the values in the respective
    row from each sensor. The code to accomplish
    this is written as follows.
  •  
  • s_last (s_left 4) (s_middle 2) s_right
  •  
  • The code below is designed to allow the robot to
    turn corners. Remember, the pwm values will most
    likely need changed to make this program work
    better.
  •  
  • Code typed in BOLD below in Listing 8 should be
    added to the code in Listing 7 as indicated.
  •  
  • //place this line before while
    (autonomous_mode).
  • static unsigned char s_last
  • unsigned char s_left rc_dig_in01
  • unsigned char s_middle rc_dig_in02
  • unsigned char s_right rc_dig_in03
  •  
  • //place this after Getdata(rxdata)
  • if (s_left 1 s_middle 1 s_right 1)
  • s_last (s_left 4) (s_middle 2) s_right

85
  • else if (s_left 1 s_middle 1 s_right
    0) // turn //right
  • pwm01160 //left motor
  • pwm02127 //right motor
  • else if (s_left 1 s_middle 0 s_right
    0) // turn //hard right
  • pwm01180 //left motor
  • pwm02127 //right motor
  • else if (s_left 0 s_middle 1 s_right
    1) // turn //left
  • pwm01127 //left motor
  • pwm02160 //right motor
  • else if (s_left 0 s_middle 0 s_right
    1) // turn //hard left
  • pwm01127 //left motor

86
Program Listing 8 - All Sensor Conditions
  • Autonomous Mode Using Predefined Values at 1
    Second Intervals
  •  
  • First, add one line of code (BOLD) to the
    following in user_routines_fast.c.
  •  
  •  
  • void User_Autonomous_Code(void)
  • while (autonomous_mode) / DO NOT CHANGE! /
  • if (statusflag.NEW_SPI_DATA) / 26.2ms
    loop area /
  • Getdata(rxdata) / DO NOT DELETE, or
    you will be stuck here forever! /
  •  
  • / Add your own autonomous code here. /
  • AutoDrive()
  • Putdata(txdata) / DO NOT DELETE, or
    you will get no PWM outputs! /

87
  • Second, create a file named auto.h and add it
    to the project. Add this code to the auto.h
    file.
  •  
  • include "ifi_aliases.h"
  • include "ifi_default.h"
  • include "ifi_utilities.h"
  •  
  • void AutoDrive (void)
  •  
  • Third, create another file named auto.c and add
    it to the project. Add this code to the auto.c
    file.
  •  
  • include "auto.h"
  •  
  • unsigned char p1 127,150,150,150,150,150,150,
    150,150,127
  •  
  • void AutoDrive (void)
  • static unsigned char i
  • static unsigned char k

88
Program Listing 9 Predefined Values
  • Explanation. The array p1 stores 10 values
    representing desired pwm01 commands. The
    function AutoDrive is called every 26.2 mSec.
    The variables i and k are remembered because of
    the keyword static. Every 38 times, k is gt
    then t. The pwm01 command is set to the value
    in the array at position i . This repeats
    until the array is exhausted. At this time the
    pwm01 is set to 127. Modify the value of t to
    see the results.
  •  
  • The value of pwm01 will be updated every (t x
    26.2 mS). In this case t 38. Time is 38 x
    26.2 mSec or 0.99 seconds (approximately 1
    second).

89
Conclusion
  • This handout hopefully has given you some ideas
    and possibly some help. Good luck!!
Write a Comment
User Comments (0)
About PowerShow.com