background image

directly led to that intersection. The units of the segment times were chosen to provide numbers that
can allow the robot to meaningfully differentiate between longer and shorter segments but that never
exceed 255 for any segment in the maze. This second restriction means that the values can be stored
in an array of unsigned chars (i.e. each segment’s time takes up just one byte of memory), which
helps keep memory usage down. The ATmega168 has just 1024 bytes of RAM, so it’s important that
applications like this store data in an efficient way that leaves enough room for the stack, which is also
stored in RAM. A good rule of thumb is to leave 300 – 400 bytes of RAM available for the stack and
data used by the Pololu AVR library (or more if you have some deeply nested functions or functions
with a lot of local variables). Note that the ATmega328 has 2048 bytes of RAM, which gives you a bit
more room for your data.

Once the 3pi has learned the maze, the maze-driving algorigthm is essentially:

1. If the robot is going straight at the next intersection, drive the current segment at high speed;

don’t even worry about slowing down until we know we have an intersection coming up that
will require a turn.

2. Otherwise, drive the current segment at high speed until time

T

has elapsed, at which point

slow back down to normal speed until the next intersection is reached.

The value

T

is computed from a function that uses the previously measured segment “length”. For

short segments,

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 just in time to handle the intersection safely. We came up with a function for

T

on paper and then

ran a series of tests to get the various constants right.

Typically, one might use encoders to measure the lengths of the segments. We were able to just use
timing on the 3pi, however, because of the 3pi’s power system, which uses a regulated voltage for the
motors and produces highly repeatable results. With a more traditional power system, motor speed
would decrease as the batteries discharge, and a timing approach like this would potentially produce
unreliable results. For example, if you were to use a robot with a more traditional power system, the
function you come up with for

T

when the batteries are freshly charged might work poorly when they

are nearly drained.

Tip:

Once you start significantly increasing your maze-solving speed, performance becomes

dependent on the traction of the tires. Unfortunately, traction decreases over time as the tires pick up
dust and dirt from the course. Our fast maze solver needs to have its tires cleaned every few runs or
else it starts fishtailing on the turns, which slows it down and can even cause it to mess up. You can
see the effects of this on the second (solution) run of the video (the tires hadn’t been cleaned recently).
You can easily clean the tires by wiping them with a little rubbing alcohol on a paper towel.

Pololu 3pi Robot User’s Guide

© 2001–2019 Pololu Corporation

8. Example Project #2: Maze Solving

Page 53 of 85

Содержание 0J5840

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 24: ...Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 5 How Your 3pi Works Page 24 of 85...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 67: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 67 of 85...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 77: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 77 of 85...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Страница 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...

Отзывы: