background image

This line declares the

turn()

function without actually including a copy of its code. To access the

declaration, each C file that needs to call

turn()

adds the following line:

Note the double-quotes being used instead of angle brackets. This signifies to the C compiler that
the header file is in the project directory, rather than being a system header file like

3pi.h

. Always

remember to put the code for your functions in the C file instead of the header file! If you do it the other
way, you will be making a separate copy of the code in each file that includes the header.

The file

follow-segment.c

also contains a single function,

follow_segment()

, which will drive 3pi

straight along a line segment until it reaches an intersection or the end of the line. This is almost the
same as the line following code discussed in

Section 7

, but with extra checks for intersections and

the ends of lines. Here is the function:

1

#include "turn.h"

?

Pololu 3pi Robot User’s Guide

© 2001–2019 Pololu Corporation

8. Example Project #2: Maze Solving

Page 39 of 85

Summary of Contents for 0J5840

Page 1: ...Pololu 3pi Robot User s Guide Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation https www pololu com docs 0J21 all Page 1 of 85...

Page 2: ...Following 27 7 a About Line Following 27 7 b A Simple Line Following Algorithm for 3pi 27 7 c Advanced Line Following with 3pi PID Control 35 8 Example Project 2 Maze Solving 37 8 a Solving a Line Maz...

Page 3: ...more all connected to a user programmable AVR microcontroller The 3pi measures approximately 3 7 inches 9 5 cm in diameter and weighs 2 9 oz 83 g without batteries The 3pi is based on an Atmel ATmega1...

Page 4: ...r from you about any of your projects and about your experience with the 3pi robot You can contact us https www pololu com contact directly or post on our forum http forum pololu com Tell us what we d...

Page 5: ...our 3pi on surfaces that might scrape or damage the underside of your robot s PCB as it drives around Avoid placing the robot so that the underside of the PCB makes contact with conductive materials e...

Page 6: ...it out of the box adding batteries and turning it on The 3pi ships with a demo program that will give you a brief tour of its features General features of the Pololu 3pi robot top view Pololu 3pi Robo...

Page 7: ...a local store If you use rechargeable batteries you will also need a battery charger Battery chargers designed to connect to external series battery packs such as the iMAX B6AC https www pololu com pr...

Page 8: ...s orientation as you will want to plug it back in this way when you are done With the LCD removed your 3pi should look like the picture to the right Once the batteries are in place you should return...

Page 9: ...nsors will cause the corresponding reading to drop visibly on the graph This demo also displays C to indicate that button C has an effect press C and the IR emitters will be turned off In indoor light...

Page 10: ...EDs https www pololu com category 20 leds if you want more color brightness options Note that you should only add LEDs if you are comfortable soldering and you should take care to avoid desoldering an...

Page 11: ...s are pushed which we measure in volts V the units of electric potential An NiMH battery has a voltage of about 1 2 V To understand how much power you can get out of a battery you also need to know ho...

Page 12: ...her ground If they stand next to each other working in parallel they will be able to lift the water to the same height as before while delivering twice the amount of water If one of them stands uphill...

Page 13: ...ould permanently disable your 3pi 5 b Power management Battery voltage drops as the batteries are used up but many electrical components require a specific voltage A special kind of component called a...

Page 14: ...A desktop computer power supply also uses switching regulators peek through the vent in the back of your computer and look for a donut shaped piece with a coil of thick copper wire wrapped around it...

Page 15: ...s built in to the 3pi Three resistors shown in the circuit at right comprise a voltage divider that outputs a voltage equal to two thirds of the battery voltage which will always be safely below the m...

Page 16: ...ce and distance for example a motor that produces 6 oz in of torque can product a force of 6 oz with a 1 inch lever arm 3 oz with a 2 inch lever and so on Multiplying the torque and speed measured at...

Page 17: ...iving forward is 2 6 0 67 18 oz The 3pi weighs about 7 oz with batteries so the motors are strong enough to lift the 3pi up a vertical slope or accelerate it at 2 g twice the acceleration of gravity T...

Page 18: ...driver chip the TB6612FNG and output ports of the main microcontroller operate the switches through this chip Here is a table showing how output ports PD5 and PD6 on the microcontroller control the t...

Page 19: ...he set_motors function in the Pololu AVR Library see Section 6 for more information lets you set the duty cycle and it uses 8 bit precision a value of 255 corresponds to 100 duty cycle For example to...

Page 20: ...1 but pressing the button connects the input to ground 0 V through a 1 k resistor which is much lower than the value of R This brings the input voltage very close to 0 V so the pin reads as a 0 Withou...

Page 21: ...our program The way it works is that the pin is set to an output and driven high 5 V to charge the output node The pin is then set to an input and the voltage falls as current flows through the photot...

Page 22: ...verflows dropping back to zero Upon detecting a transition from a 1 to a 0 on one of the sensors by measuring a change in the input port PINC the code determines which sensor changed and records the t...

Page 23: ...d 5 e 3pi Simplified Schematic Diagram A full understanding of how your 3pi works cannot be achieved without first understanding its schematic diagram Pololu 3pi Robot User s Guide 2001 2019 Pololu Co...

Page 24: ...Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 5 How Your 3pi Works Page 24 of 85...

Page 25: ...can download a pdf version of the schematic here https www pololu com file 0J119 3pi schematic pdf 481k pdf Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 5 How Your 3pi Works Page 25 of...

Page 26: ...d it s user s guide here https www pololu com docs 0J67 You might also check out our original Pololu AVR Programming Quick Start Guide https www pololu com docs 0J51 for tutorials on how to get starte...

Page 27: ...dollars in a couple of hours at home For information on building your own course see our tutorial on Building Line Following and Line Maze Courses https www pololu com docs 0J22 7 b A Simple Line Foll...

Page 28: ...ee whether the line sensors are working properly before starting the robot For more information on this and other LCD commands see Section 5 of the command reference https www pololu com docs 0J18 5 W...

Page 29: ...the algorithm causes a lot of unnecessary shaking on the curves At this point you might want to work on trying to adjust and improve this algorithm before moving on to the next section Some ideas for...

Page 30: ...entire source code to this simple line following program is presented below for your reference Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 7 Example Project 1 Line Following Page 30 of...

Page 31: ...ace h Introductory messages The PROGMEM identifier causes the data to go into program space const char welcome_line1 PROGMEM Pololu const char welcome_line2 PROGMEM 3 xf7 Robot const char demo_name_li...

Page 32: ...the range of 0 to 1000 and 1000 101 is 9 with integer math char c display_characters calibrated_values i 101 Display the bar graph character print_character c Initializes the 3pi displays a welcome me...

Page 33: ...LEDs will be turned on during the reading which is usually what you want calibrate_line_sensors IR_EMITTERS_ON Since our counter runs to 80 the total delay will be 80 20 1600 ms delay_ms 20 set_motors...

Page 34: ...f the line turn left Set the right motor to 100 and the left motor to zero to do a sharp turn to the left Note that the maximum value of either motor speed is 255 so we are driving it at just about 40...

Page 35: ...tive number and to the right of the line it will be negative This is computed from the result returned by read_line simply by subtracting 2000 The integral value records the history of your robot s mo...

Page 36: ...0 try increasing the speed You ll probably need to readjust the parameters as the maximum speed increases By gradually increasing the maximum speed and tuning the parameters see if you can get your 3p...

Page 37: ...a looped maze since a simple strategy allows you to explore the entire maze We ll talk about that strategy in the next section We also usually construct our mazes using only straight lines drawn on a...

Page 38: ...right turns and U turns Straight turns are also handled by this function though they don t require us to take any action The motor speeds and the timings for the turns are parameters that needed to be...

Page 39: ...in the C file instead of the header file If you do it the other way you will be making a separate copy of the code in each file that includes the header The file follow segment c also contains a singl...

Page 40: ...ings m1 m2 If this is a positive number the robot will turn to the left If it is a negative number the robot will turn to the right and the magnitude of the number determines the sharpness of the turn...

Page 41: ...he right and start walking back the way you came Eventually as long as there are no loops your hand will travel along each length of wall in the entire labyrinth exactly once and you ll find your way...

Page 42: ...rse decide how to turn and record the turn in the path variable To pass the correct arguments to select_turn we need to carefully examine the intersection as we cross it Note that there is a special e...

Page 43: ...rs 4 100 found_right 1 Drive straight a bit more this is enough to line up our wheels with the intersection set_motors 40 40 delay_ms 200 Check for a straight exit read_line sensors IR_EMITTERS_ON if...

Page 44: ...encounter a dead end we can simplify the path to something shorter Consider the sequence LBL where B stands for back and is the action taken when a dead end is encountered This is what happens if the...

Page 45: ...ld be replaced with a single R In fact whenever we have a sequence like xBx we can replace all three turns with a turn corresponding to the total angle eliminating the U turn and speeding up our solut...

Page 46: ...unter a sequence xBx we can simplify it by cutting out the dead end For example LBL S because a single S bypasses the dead end represented by LBL void simplify_path only simplify the path if the secon...

Page 47: ...ist to represent the shortest path from start to finish by weeding out all of the dead ends One option is to perform this pruning when we finish the maze but the better approach is to perform the prun...

Page 48: ...st of actions In this case the most recent actions in our list is the sequence SBL and the diagram shows that this sequence can be simplified into a single right turn R Prune out the rest of this dead...

Page 49: ...s with the next action to produce the sequence LBL which reduces to a single straight S Prune out the final dead end branch to leave us with the shortest path from start to finish Pololu 3pi Robot Use...

Page 50: ...from start to finish As we drove the maze our action list would have looked like the following 1 L 2 LS 3 LSB 4 LSBL LR pruning occurs here 5 LRB 6 LRBL LB pruning occurs here 7 LBL S pruning occurs...

Page 51: ...ing speed Identifying situations where the robot has gotten lost Adjusting the speed based on what is coming up e g driving straight through an S at full speed The following video shows a 3pi prototyp...

Page 52: ...ugh to survive the intersections led to unnecessarily slow driving on long straight segments however Our solution was to time the length of every segment the robot encountered during the learning phas...

Page 53: ...egments T is negative and the 3pi just drives the entire segment at normal speed For longer segments T is positive and causes the 3pi to drive most of the segment at high speed before slowing down jus...

Page 54: ...9 Pin Assignment Tables General features of the Pololu 3pi robot top view Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 9 Pin Assignment Tables Page 54 of 85...

Page 55: ...Labeled bottom view of the Pololu 3pi robot Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 9 Pin Assignment Tables Page 55 of 85...

Page 56: ...Specific features of the Pololu 3pi robot top view Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 9 Pin Assignment Tables Page 56 of 85...

Page 57: ...shbuttons left to right x3 PB1 PB4 and PB5 digital inputs 9 12 and 13 buzzer PB2 digital pin 10 LCD control RS R W E PD2 PB0 and PD4 digital pins 2 8 and 4 LCD data 4 bit DB4 DB7 PB1 PB4 PB5 and PD7 d...

Page 58: ...Pin Assignment Table Sorted by Pin Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 9 Pin Assignment Tables Page 58 of 85...

Page 59: ...3 M2 control line Timer2 PWM output A OC2A ISP programming line PB4 LCD data line DB5 user pushbutton pressing pulls pin low Caution also an ISP programming line PB5 LCD data line DB6 user pushbutton...

Page 60: ...t jumpered to 2 3rds of battery voltage ADC input channel 6 ADC6 ADC7 dedicated analog input jumpered to user trimmer potentiometer ADC input channel 7 ADC7 reset reset pushbutton internally pulled hi...

Page 61: ...y another processor It is easy to add more commands yourself or adapt the library to work on a different board Note that we offer several basic expansion kits on which you can mount such a secondary m...

Page 62: ...dule XBee Wixel https www pololu com product 1337 Bluetooth etc Please note that the m3pi robot is also available fully assembled Please see the m3pi robot product page https www pololu com product 21...

Page 63: ...e commands result in the 3pi sending data back out to the controlling device For commands where integers are sent back the least significant byte is sent first little endian If bad commands or data by...

Page 64: ...3 play music 2 101 0 Plays a tune specified by a string of musical commands The first data byte specifies the length of the following string max length 100 so that the slave program knows how many mor...

Page 65: ...ursor to x y coordinates given by the next two bytes 0xBA autocalibrate 0 1 Turns the robot left and right while calibrating For use when the robot it positioned over a line Returns the character c wh...

Page 66: ...to 127 full reverse 0xC5 M2 forward 1 0 Sets motor M2 turning forward with a speed of 0 off up to 127 full speed 0xC6 M2 backward 1 0 Sets motor M2 turning backward with a speed of 0 off up to 127 ful...

Page 67: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 67 of 85...

Page 68: ...ition with serial interrupts running in the background serial_set_mode SERIAL_AUTOMATIC unsigned int position read_line sensors IR_EMITTERS_ON serial_set_mode SERIAL_CHECK The proportional term should...

Page 69: ...ceiving bytes Calls pid_check to keep following the line char read_next_byte while serial_get_received_bytes read_index serial_check play_check pid_check takes some time only run it if we don t have m...

Page 70: ...useful as an initial command void send_signature serial_send_blocking 3pi1 0 6 set_motors 0 0 pid_enabled 0 Reads the line sensors and sends their values This function can do either calibrated or unc...

Page 71: ...rial_send_blocking char message 2 Sends the trimpot value 0 1023 void send_trimpot int message 1 message 0 read_trimpot serial_send_blocking char message 2 Sends the battery voltage in millivolts void...

Page 72: ...buffer 100 Plays a musical sequence void do_play unsigned char tune_length read_next_byte if check_data_byte tune_length return unsigned char i for i 0 i tune_length i if i sizeof music_buffer avoid o...

Page 73: ...x return unsigned char y read_next_byte if check_data_byte y return lcd_goto_xy x y Runs through an automatic calibration sequence void auto_calibrate time_reset set_motors 60 60 while get_ms 250 cali...

Page 74: ...2 kbaud serial_set_baud_rate 115200 serial_set_mode SERIAL_CHECK serial_receive_ring buffer 100 while 1 wait for a command char command read_next_byte The list of commands is below add your own simply...

Page 75: ...7 case char 0xB4 calibrate_line_sensors IR_EMITTERS_ON send_calibrated_sensor_values 1 break case char 0xB5 line_sensors_reset_calibration break case char 0xB6 send_line_position break case char 0xB7...

Page 76: ...d by the signature of the slave source code e g 3pi1 0 The master will then instruct the slave to display Connect and play a short tune Pressing the B botton on the master causes the slave to go throu...

Page 77: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 77 of 85...

Page 78: ...0b11111 0b11111 0b11111 0b11111 0b11111 0b11111 This function loads custom characters into the LCD Up to 8 characters can be loaded we use them for 6 levels of a bar graph plus a back arrow and a musi...

Page 79: ...ard message 3 speed2 serial_send_blocking message 4 do calibration void slave_calibrate serial_send xB4 1 int tmp_buffer 5 read 10 characters but we won t use them serial_receive_blocking char tmp_buf...

Page 80: ...cking string length go to coordinates x y on the slave LCD void slave_lcd_goto_xy char x char y serial_send_blocking xB9 1 serial_send_blocking x 1 serial_send_blocking y 1 int main char buffer 20 loa...

Page 81: ...ters if serial_receive_blocking buffer 10 100 break get the line position serial_send xB6 1 int line_position 1 if serial_receive_blocking char line_position 2 100 break get the battery voltage serial...

Page 82: ...the two motors labeled PD0 PD1 ADC6 ADC7 and PC5 If you are using an expansion kit these lines are brought up to the expansion PCB Pins PC5 ADC6 and ADC7 are all connected to 3pi hardware via removabl...

Page 83: ...whatever you connect to them Pins PB1 PB4 and PB5 connect to the user pushbuttons and PD7 connects to the green user LED It is important to note that PB4 and PB5 are also programming lines so you mus...

Page 84: ...mel Studio 7 https www microchip com avr support atmel studio 7 AVR Libc Home Page http www nongnu org avr libc ATmega328P documentation http www atmel com devices atmega328p aspx ATmega168 documentat...

Page 85: ...it unsafe to configure the AVR s ADC to use the internal voltage reference Newer 3pi robots disconnect AREF from AVCC and add capacitor C32 between AREF and GND making it safe to use the internal volt...

Reviews: