Home

Pololu - Pololu AVR C/C++ Library User`s Guide

image

Contents

1. 6 Example programs Page 27 of 47 Pololu AVR C C Library User s Guide 2001 2010 Pololu Corporation while 1 note that the following line could also be accomplished with int pot analogRead 7 int pot read_trimpot determine the trimpot position int motorSpeed pot 2 256 turn pot reading into number between 256 and 255 if motorSpeed 256 motorSpeed 255 256 is out of range set_motors motorSpeed motorSpeed int ledDelay motorSpeed if ledDelay lt 0 ledDelay ledDelay make the delay a non negative number ledDelay 256 ledDelay the delay should be short when the speed is high red_led 1 turn red LED on delay ms ledDelay red_led 0 turn red LED off delay ms ledDelay 2 motors2 Demonstrates controlling the motors using the trimmer potentiometer but it uses the LCD for most of the feedback so it will not fully work on the Baby Orangutan include lt pololu orangutan h gt motors2 for the 3pi robot Orangutan LV 168 Orangutan SVP and Orangutan SV xx8 This example uses the OrangutanMotors and OrangutanLCD libraries to drive motors in response to the position of user trimmer potentiometer and to display the potentiometer position and desired motor speed on the LCD It uses the OrangutanAnalog library to measure the trimpot position and it uses the OrangutanLEDs library to provide limited feedback with the red and gree
2. char prevMood 5 int main lcd load custom character happy 0 lcd load custom character sad 1 lcd load custom character indifferent 2 lcd load _ custom character surprised 3 lcd load custom character mocking 4 clear this must be called before we can use the custom characters print moods 2 initialize the random number generator based on how long they hold the button the first time wait_for button press ALL BUTTONS long seed 0 while button _ is pressed ALL BUTTONS 6 Example programs Page 25 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation seed srandom seed while 1 lcd_goto_xy 6 0 move cursor to the correct position char mood do mood random 5 while mood prevMood ensure we get a new mood that differs from the previous prevMood mood print_character mood print a random mood character wait_for button ALL BUTTONS wait for any button to be pressed return 0 6 g Orangutan LED Control Functions Overview These functions allow you to easily control the user LED s on the 3pi Robot http www pololu com catalog product 975 Orangutan SYV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 Orangutan LV 168 http www pololu com catalog product 775 and Baby Orangutan B http www pololu com catalog product 1220 On the Orangut
3. gt Search File Edit View Tools Help Folders a Li WinAVR 20080430 4 Lar bin a include gt Wb lib de bin gt de doc gt de lib gt Je libexec mfile gt de pn de sample D de share gt source gt I utils Date modified Type E 3pi h _ analog h buzzer h delay h Icd h _ leds h _ motors h _ orangutan _ orangutan h _ OrangutanAnalog h _ OrangutanBuzzer h _ OrangutanDelay h _ OrangutanLCD h _ OrangutanLEDs h _ OrangutanMotors h _ OrangutanPushbuttons h _ Pololu3pi h L PololuQTRSensors h _ pushbuttons h E qtr h The Pololu AVR Library header files installed correctly You are now ready to use the Pololu AVR library The next section provides example programs that are already set up to use the library For information on using the Pololu AVR library in your own programs e g configuring AVR Studio projects to use the library please see Section 7 5 Installation of the Pololu AVR Library Page 9 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 6 Example programs 6 a Example program AVR Studio A very simple demo program for the Orangutan or 3pi is available in the folder examples atmegaxxxX simple test Where atmegaXXX is the model of the microcontroller on your board Double click on the file test aps and the project should open automatically in AVR Studio showing a C file that uses a few basic commands fr
4. AVR Libc Home Page http www nongnu org avr libe ATmega328P documentation http www atmel com dyn products product_card asp PN ATmega328P ATmega324PA documentation http www atmel com dyn products product_card asp PN ATmega324PA ATmega1284P documentation http www atmel com dyn products product_card asp PN ATmega1284P ATmega168 documentation http www atmel com dyn products product_card asp PN ATmegal 68 Tutorial AVR Programming on the Mac http bot thoughts blogspot com 2008 02 avr programming on mac html Finally we would like to hear your comments and questions over at the Pololu Robotics Forum http forum pololu com 8 Additional resources Page 47 of 47
5. When a pin is configured as a digital output the AVR is either driving it low 0 V or high 5 V This means that the pin has a strong electrical connection to either 0 V GND or 5 V VCC An output pin can be used to send data to a peripheral device or supply a small amount of power for example to light an LED Digital inputs When a pin is configured as a digital input the AVR can read the voltage on the pin The reading is always either low 0 or high 1 Basically a low reading means that the voltage is close to 0 V while a high reading means that the voltage is close to 5 V see the DC characteristics section of your AVR s datasheet for details Note that when we talk about absolute voltages in this document we are assuming that the voltage of the ground GND line is defined to be 0 V Every I O pin on the AVR comes with an internal 20 50 kilo ohm pull up resistor that can be enabled or disabled A pull up resistor is a resistor with a relatively high resistance that connects between a pin and the 5 V supply VCC If nothing is driving the pin strongly then the pull up resistor will pull the voltage on the pin up to 5 V Pull up resistors are useful for ensuring that your input pin reaches a well known state when nothing is connected to it If your input pin has nothing connected to it and the pull up resistor is disabled then it is called a floating pin In general it is not recommended to take a digital reading on a floating pi
6. for examples of this class in the Arduino environment which is almost identical to C Complete documentation of this library s methods can be found in Section 5 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Examples This library comes with two examples in libpololu avr examples 6 Example programs Page 23 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 1 led1 Demonstrates shifting the contents of the display by moving the word Hello around the two lines of the LCD include lt pololu orangutan h gt lcdl for the Orangutan controllers and 3pi robot This example uses the OrangutanLCD library to display things on the LCD http www pololu com docs 0J20 http www pololu com http forum pololu com if int main while 1 print Hello display Hello at 0 0 a k a upper left delay ms 200 lcd_scroll LCD_ RIGHT 3 200 shift the display right every 200ms three times clear clear the LCD lcd_goto_xy 3 1 go to the fourth character of the second LCD line print Hello display Hello at 3 1 a k a lower right delay ms 200 lcd _scroll LCD_LEFT 3 200 shift the display left every 200ms three times clear clear the LCD return 0 1 led2 Demonstrates creating and displaying custom characters on the LCD The following picture shows an example of custom characters
7. int position qtr_read_line sensors QTR EMITTERS ON if all three sensors see very low reflectance take some appropriate action for this situation if sensors 0 gt 750 amp amp sensors 1 gt 750 amp amp sensors 2 gt 750 do something Maybe this means we re at the edge of a course or about to fall off a table in which case we might want to stop moving back up and turn around return compute our error from the line position We will make it so that the error is zero when 6 Example programs Page 40 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation the middle sensor is over the line because this is our goal Error will range from 1000 to 1000 If we have sensor 0 on the left and sensor 2 on the right a reading of 1000 means that we see the line on the left and a reading of 1000 means we see the line on the right int error position 1000 RASO int leftMotorSpeed 100 int rightMotorSpeed 100 if error lt 500 the line is on the left leftMotorSpeed 0 turn left if error gt 500 the line is on the right rightMotorSpeed 0 turn right set motor speeds using the two motor speed variables above PID Control The integer value returned by qtr_read_line can be easily converted into a measure of your position error for line following applications as was demonstrated in the previous code sample The function used to gen
8. sequence of servo movements if button is pressed TOP_ BUTTON Set the servo speed to 150 This means that the pulse width will change by at most 15 microseconds every 20 ms So it will take 1 33 seconds to go from a pulse width of 1000 us to 2000 us set_servo_speed 0 150 Slowly move the servo to position 1800 set_servo_target 0 1800 delay _ms 700 Disable the speed limit set_servo_speed 0 0 Make the servo move back to position 1300 as fast as possible set_servo_target 0 1300 6 Example programs Page 33 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation if button is pressed BOTTOM BUTTON While the user holds down the bottom button move the servo slowly towards position 1800 set_servo_speed 0 60 set_servo_target 0 1800 wait for button release BOTTOM BUTTON When the user releases the bottom button print its current position in microseconds and then move it back quickly clear print_long get_servo_position 0 print from program space PSTR xE4s set_servo _speed 0 0 set_servo target 0 1300 Local Variables mode C c basic offset 4 tab width 4 indent tabs mode t end 2 Svp eight servo This example program demonstrates how to control up to eight servos on the Orangutan SVP using the hardware demultiplexer include lt pololu orangutan h gt 5 AAA FF F FF F
9. using them to display a bar graph of sensor readings and a smiley face include lt pololu orangutan h gt get random functions include lt stdlib h gt led2 for the Orangutan controllers and 3pi robot This example uses the OrangutanLCD functions to display custom characters on the LCD Simply push a any user pushbutton to display a new randomly chosen custom mood character http www pololu com docs 0J20 http www pololu com 6 Example programs Page 24 of 47 Pololu AVR C C Library User s Guide 2001 2010 Pololu Corporation http forum pololu com Auf define some custom mood characters include lt avr pgmspace h gt this lets us refer to data in program space i e flash const char happy PROGMEM b00000 the five bits that make up the top row of the 5x8 character 601010 601010 601010 600000 610001 601110 b00000 00000000 Q o B st char sad PROGMEM b00000 601010 b01010 b01010 b00000 b01110 b10001 b00000 o000000o00o Q o 5 st char indifferent PROGMEM b00000 b01010 b01010 b01010 b00000 b00000 601110 b00000 SO O S SO Sar Q o 2 st char surprised PROGMEM b00000 b01010 b01010 600000 b01110 b10001 b10001 601110 o0o000o000o00o0Oo st char mocking PROGMEM b00000 b01010 601010 b01010 b00000 b11111 b00101 b00010 0 0 0 00 0 0 0 5 y
10. 150 Servo a2 Demux output 2 set servo speed 3 150 Servo a3 Demux output 3 set servo speed 4 150 Servo a4 Demux output 4 set_ servo speed 5 150 Servo a5 Demux output 5 set_servo speed 6 150 Servo a6 Demux output 6 set_servo speed 7 150 Servo a7 Demux output 7 set_servo speedB 0 150 Servo b0 pin AO set_servo speedB 1 150 Servo bl pin Al set servo speedB 2 150 Servo b2 pin A2 set servo speedB 3 150 Servo b3 pin A3 set servo speedB 4 150 Servo b4 pin A4 set servo speedB 5 150 Servo b5 pin A5 set_servo_speedB 6 150 Servo b6 pin A6 set_servo speedB 7 150 Servo b7 pin A7 Make all the servos go to a neutral position set_servo_target 0 1300 set servo target 1 300 set servo target 2 300 set_servo_target 3 1300 set_servo target 4 1300 set_servo target 5 1300 set_servo target 6 1300 set_servo _target 7 1300 set_servo targetB 0 1300 set_servo targetB 1 1300 set_servo targetB 2 1300 set_servo targetB 3 1300 set_servo targetB 4 1300 set_servo targetB 5 1300 set_servo targetB 6 1300 set_servo targetB 7 1300 while 1 Loop forever When the user presses the top button execute a pre programmed sequence of servo movements if button is pressed TOP_ BUTTON set_servo_target 0 1800 delay ms 350 set servo target 1 1800 delay ms 35
11. NOTE C 6 SILENT NOTE C 6 SILENT NOTE C 6 6 Example programs Page 17 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation SILENT_NOTE G 5 F_SHARP 5 F 5 D_SHARP 5 SILENT NOTE G_SHARP 4 A 4 C 5 SILENT NOTE A 4 C 5 D 5 SILENT NOTE E FLAT 5 SILENT_NOTE D 5 C 5 y unsigned int duration MELODY LENGTH 100 25 125 125 125 125 125 250 250 250 250 250 Soy DO 250 soy 250 250 125 250 267 Ver LO 250 125 125 1257 290 125r 125 315 S195 L254 2504 375 2504 2000p Zo 250 LO Lor LOT 250 225 25 1257 2507 125 125 379i 2507 W255 1257 1255 LIO 254 125 LAr D253 V25 25 7 257 253 125 25071297 1257 125 2507 125 125 200 507 L007 25 500 2507 125 125 EZ 2507 ZO LL ELO 125i LL 2 ELO LD 125 250 250 125 375 500 y unsigned char currentldx int main run once when the sketch starts currentIdx 0 print Music while 1 run over and over again if we haven t finished playing the song and the buzzer is ready for the next note play the next note if currentIdx lt MELODY LENGTH amp amp is playing play note at max volume play note note currentIdx duration currentldx 15 optional LCD feedback for fun lcd _goto_xy 0 1 go to start of the second LCD line if note currentldx 255 display blank for rests prin
12. O 2001 2010 Pololu Corporation m2Speed 0 set motor speeds using the two motor speed variables above 6 n Pololu Wheel Encoder functions The PololuWheelEncoders class and the associated C functions provide an easy interface for using the Pololu Wheel Encoders http www pololu com catalog product 1217 which allow a robot to know exactly how far its motors have turned at any point in time Note that this library should work with all standard quadrature encoders not just the Pololu Wheel Encoders This section of the library makes uses of pin change interrupts to quickly detect and record each transition on the encoder Interrupt vectors for PCINTO PCINT1 PCINT2 will be defined if functions from this library are used even if the pins selected are all on a single port so this section of the library will conflict with any other uses of these interrupts The interrupt service routine ISR will take about 20 30 us If you need better control of the interrupts used or you want to write a more efficient ISR you can copy the library code from PololuWheelEncoders cpp into your own project and modify it as necessary Complete documentation of this library s methods can be found in Section 18 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Notes The two sensors A and B on the encoder board go through a four step cycle as each tooth of the wheel passes by for a total of 48 counts per revolution T
13. c basic offset 4 tab width 4 indent tabs mode t end 6 1 Orangutan SVP Functions Overview The Orangutan SVP http www pololu com catalog product 1325 is based on the AVR ATmega324 or ATmega1284 processor It has an auxiliary processor that provides the USB connection five configurable input lines and battery voltage reading Several parts of the Pololu AVR C C Library have built in support for using the auxiliary processor so you will not need to worry about the details of the Serial Peripheral Interface SPI protocol used to talk to the auxiliary processor If you are curious about the details of the SPI protocol you can read the C source code of the library Complete documentation of the SVP specific functions can be found in Section 13 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 An overview of the analog input functions which support reading the analog inputs on the SVP s auxiliary processor can be found in Section 6 c An overview of the serial port communication functions which support sending and receiving bytes from the computer via the auxiliary processor s USB connection can be found in Section 6 j Setting the mode One of the first things to think about when writing a program for the SVP is what mode you want the auxiliary processor to be in If you want to use quadrature encoders you can use the SVP_ MODE ENCODERS mode and let the auxiliary processor h
14. for more information 1 Introduction Page 4 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 2 Prerequisites The free avr gcc compiler avr libc and other associated tools must be installed before the Pololu AVR library Installing the AVR development tools for Windows For Windows users these tools are made available as the WinAVR distribution http winavr sourceforge net We also recommend the AVR Studio development environment http www atmel com avrstudio which may be downloaded free of charge from Atmel To load your compiled code onto the Pololu Orangutan or 3pi we recommend the Pololu USB AVR Programmer http www pololu com catalog product 1300 but any AVR ISP programmer will work If you will be using a Pololu programmer follow the installation instructions http www pololu com docs 0J36 to install it on your computer before continuing with these instructions y Windows Vista WinAVR might not work without additional help from this WinAVR and Windows Vista Guide http www madwizard org electronics articles winavrvista Installing the AVR development tools for Linux Linux users should install all AVR related packages available in their distribution s package manager In particular under Ubuntu you will need to install the following packages e avr libc e gcc avr avra e binutils avr e avrdude for use with the Pololu Orangutan Programmer 2 Prerequisites Page
15. goal Error will range from 1000 to 1000 If we have sensor 0 on the left and sensor 2 on the right a reading of 1000 means that we see the line on the left and a reading of 1000 means we see the line on the right int error position 1000 set the motor speed based on proportional and derivative PID terms KP is the a floating point proportional constant maybe start with a value around 0 1 KD is the floating point derivative constant maybe start with a value around 5 note that when doing PID it s very important you get your signs right or else the control loop will be unstable int motorSpeed KP error KD error lastError lastError error M1 and M2 are base motor speeds That is to say they are the speeds the motors should spin at if you are perfectly on the line with no error If your motors are well matched M1 and M2 will be equal When you start testing your PID loop it might help to start with small values for M1 and M2 You can then increase the speed as you fine tune your PID constants KP and KD int mlSpeed M1 motorSpeed int m2Speed M2 motorSpeed it might help to keep the speeds positive this is optional note that you might want to add a similiar line to keep the speeds from exceeding any maximum allowed value if mlSpeed lt 0 mlSpeed 0 if m2Speed lt 0 6 Example programs Page 41 of 47 Pololu AVR C C Library User s Guide
16. lt fe lt ge lt ad lt fd 05 e gt ee gt ef gt df gt d b gt c b gt c a gt df gt d e gt ee gt ef gt df gt d e gt d gt c gt db gt d gt c b gt ctagaegfe f 06 dc dfdc lt b c 4 void loop run over and over again wait here for one of the three buttons to be pushed unsigned char button wait _for button ALL BUTTONS clear if button TOP_BUTTON play from program space fugue print Fugue led_goto_xy 0 1 print flash gt if button MIDDLE BUTTON play V8 cdefgab gt cbagfedc print C Major led goto xy 0 1 print RAM gt if button BOTTOM BUTTON if is playing stop playing print stopped else play note A 5 200 15 print note A5 int main run once when the program starts print Press a 6 Example programs Page 19 of 47 Pololu AVR C C Library User s Guide lcd goto _xy 0 1 print button while 1 loop return 0 3 buzzer3 Demonstrates the use of this library s playMode and playCheck methods In this example automatic play mode is used to allow the melody to keep playing while it blinks the red user LED Then the mode is switched to play check mode during a phase where we are trying to accurately measure time There are three define macros that allow you to run this example in different ways and observe the result Please see the comments at
17. not work on those pins For example if you have enabled UARTO then you can not control the output value on PD1 using these functions because PD1 serves as the serial transmit line Usage Example This library comes with an example in 1ibpololu avr examples 6 Example programs Page 22 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 1 digital This example program takes a digital reading on PC1 and uses that reading to decide whether to drive pin PD1 the red LED pin low or high You can test that the example is working by connecting a wire from PC1 to ground When the connection is made the red LED should change state tinclude lt pololu orangutan h gt digitall for the Orangutan controllers and 3pi robot This example uses the OrangutanDigital functions to read a digital input and set a digital output It takes a reading on pin PCl and provides feedback about the reading on pin PD1 the red LED pin If you connect a wire between PC1 and ground you should see the red LED change state http www pololu com docs 0J20 http www pololu com http forum pololu com xf int main Make PC1 be an input with its internal pull up resistor enabled It will read high when nothing is connected to it set_digital_input IO_Cl PULL_UP_ENABLED while 1 if is digital input high IO Cl Take digital reading of PCl i set_digital_output IO_D1 HI
18. 0 set_servo_target 2 1800 delay ms 350 set_servo _target 3 1800 delay ms 350 set servo target 4 1800 delay ms 350 set_servo_target 5 1800 delay _ms 350 set_servo_target 6 1800 delay ms 350 set servo target 7 1800 delay ms 350 set_servo_targetB 0 1800 delay ms 350 set servo targetB 1 1800 delay _ms 350 set servo targetB 2 1800 delay ms 350 set_servo targetB 3 1800 delay ms 350 set servo targetB 4 1800 delay ms 350 set _ servo targetB 5 1800 delay ms 350 set_servo targetB 6 1800 delay ms 350 set_servo targetB 7 1800 delay ms 1000 set_servo target 0 1300 delay ms 350 set_servo target 1 1300 delay ms 350 set_servo target 2 1300 delay ms 350 set_servo _target 3 1300 delay ms 350 set_servo target 4 1300 delay ms 350 set_servo _target 5 1300 delay ms 350 set_servo_target 6 1300 delay ms 350 set_servo_target 7 1300 delay ms 350 set_servo targetB 0 1300 delay ms 350 set_servo targetB 1 1300 delay ms 350 set servo targetB 2 1300 delay ms 350 set _ servo targetB 3 1300 delay ms 350 set_servo targetB 4 1300 delay ms 350 set servo targetB 5 1300 delay ms 350 set servo targetB 6 1300 delay ms 350 set_servo targetB 7 1300 delay ms 350 6 Example programs Page 36 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Local Variables mode C
19. 0 being as or more reflective i e whiter than the most reflective surface encountered during calibration and 1000 being as or less reflective i e blacker than the least reflective surface encountered during calibration 3 For line detection applications you can request the line location using the qtr_read_line functions which takes as optional parameters a boolean that indicates whether the line is white on a black background or black on a white background and a boolean that indicates whether the IR emitters should be on or off during the measurement qtr_read_line provides calibrated values for each sensor and returns an integer that tells you where it thinks the line is If you are using N sensors a returned value of 0 means it thinks the line is on or to the outside of sensor 0 and a returned value of 1000 N 1 means it thinks the line is on or to the outside of sensor N 1 As you slide your sensors across the line the line position will change monotonically from 0 to 1000 N 1 or vice versa This line position value can be used for closed loop PID control A sample routine to obtain the sensor values and perform rudimentary line following would be void loop call this routine repeatedly from your main program unsigned int sensors 3 get calibrated sensor values returned in the sensors array along with the line position position will range from 0 to 2000 with 1000 corresponding to the line over the middle sensor
20. 0J21 We do not recommend using these functions directly on the 3pi unless you are adding additional sensors C users See Section 3 of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J19 for examples of this class in the Arduino environment which is almost identical to C Complete documentation of these functions can be found in Section 16 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Notes Calibration This library allows you to use the calibrate method to easily calibrate your sensors for the particular conditions it will encounter Calibrating your sensors can lead to substantially more reliable sensor readings which in turn can help simplify your code As such we recommend you build a calibration phase into your application s initialization routine This can be as simple as a fixed duration over which you repeated call the calibrate method During this calibration phase you will need to expose each of your reflectance sensors to the lightest and darkest readings they will encounter For example if you have made a line follower you will want to slide it across the line during the calibration phase so the each sensor can get a reading of how dark the line is and how light the ground is A sample calibration routine would be tinclude lt pololu orangutan h gt int main 6 Example programs Page 39 of 47 Pololu AVR C C Lib
21. 5 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 3 Downloading and extracting the files To begin the installation process for the Pololu AVR C C Library you will need to download the zip file e Pololu AVR Library version 100607 http www pololu com file download libpololu avr 100607 zip file_id 0J3381 1926k zip released 2010 06 07 Next 1f you are using Windows open the zip file and click Extract all to extract the Pololu AVR Library files to a folder on your desktop If you are using Linux run the command unzip libpololu avr yymmdd zip Where yymmdd is replaced by the version of the library that you have downloaded A directory called libpololu avr will be created Older versions of the library These are available in case you have trouble with the most recent version e libpololu avr 100326 zip http www pololu com file download libpololu avr 100326 zip file_id 0J3334 1923k zip e libpololu avr 100129 zip http www pololu com file download libpololu avr 100129 zip file_id 0J325 1275k zip e libpololu avr 091201 zip http www pololu com file download libpololu avr 091201 zip file_id 03281 1063k zip e libpololu avr 091106 zip http www pololu com file download libpololu avr 091106 zip file_id 0J262 1064k zip e libpololu avr 090605 zip http www pololu com file download libpololu avr 090605 zip file_id 03200 721k zip e libpololu avr 090420 zip http www pololu com file
22. 8 and Baby Orangutan B using these functions will cause the red user LED functions to stop working because the red LED is on the same pin as the UART transmitter PD1 TXD When the AVR is not transmitting bytes on TXD the red LED will be on When the AVR is transmitting bytes on TXD the red LED will flicker On the Orangutan SVP using these functions to control UARTO will cause the red user LED functions to stop working because the red LED is on the same pin as the UARTO transmitter PD1 TXDO When the AVR is not transmitting bytes on TXDO the red LED will be off When the AVR is transmitting bytes on TXDO the red LED will flicker However the AVR on the Orangutan SVP has two UARTs so if you want to use the red LED and you only need one UART then you can use UART instead of UARTO Complete documentation of this library s methods can be found in Section 10 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Examples Example code for making the 3pi Robot into a serial slave controlled by another microcontroller is given in Section 10 a of the Pololu 3pi Robot User s Guide http www pololu com docs 0J21 This library also comes with an example program in libpololu avr examples seriall include lt pololu orangutan h gt seriall for the Orangutan controllers and 3pi robot This example listens for bytes on PDO RXD Whenever it receives a byte it performs a custom action Whenever th
23. B http www pololu com catalog product 1220 It accomplishes this by using the four hardware PWM outputs from timers Timer0 and Timer2 so this library will conflict with any other libraries that rely on or reconfigure Timer0 or Timer2 C users See Section 5 e of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17 for examples of this class in the Arduino environment which is almost identical to C Complete documentation of these functions can be found in Section 7 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Examples This library comes with two examples in libpololu avr examples 1 motors1 Demonstrates controlling the motors using the trimmer potentiometer and uses the red LED for feedback include lt pololu orangutan h gt motorsl for the Orangutan LV 168 Orangutan SV xx8 Orangutan SVP Bl and Baby Orangutan B This example uses the OrangutanMotors functions to drive motors in response to the position of user trimmer potentiometer and blinks the red user LED at a rate determined by the trimmer potentiometer position It uses the OrangutanAnalog library to measure the trimpot position and it uses the OrangutanLEDs library to provide limited feedback with the red user LED http ww pololu com docs 0J20 http ww pololu com http forum pololu com unsigned long prevMillis 0 int main
24. GH PC1 is high so drive PD1 high ig i set_digital_output IO_D1 LOW PC1 is low so drive PD1 low 6 f Orangutan LCD Control Functions Overview This section of the library gives you the ability to control the 8x2 character LCD on the 3pi Robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 and Orangutan LV 168 http www pololu com catalog product 775 It implements the standard 4 bit HD44780 protocol and it uses the busy wait flag feature to avoid the unnecessarily long delays present in other 4 bit LCD control libraries This comprehensive library is meant to offer as much LCD control as possible so it most likely gives you more methods than you need Make sure to use the linker option w1 gc sections when compiling your code so that these extra functions will not be included in your hex file See Section 7 for more information This library is designed to gracefully handle alternate use of the four LCD data lines It will change their data direction registers and output states only when needed for an LCD command after which it will immediately restore the registers to their previous states This allows the LCD data lines to additionally function as pushbutton inputs and an LED driver C users See Section 5 c of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17
25. HF OH svp eight servo for the Orangutan SVP This example uses the OrangutanServos functions to control eight servos To use this example you must connect the correct AVR I O pins to their corresponding servo demultiplexer output selection pins Connect PB3 to SA Connect PB4 to SB Connect PCO to SC http www pololu com docs 0J20 http www pololu com http forum pololu com int main This array specifies the correspondence between I O pins and DEMUX output selection pins This demo uses three pins which allows you to control up to 8 servos You can also use two one or zero pins to control fewer servos const unsigned char demuxPins const unsigned char demuxPins const unsigned char demuxPins const unsigned char demuxPins SS IO B3 IO B4 four servos B3 SA B4 SB IO _B3 two servos B3 SA 0 one servo IO B3 IO B4 IO CO eight servos B3 SA B4 SB C0 B5 servos init demuxPins sizeof demuxPins Set the servo speed to 150 This means that the pulse width will change by at most 15 microseconds every 20 ms So it will take 1 33 seconds to go from a pulse width of 1000 us to 2000 us set_servo _speed 0 150 i set_servo speed 2 150 set_servo _speed 3 150 set_servo _speed 4 150 set _servo speed 5 150 set_servo _speed 6 150 set_servo _speed 7 150 6 Example programs Page 34 of 47 Pololu AVR C C Library
26. Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Pololu AVR C C Library User s Guide print H red_l 9 y a 8 1 Introductions s ae ira a da a AR e e A do h 2 A A A Sy Pood aks a DEAE Gp cab see IA 5 3 Downloading and extracting the files o e 6 4 Compiling the Pololu AVR Library Optional a 7 5 Installation of the Pololu AVR Library aoaaa ee 8 6 Example programs sace por eta Ll a ak By ead a a AA i a aa A a al oe 10 6 a Example program AVR Studio ee 10 6 b Example program Linux serc ee 12 6 c Orangutan Analog Input Functions oaoa 14 6 d Orangutan Buzzer Control Functions 16 6 e Orangutan Digital I O Functions ee 21 6 f Orangutan LCD Control Functions aoaaa ee 23 6 g Orangutan LED Control Functions ooa e 26 6 h Orangutan Motor Control Functions ee 27 6 1 Orangutan Pushbutton Interface Functions ee 29 6 j Orangutan Serial Port Communication Functions oeoo ee 30 6 k Orangutan Servo Control Functions ee 32 6 1 Orangutan SVP Functions 37 6 m Pololu QTR Sensor Functions aoaaa 39 6 n Pololu Wheel Encoder functions ee 42 7 Using the Pololu AVR Library for your own projects o oo 44 8 Additional FESQUICES s e soe aes A a ae a ls Eat ee Ga e E oe RO OE eat foe ety 47 Page 1 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Polol
27. User s Guide O 2001 2010 Pololu Corporation Make all the servos go to a neutral position set_servo_target 0 1300 set servo target 1 1300 set servo target 2 1300 set servo _target 3 1300 set servo target 4 1300 set servo target 5 1300 set servo target 6 1300 set_ servo target 7 1300 while 1 Loop forever When the user presses the top button execute a pre programmed sequence of servo movements if button is pressed TOP BUTTON set_servo_target 0 1800 delay ms 350 set servo target 1 1800 delay ms 350 set servo target 2 1800 delay ms 350 set servo target 3 1800 delay ms 350 set servo target 4 1800 delay ms 350 set servo target 5 1800 delay _ms 350 set servo target 6 1800 delay ms 350 set_ servo target 7 1800 delay ms 1000 set_servo_target 0 1300 delay ms 350 set servo target 1 1300 delay ms 350 set servo target 2 1300 delay _ms 350 set servo target 3 1300 delay _ms 350 set servo target 4 1300 delay _ms 350 set servo target 5 1300 delay ms 350 set servo target 6 1300 delay ms 350 set_ servo target 7 1300 delay ms 350 3 svp sixteen servo This example program demonstrates how to control up to sixteen servos on the Orangutan SVP using the hardware demultiplexer include lt pololu orangutan h gt svp sixteen servo for the Orangutan SVP This ex
28. age 38 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation if dtr_enabled The DTR virtual handshaking line is 1 This often means that a terminal program is conencted to the Pololu Orangutan SVP USB Communication Port lcd goto xy 8 0 print DTR if rts_enabled The RTS virtual handshaking line is 1 lcd_goto_xy 12 0 print RIS Display an analog reading from channel D in millivolts led goto xy 0 1 print Channel D print_long analog_ read millivolts CHANNEL D Wait for 100 ms otherwise the LCD would flicker delay ms 100 6 m Pololu QTR Sensor Functions Overview This set of functions provides access to the QTR family of reflectance sensors which come as single sensor units QTR 1A http www pololu com catalog product 958 _ and QTR 1RC http www pololu com catalog product 959 or as 8 sensor arrays QTR 8A http www pololu com catalog product 960 and QTR 8RC http www pololu com catalog product 961 To initialize the set of sensors that you are using choose either the qtr_analog_init or qtr_re_init function and specify the set of pins connected to the sensors that you will be using The initialization may only be called once within the C environment while C allows the sensors to be used in more complicated ways These functions are used by the 3pi support described in the 3pi Robot User s Guide http www pololu com docs
29. ample uses the OrangutanServos functions to control sixteen servos To use this example you must connect the correct AVR I O pins to their corresponding servo demultiplexer output selection pins x Connect PB3 to SA Connect PB4 to SB Connect PCO to SC Servos a0 a7 will be on the servo demux outputs Servos b0 b7 will be on pins PAO PA7 http www pololu com docs 0J20 http www pololu com http forum pololu com int main This array specifies the correspondence between I O pins and DEMUX output selection pins This demo uses three pins which allows you to control up to 8 servos from the demux const unsigned char demuxPins IO B3 IO B4 IO CO B3 SA B4 SB CO B5 This array specifies the correspondence between I O pins and software PWMed servos const unsigned char servoPinsB IO AO IO Al IO A2 IO A3 IO A4 IO A5 IO A6 IO A7 servos init _extended demuxPins sizeof demuxPins servoPinsB sizeof servoPinsB Set the servo speed to 150 This means that the pulse width 6 Example programs Page 35 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation will change by at most 15 microseconds every 20 ms So it will take 1 33 seconds to go from a pulse width of 1000 us to 2000 us set_servo _speed 0 150 Servo a0 Demux output 0 set servo speed 1 150 Servo al Demux output 1 set servo speed 2
30. ams Page 20 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation define ALWAYS CHECK playMode is always PLAY CHECK delays interrupt the sequence include lt avr pgmspace h gt const char rhapsody PROGMEM 06 T40 L16 d lt b lt f lt d lt f lt bd f T80 c lt b lt f lt c lt f lt b c 8 T180 d b lt f d f gt bd f c b lt f cf gt b c 8 c gt c lt c gt c lt b gt c lt c gt c c gt c lt c gt c lt b gt c lt c gt c c gt c lt c gt c lt b gt c lt cH gt c c gt c lt c gt c lt b gt c lt c gt cH c gt c lt c gt c f gt c lt c gt c c gt c lt c gt c f gt c lt cH gt cH c gt c lt c gt cf gt c lt cH gt cc gt c lt cH gt c ft gt c lt cH gt c d bb bd bf d c b ab c b f d int main TCCR2A 0 configure timer2 to run at 78 kHz TCCR2B 0x06 and overflow when TCNT2 256 3 ms play from_program_space rhapsody while 1 allow the sequence to keep playing automatically through the following delays ifndef ALWAYS CHECK play mode PLAY AUTOMATIC else play mode PLAY CHECK endif lcd goto xy 0 0 print blink intei for i 0 i lt 8 i ifdef ALWAYS_CHECK play check endif red_led 1 delay ms 500 red_led 0 delay ms 500 led goto xy 0 0 print timing lcd_goto_xy 0 1 print ERE clear bottom LCD line turn off automatic playing so that our time critical code won t be interrupted by the
31. an SV xx8 and LV 168 there are two user LEDs are on the top side of the PCB with the red LED on the bottom left and the green LED on the top right On the 3pi there are two user LEDs on the bottom side of the PCB with the red LED on the right when looking at the bottom and the green LED on the left Additional LEDs included with the 3pi may be soldered in on the top side in parallel with the surface mount LEDs on the underside for easier viewing The Orangutan SVP has two user LEDs a red LED on the bottom right and a green LED on the top left The Baby Orangutan has a single red LED and no green LED Note that the red LED is on the same pin as the UARTO serial transmitter PD1 so if you are using UARTO for serial transmission then the red LED commands will not work and you will see the red LED blink briefly whenever data is transmitted on UARTO Note that the green LED is on the same pin as an LCD control pin the green LED will blink briefly whenever data is sent to the LCD but the two functions will otherwise not interfere with each other C users See Section 5 d of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17 for examples of this class in the Arduino environment which is almost identical to CFH Complete documentation of these functions can be found in Section 10 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 This library comes with an example pr
32. andle the readings from two quadrature encoders on lines A B C and D RX If you want to maximize the number of analog inputs available you can use the SVP_ MODE ANALOG mode which makes A B C and D RX be analog inputs The ADC SS line is also available as an analog input It is hardwired to a user trimpot but you can cut the labeled trace between ADC SS and POT on the bottom of the board to disconnect the pot and then connect something else to that pin This allows you to use a total of 13 analog inputs eight on the AVR and five on the auxiliary processor All 13 inputs can be read using the same functions see Section 6 c so you don t need to worry too much about which processor is converting them If you want to receive TTL level serial bytes on your computer you can use the SVP_MODE_RX mode the default which makes A B and C be analog inputs and D RX be the serial receive line In this mode TTL level serial bytes received on the RX line will be sent to the computer on the Pololu Orangutan SVP TTL Serial Port The RX line along with the TX line which is always the serial transmit line make the Orangutan SVP s auxiliary processor function as a USB to TTL serial adapter for your computer allowing you to control serial devices from your computer Alternatively you can control the serial devices directly from the AVR using the functions in Section 6 j and you can use the RX line to monitor and debug the bytes that are being transmitted
33. buzzer s long timerl interrupt Otherwise this interrupt could throw off our timing measurements Instead we will now use playCheck to keep the sequence playing in a way that won t throw off our measurements tifndef ALWAYS AUTOMATIC play mode PLAY CHECK endif 7 unsigned char maxTime 0 for i 0 i lt 8000 i TCNT2 0 while TCNT2 lt 20 time for 250 us if TCNT2 gt maxTime maxTime TCNT2 if the elapsed time is greater than the previous max save it ifndef ALWAYS AUTOMATIC play_check check if it s time to play the next note and play it if so endif lcd_goto_xy 0 1 print max print_long unsigned int maxTime print overwrite any left over characters return 0 6 e Orangutan Digital I O Functions Overview This section of the library provides commands for using the AVR s pins as generic digital inputs and outputs Every pin on the AVR that has a name starting with P followed by a letter and number e g PC2 can be configured as a digital input or 6 Example programs Page 21 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation digital output The program running on the AVR can change the configuration of these pins on the fly using the functions in this library Complete documentation of these functions can be found in Section 4 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Digital outputs
34. commended that you make SS an output at the beginning of your program This can be done with one of the following lines of code set_digital output I0O _B4 LOW Make SSbar an output C only OrangutanDigital setOutput 10_B4 LOW Make SSbar an output C only DDRB 1 lt lt DDB4 Make SSbar an output Usage Examples This library comes with an example program in 1ibpololu avr examples 1 svp1 A simple example that demonstrates some SVP specific functions include lt pololu orangutan h gt svpl for the Orangutan SVP This example uses the OrangutanSVP functions to set the mode of the auxiliary processor take analog readings on line D RX and display information about the Orangutan s current USB device state on the LCD http www pololu com docs 0J20 http www pololu com http forum pololu com int main Make SSbar be an output so it does not interfere with SPI communication set_digital_ output IO_B4 LOW Set the mode to SVP_MODE ANALOG so we can get analog readings on line D RX Svp_set_ mode SVP_MODE ANALOG while 1 clear Erase the LCD if usb configured Connected to USB and the computer recognizes the device print USB else if usb power present Connected to USB print usb if usb_suspend Connected to USB in the Suspend state lcd_goto_xy 4 0 print SUS 6 Example programs P
35. download libpololu avr 090420 zip file_id 03192 719k zip e libpololu avr 090414 src zip http www pololu com file download libpololu avr 090414 zip file_id 0J191 877k zip libpololu avr 081209 src zip http www pololu com file download libpololu avr 081209 sre zip file_id 0J145 295k zip e libpololu avr 081209 zip http www pololu com file download libpololu avr 081209 zip file_id 03144 254k zip e libpololu avr 081104 sre zip http www pololu com file download libpololu avr 081104 sre zip file_id 0J140 292k zip e libpololu avr 081104 zip http www pololu com file download libpololu avr 081104 zip file_id 0J139 251k zip 3 Downloading and extracting the files Page 6 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 4 Compiling the Pololu AVR Library Optional Y This section is optional for people who want to modify the library or get a better understanding of how it works If you just want to install the library proceed to Section 5 Unpack the entire archive and open a command prompt within the libpololu avr directory If avr gec is correctly installed on your system you will be able to type make clean then make to compile the entire library Pay attention to any errors that occur during the build process If all goes well this will generate three versions of the library for the ATmega48 ATmegal68 ATmega328P ATmega324PA ATmega644P and ATmegal284P If you see errors it i
36. duct 775 by either polling for the state of specific buttons or by waiting for press release events on specifiable buttons The first time any function in this section is called the function will initialize all the button I O pins to be inputs and enable their internal pull up resistors The wait_for_button_ methods in this library automatically take care of button debouncing C users See Section 5 f of Programming Orangutans from the Arduino Environment http www pololu com docs 0J17 for examples of this class in the Arduino environment which is almost identical to C Complete documentation of these functions can be found in Section 9 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 This library comes with an example program in 1ibpololu avr examples 1 pushbuttons1 Demonstrates interfacing with the user pushbuttons It will wait for you to push either the top button or the bottom button at which point it will display on the LCD which button was pressed It will also detect when that button is subsequently released and display that to the LCD tinclude lt pololu orangutan h gt OrangutanPushbuttonExample for the Orangutan LV 168 Orangutan SV xx8 Orangutan SVP and 3pi robot This example uses the OrangutanPushbuttons library to detect user input from the pushbuttons and it uses the OrangutanLCD library to display feedback on the LCD http www polol
37. e Orangutan LV 168 is connected to 2 3rds of the battery voltage on the 3pi and to 1 3rd of the battery voltage on the Orangutan SV xx8 It displays on the LCD the trimmer potentiometer output in millivolts and the temperature sensor output in degrees Farenheit If you hold a finger on the underside of the Orangutan LV 168 s PCB near the center of the board you should see the temperature reading slowly start to rise Be careful not to zap the board with electrostatic discharge if you try this include lt pololu orangutan h gt analog2 for the Orangutan LV SV xx8 This example uses the OrangutanAnalog functions to read the voltage output of the trimpot in millivolts and to read the Orangutan LV 168 s temperature sensor in degrees Farenheit These values are printed to the LCD 10 times per second This example is intended for use with the Orangutan LV SV xx8 only You should see the trimpot voltage change as you turn it and you can get the temperature reading to slowly increase by holding a finger on the underside of the Orangutan LV SV xx8 s PCB near the center of the board Be careful not to zap the board with electrostatic discharge if you try this http ww pololu com docs 0J20 http ww pololu com http forum pololu com int main set_analog mode MODE 10 BIT 10 bit analog to digital conversions while 1 run over and over again lcd_goto_xy 0 0 LCD cursor to home posit
38. e if the buzzer is currently inactive include lt pololu orangutan h gt buzzer2 This example uses the OrangutanBuzzer functions to play a series of notes on the Orangutan s 3pi s buzzer It uses the OrangutanPushbuttons library to allow the user select which melody plays This example demonstrates the use of the play method which plays the specified melody entirely in the background requiring no further action from the user once the method is called The CPU is then free to execute other code while the melody plays http www pololu com docs 0J20 http www pololu com http forum pololu com a include lt avr pgmspace h gt this lets us refer to data in program space i e flash store this fugue in program space using the PROGMEM macro Later we will play it directly from program space bypassing the need to load it all into RAM first const char fugue PROGMEM 05 L16 agafaea dactadaea fa lt aa lt bacta dac adaea f 06 dcd lt b d lt ad lt g d lt f d lt gd lt ad lt b d lt dd lt ed lt f d lt g d lt f d lt gd lt ad L8 MS lt b d lt b d MLe lt ge lt g MSc lt ac lt a ML d lt fd lt f 05 MS b gb g ML gt c e gt c e MS afaf ML gc gc MS fdfd ML e lt b e lt b 06 Ll6ragafaea dac adaea fa lt aa lt bac a dac tadaea faeadaca lt b acadg lt b g egdgcg lt b g lt ag lt b gcf lt af dfcf lt b f lt af lt gf lt af lt b e lt ge cite lt b e lt ae lt ge
39. e three define statements that allow you to alter the way this program runs You should have one of the three lines uncommented while commenting out the other two If only WORKING CORRECTLY is uncommented the program should run in its ideal state using automatic play mode during the LED blinking phase and using play check mode during the timing phase The maximum recorded time should be 20 as expected If only ALWAYS AUTOMATIC is uncommented the program will use automatic play mode during both the LED blinking phase and the timing phase Here you will see the effect this has on the time measurements instead of 20 you should see a maximum reading of around 27 or 28 If only ALWAYS CHECK is uncommented the program will be in play check mode during both the LED blinking phase and the timing phase Here you will see the effect that the LED blinking delays have on play check mode the sequence will be very choppy while the LED is blinking but sound normal during the timing phase The maximum timing reading should be 20 as expected http ww pololu com docs 0J20 http www pololu com http forum pololu com O 2001 2010 Pololu Corporation UNCOMMENT ONE OF THE FOLLOWING PRECOMPILER DIRECTIVES the remaining two should be commented out define WORKING CORRECTLY this is the right way to use playMode define ALWAYS AUTOMATIC playMode is always PLAY AUTOMATIC timing is inaccurate 6 Example progr
40. e user presses the middle button it transmits a greeting on PD1 TXD The Baby Orangutan does not have a green LED LCD or pushbuttons so that part of the code will not work To make this example compile for the Orangutan SVP you must add a first argument of UARTO to all the serial function calls http www pololu com docs 0J20 http www pololu com http forum pololu com A Fe FF FF F FF FF HF OF receive buffer A ring buffer that we will use to receive bytes on PDO RXD The OrangutanSerial library will put received bytes in to the buffer starting at the beginning receiveBuffer 0 After the buffer has been filled the library will automatically start over at the beginning char receive buffer 32 receive buffer position This variable will keep track of which bytes in the receive buffer we have already processed It is the offset 0 31 of the next byte in the buffer to process 6 Example programs Page 30 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation unsigned char receive buffer position 0 send buffer A buffer for sending bytes on PD1 TXD char send buffer 32 wait for sending to finish Waits for the bytes in the send buffer to finish transmitting on PD1 TXD We must call this before modifying send buffer or trying to send more bytes because otherwise we could corrupt an existing transmission void wait for sending to f
41. erate this position error value is designed to be monotonic which means the value will almost always change in the same direction as you sweep your sensors across the line This makes it a great quantity to use for PID control Explaining the nature of PID control is beyond the scope of this document but Wikipedia has a very good article http en wikipedia org wiki PID_controller on the subject The following code gives a very simple example of PD control I find the integral PID term is usually not necessary when it comes to line following The specific nature of the constants will be determined by your particular application but you should note that the derivative constant Kd is usually much bigger than the proportional constant Kp This is because the derivative of the error is a much smaller quantity than the error itself so in order to produce a meaningful correction it needs to be multiplied by a much larger constant int lastError 0 void loop call this routine repeatedly from your main program unsigned int sensors 3 get calibrated sensor values returned in the sensors array along with the line position position will range from 0 to 2000 with 1000 corresponding to the line over the middle sensor int position qtr_read_line sensors QTR_EMITTERS_ ON compute our error from the line position We will make it so that the error is zero when the middle sensor is over the line because this is our
42. gures Timer 1 6 Example programs Page 16 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation y This library is incompatible with some older releases of WinAVR If you experience any problems when using this library make sure that your copy of the compiler is up to date We know that it works with WinAVR 20080610 The benefit to this approach is that you can play notes on the buzzer while leaving the CPU mostly free to execute the rest of your code This means you can have a melody playing in the background while your Orangutan does its main task You can poll the isPlaying method to determine when the buzzer is finished playing C users See Section 5 b of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17 for examples of this class in the Arduino environment which is almost identical to C Complete documentation of the functions can be found in Section 3 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 Usage Examples This library comes with three examples in 1ibpololu avr examples 1 buzzerl Demonstrates one way to use this library s play_note method to play a simple melody stored in RAM It should immediately start playing the melody and you can use the top user pushbutton to stop and replay the melody The example is structured so that you can add your own code to the main loop and the melody will still play normal
43. high power motor drivers capable of delivering hundreds of watts Pololu Orangutan SV 328 http www pololu com catalog product 1227 a full featured mega328 based robot controller that includes an LCD display The SV 328 runs on an input voltage of 6 13 5V giving you a wide range of robot power supply options and can supply up to 3 A on its regulated 5 V bus This library also supports the original Orangutan SV 168 http www pololu com catalog product 1225 which was replaced by the SV 328 SD Pololu Orangutan LV 168 http www pololu com catalog product 775 a full featured megal68 based robot controller that includes an LCD display The LV 168 runs on an input voltage of 2 5V allowing two or three f batteries to power a robot Pololu Baby Orangutan B 48 http www pololu com catalog product 1215 a compact complete robot controller based on the mega48 The B 48 packs a voltage regulator processor and a two channel motor driver into a 24 pin DIP format Pololu Baby Orangutan B 328 http www pololu com catalog product 1220 a mega328 version of the above The mega328 offers more memory for your programs 32 KB flash 2 KB RAM This library also supports the Baby Orangutan B 168 http www pololu com catalog product 1216 which was replaced by the Baby B 328 Pololu QTR 1A http www pololu com catalog product 958 and QTR 8A http www pololu com catalog product 960 reflectance sensors analog an analog sensor con
44. his corresponds to about 3 mm for each count though you will have to calibrate values for your own robot to get a precise measure of distance Normally there will be at least 1 ms between counts which gives the ISR plenty of time to finish one count before the next one occurs This is very important because if two counts occur quickly enough the ISR will not be able to determine the direction of rotation In this case an error can be detected by the functions encoders_check_error_m1 or encoders_check_error_m2 An error like this either corresponds to a miscalibration of the encoder or a timing issue with the software For example if interrupts are disabled for several ms while the wheels are spinning quickly errors will probably occur Usage Examples This library comes with one example program in libpololu avriexamples The example measures the outputs of two encoders one connected to ports PC2 and PC3 and another connected to ports PC4 and PC5 these are the leftmost four ports on the Orangutan The values of the two encoder outputs are displayed on the LCD and errors if any are reported below For use on the Baby Orangutan remove the LCD display code and come up with some other way to use the values 1 wheel_encoders1 include lt pololu orangutan h gt include lt avr interrupt h gt int main encoders init 16 17 18 19 the Arduino numbers for ports PC2 PC5 while 1 led_goto_xy 0 0 print_long encode
45. ice atmegal68 Program 3190 bytes 19 53 Full text data bootloader Data 40 bytes 3 93 Full data bss noinit Build succeeded with 0 Warnings 4 5 E Build O Message 3 Find in Files mr Breakpoints and Tracepoints AVR Studio build window compiling the example project Connect your programmer to your computer and your Orangutan board or 3pi robot and turn on the target s power If you are using a Pololu programer its LEDs will give you feedback as to whether it has a good connection to the target see the programmer s user s guide for more information Select Tools gt Program AVR gt Connect to connect to the programmer If you are using a Pololu programmer select AVRISP and Auto When you click the Connect button the AVRISP programming window should appear You will use AVRISP to load test hex into the flash memory of your AVR To do this click in the Flash section and select file test hex that was compiled earlier Note that you have to first navigate to your project directory Now click Program in the Flash section and the test code should be loaded onto your Orangutan or 3pi 6 Example programs Page 11 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Main Program Fuses LockBits Advanced HW Settings HW Info Auto V Erase device before flash programming Y Verify device after progra
46. in your own programs e g configuring AVR Studio projects to use the library please see Section 7 Manual installation if necessary Determine the location of your avr gcc files In Windows they will usually be in a folder such as c winAVR 20080610 avr In Linux the avr gcc files are probably located in usr avr If you currently have an older version of the Pololu AVR Library your first step should be to delete all of the old include files and the 1ibpololu a file or 1ibpololu_atmegax a files that you installed previously Next copy libpololu_atmega48 a libpololu_atmegal68 a libpololu_atmega328p a libpololu_atmega324p a libpololu_atmega644p a and libpololu_atmegal284p a into the lib subdirectory of your avr directory e g C WinAVR 20080610 avr 1ib Note that there is also a 1ib subdirectory directly below the main WinAVR directory it will not work to put the 1ibpololu_atmegaX a files here These six library files represent separate compilations for the six different AVR microcontrollers found on our Orangutans and 3pi robot When you make your own projects you will need to use the appropriate library for your particular microcontroller Finally copy the entire pololu subfolder into the include subfolder The Pololu include files should now be located in avr include pololu 5 Installation of the Pololu AVR Library Page 8 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Con pololu v
47. ing themselves if they are commanded to move to a position outside their range of motion To start off you can send pulse widths of 1 5 ms and then slowly change the pulse width until you discover its upper and lower limits Orangutan Servo Control The OrangutanServos section of the library allows you to generate the control pulses for up to 16 servos 6 Example programs Page 32 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation On every Orangutan except the Orangutan SVP each servo requires one free I O pin The library allows you to choose which I O pins to use for your servos On the Baby Orangutan B there are enough free I O lines for you to control the full 16 servos On the Orangutan SV and Orangutan LV 168 there are 8 free 1 O lines so you can easily control eight servos but you can control more servos if you remove the LCD or other unused hardware The pulses are generated using software PWM On the Orangutan SVP the pulses for your first 8 servos are all generated on pin PDS This pin is a hardware PWM output OCIA so the OrangutanServos library generates the servo pulses using hardware PWM which is more accurate and takes less CPU time than software PWM Pin PDS is connected to the input line of an on board 8 output demultiplexer If you just need to control one servo you can leave the demultiplexer input lines disconnected and plug your servo in to servo port 0 If you want to control more than one
48. inish while serial send buffer empty process received byte Responds to a byte that has been received on PDO RXD If you are writing your own serial program you can replace all the code in this function with your own custom behaviors void process received byte char byte switch byte If the character G is received turn on the green LED case G green _led 1 break 1f the character g is received turn off the green LED Case g green _led 0 break 1f the character c is received play the note c case c play from program space PSTR c16 break 1f the character d is received play the note d case d play from program _space PSTR dl6 break If any other character is received change its capitalization and send it back default wait for sending to finish send buffer 0 byte 0x20 serial send send buffer 1 break void check for new bytes received while serial get_received bytes receive buffer position int main Process the new byte that has just been received process received byte receive buffer receive buffer position Increment receive buffer position but wrap around when it gets to the end of the buffer if receive buffer position sizeof receive buffer 1 receive buffer position 0 receive buffer position Set the baud ra
49. ion upper left print_long to_millivolts read_trimpot trimpot output in mV print mv added spaces are to overwrite left over chars lcd_goto_xy 0 1 LCD cursor to start of the second line unsigned int temp read_temperature f get temp in tenths of a degree F 5 get the whole number of degrees DO print the decimal point y temp 10 10 print the tenths digit print character 223 print a degree symbol print E jj added spaces are to overwrite left over chars print_long temp 10 print character print long temp delay _ms 100 wait for 100 ms otherwise LCD flickers too much 6 d Orangutan Buzzer Control Functions Overview These functions allow you to easily control the buzzer on the 3pi robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 and Orangutan LV 168 http www pololu com catalog product 775 You have the option of playing either a note or a frequency for a specified duration at a specified volume or you can use the play method to play an entire melody in the background Buzzer control is achieved using one of the Timer 1 PWM outputs and duration timing is performed using a Timer 1 overflow interrupt 9 Note The OrangutanServos and OrangutanBuzzer libraries both use Timer 1 so they will conflict with each other and any other code that relies on or reconfi
50. ly in the background assuming your code executes quickly enough to avoid inserting delays between the notes You can use this same technique to play melodies that have been stored in EEPROM the megal68 has enough room in EEPROM to store 170 notes the mega328 has enough room in EEPROM to store 340 notes include lt pololu orangutan h gt buzzerl This example uses the OrangutanBuzzer library to play a series of notes on the Orangutan s 3pi s buzzer It also uses the OrangutanLCD library to display the notes its playing and it uses the OrangutanPushbuttons library to allow the user to stop reset the melody with the top pushbutton http ww pololu com docs 0J20 http www pololu com http forum pololu com define MELODY LENGTH 95 These arrays take up a total of 285 bytes of RAM out of a 1k limit unsigned char note MELODY LENGTH E 5 SILENT NOTE E 5 SILENT NOTE E 5 SILENT NOTE C 5 E 5 G 5 SILENT NOTE G 4 SILENT NOTE C 5 G 4 SILENT NOTE E 4 A 4 B 4 B FLAT 4 A 4 G 4 E 5 G 5 A 5 F 5 G 5 SILENT NOTE E 5 C 5 D 5 B 4 C 5 G 4 SILENT NOTE E 4 A 4 B 4 B FLAT 4 A 4 G 4 E 5 G 5 A 5 F 5 G 5 SILENT NOTE E 5 C 5 D 5 B 4 SILENT NOTE G 5 F_SHARP 5 F 5 D_SHARP 5 E 5 SILENT NOTE G SHARP 4 A 4 C 5 SILENT NOTE A 4 C 5 D 5 SILENT NOTE G 5 F_SHARP 5 F 5 D_SHARP 5 E 5 SILENT
51. mming Flash Use Current Simulator Emulator FLASH Memory m EEPROM Use Current Simulator Emulator EEPROM Memory Input HEX File Fc etc tke tell Progam ety Ret ipat ELF Re f Fuses and lockbits settings Program Save must be specified before saving to ELF Programming the Orangutan from AVR Studio If your controller was successfully programmed and you not using a Baby Orangutan you should hear a short tune see the message Hello on the LCD if one is present and the contrast is set correctly and the LEDs on the board should blink If you are using a Baby Orangutan B you will just see the red user LED blink In case you are having trouble performing the compilation precompiled hex files for this example and all of the other examples included with the C C library are available in examples processor hex_files You can load these hex files onto your controller using AVR Studio as described above 6 b Example program Linux A simple demo program is supplied in the directory examples atmegaXXX simple test where atmegaXXX is the processor on your board Change to this directory and inspect the Makefile Depending on your system you may need to update the paths to the avr gcc binaries and the device for your programmer Then you should be able to compile the example with make which should generate the output like this as it compiles the source code 6 Example program
52. n because the reading will be unpredictable An input pin can be used to read data from a sensor or other peripheral When the AVR powers up all I O pins are configured as inputs with their pull up resistors disabled Caveats To use your digital I O pins correctly and safely there are several things you should be aware of e Maximum voltage ratings Be sure to not expose your input pins to voltages outside their allowed range which is 0 5 V 5 5 V assuming a VCC of 5 V For example do not connect any AVR pins directly to an RS 232 output which varies between 12 V and 12 V You can use a voltage divider circuit to overcome this limitation Drawing too much current from an output pin Be sure you do not attempt to draw too much current from your output pin it may break Basically each output pin can supply up to 20 mA of current see the DC characteristics section of your AVR s datasheet for details This is enough to power typical LEDs but is too small for many other devices You can use a transistor to overcome this limitation Shorts Be sure that you do not connect a high output pin to a low output pin This connection is called a short because it results in a low resistance path from VCC to ground which will conduct large amounts of current until something breaks e Alternative functions Many of the pins on the AVR have alternative functions If these alternate functions are enabled then the functions in this library may
53. n user LEDs http www pololu com docs 0J20 http ww pololu com http forum pololu com iad int main run over and over again while 1 note that the following line could also be accomplished with int pot analogRead 7 int pot read_trimpot determine the trimpot position avoid clearing the LCD to reduce flicker led_goto_xy 0 0 print pot print _long pot print the trim pot position 0 1023 print overwrite any left over digits int motorSpeed 512 pot 2 lcd goto xy 0 1 print spd print_long motorSpeed print the resulting motor speed 255 255 print set_motors motorSpeed motorSpeed set speeds of motors 1 and 2 all LEDs off red_led 0 green led 0 turn green LED on when motors are spinning forward if motorSpeed gt 0 green _led 1 6 Example programs Page 28 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation turn red LED on when motors are spinning in reverse if motorSpeed lt 0 red_led 1 delay ms 100 6 i Orangutan Pushbutton Interface Functions Overview This library allows you to easily interface with the three user pushbuttons on the 3pi robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 and Orangutan LV 168 http www pololu com catalog pro
54. ogram in 1ibpololu avr examples 1 led1 A simple example that blinks LEDs tinclude lt pololu orangutan h gt ledl for the 3pi robot Orangutan LV 168 Orangutan SV xx8 Orangutan SVP or Baby Orangutan B LEDs on the 3pi robot or Orangutan It will also work to control the red This program uses the OrangutanLEDs functions to control the red and green LED on the Baby Orangutan B which lacks a second green LED 6 Example programs Page 26 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation http www pololu com docs 0J20 http www pololu com http forum pololu com El int main while 1 red_led 1 red LED on delay ms 1000 waits for a second red_led 0 red LED off waits for a second green LED on will not work on the Baby Orangutan waits for 0 5 seconds green LED off will not work on the Baby Orangutan waits for 0 5 seconds delay ms 1000 green _led 1 delay ms 500 green _led 0 delay ms 500 ASE ASEAN EASES ASS ANNAN EAN ASS return 0 6 h Orangutan Motor Control Functions Overview This set of functions gives you the ability to control the motor drivers on the 3pi Robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 Orangutan LV 168 http www pololu com catalog product 775 and Baby Orangutan
55. om the Pololu AVR Library B File Project Build Edit View Tools Debug Window Help DSRS OLNSSIC RS BAAR EN finclude OS h gt test lt t default f 29 Source Files int main print Hello 23 External Dependenci play L16 ceg gt c ernal Dependencies H E Other Files EEN le 1 red_led 0 green_led 1 delay_nms 100 red_led 1 green_led 0 delay_ns 100 return 0 B C Users paul Desktop lib orangutan test avr studio project i 4 gt E Build O Message l Find in Files r Breakpoints and Tracepoints AVR Studio showing the sample program 6 Example programs Page 10 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation To compile this program select Build gt Build or press F7 Look for warnings and errors indicated by yellow and red dots in the output displayed below If the program compiles successfully the message Build succeeded with 0 Warnings will appear at the end of the output and a file test hex will have been created in the simple test default folder Build started 15 7 2008 at 10 10 02 avr acc exe mmcu atmegal s Wall gdwarf 2 std gnu99 0s funsigne avr gcc exe mmcu atmegal68 Wl gc sections Wl Map test map test o avr objcopy 0 ihex R eeprom test elf test hex avr objcopy j eeprom set section flags eeprom alloc load cha avr objdump h S test elf gt test lss AVR Memory Usage Dev
56. or received by the AVR You can use the setMode command at the beginning of your program to set the mode of the auxiliary processor See the Pololu AVR Library Command Reference for details Powered by SPI Whenever you call a function in the Pololu AVR Library that uses the auxiliary processor the function might initiate SPI communication to the auxiliary processor This means that the MOSI PBS and SCK PB7 pins will be set to outputs the MISO PB6 pin will be set as an input a pull up resistor will be enabled on SS PB4 if it is an input and the AVR s 6 Example programs Page 37 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation hardware SPI module will be enabled The functions that do this include any analog input function that uses an input on the auxiliary processor any function for reading the battery voltage or trimpot any serial port function that uses the USB_COMM port and any function specific to the Orangutan SVP Section 13 of the Command Reference PB4 should be an output In order for the functions that talk to the auxiliary processor to work the SS PB4 pin must either be an output or be an input that is always high The AVR s SPI module is designed so that if SS is an input and it reads low 0 V then the SPI module will automatically go in to slave mode the MSTR bit in SPCR will become zero making it impossible to communicate with the auxiliary processor Therefore it is re
57. ort tune see the message Hello on the LCD if one is present and the contrast is set correctly and the LEDs on the board should blink If you are using a Baby Orangutan you will just see the red user LED blink 6 c Orangutan Analog Input Functions Overview This section of the library provides a set of methods that can be used to read analog voltage inputs as well as functions specifically designed to read the value of the trimmer potentiometer on the 3pi Robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan LV 168 http www pololu com catalog product 775 Orangutan SVP http www pololu com catalog product 1325 and Baby Orangutan B http www pololu com catalog product 1220 the value of the temperature sensor in tenths of a degree F or C on the Orangutan LV 168 only and the battery voltage 3p1 robot SV xx8 or SVP C users See Section 5 a of Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17 for examples of this class in the Arduino environment which is almost identical to C Complete documentation of the functions can be found in Section 2 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 6 Example programs Page 14 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Usage Examples This library comes with two examples in 1ibpololu avr e
58. rary User s Guide 2001 2010 Pololu Corporation initialize your QTR sensors unsigned char gtr_rc pins 10 CO IO Cl IO C2 qtr_rc init qtr_rc pins 3 2000 255 800 us timeout no emitter pin int qtr_analog pins 0 1 2 qtr_analog_init qtr_analog pins 3 10 IO C0 10 samples emitter pin is PCO optional wait for some input from the user such as a button press then start calibration phase and move the sensors over both reflectance extremes they will encounter in your application int by for i 0 i lt 250 i make the calibration take about 5 seconds qtr_calibrate QTR_ EMITTERS ON delay 20 optional signal that the calibration phase is now over and wait for further input from the user such as a button press while 1 main body of program goes here return 0 Reading the Sensors This library gives you a number of different ways to read the sensors 1 You can request raw sensor values using the read method which takes an optional argument that lets you perform the read with the IR emitters turned off note that turning the emitters off is only supported by the QTR 8x reflectance sensor arrays 2 You can request calibrated sensor values using the qtr_read_calibrated function which also takes an optional argument that lets you perform the read with the IR emitters turned off Calibrated sensor values will always range from 0 to 1000 with
59. rs get counts ml print lcd_goto_xy 4 0 print _long encoders get counts m2 print qa w if encoders check error ml lcd_goto_xy 0 1 6 Example programs Page 42 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation print Error 1 if encoders check error m2 led_goto_xy 0 1 print Error 2 delay _ms 50 6 Example programs Page 43 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 7 Using the Pololu AVR Library for your own projects After getting one of the simple examples to work with an Orangutan controller or 3pi robot you can start working on more complicated programs of your own The library provides easy access to all of the features of the Orangutans and 3pi including the LCD screen buttons LEDs motors and buzzer There are also functions in the library that make it easy for you to do more general purpose operations with the AVR such as timing and analog to digital conversion The library also provides support for the Pololu QTR sensors http www pololu com catalog product 961 and the Pololu Wheel Encoders http www pololu com catalog product 1217 it should actually work for quadrature encoders in general For a complete list of functions provided by the library see the command reference http www pololu com docs 0J18 Usually the easiest way to adapt this code to your own projects will be to start wi
60. s Page 12 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation llendo its Terminal make 9 Wall mcal l prol es mmcu at mega168 Wall mcall pr lpololu 0 R eeprom i est obj test hex 5 mmcu atmegal68 Compiling the test program under Linux If make completed successfully connect your programmer to your computer and your Orangutan board or 3pi robot and turn on the target s power The green status LED close to the USB connector should be on while the other two LEDs should be off indicating that the programmer is ready Type make program to load the program onto the Orangutan or 3pi If your programmer is installed on a port other than dev ttyusBo for example if you are using the newer Pololu USB AVR Programmer you will have to edit the Makefile and enter the correct port 6 Example programs Page 13 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 1indawrdude P mi avrdude AVR device initialized and ready to accept instructions Reading HHHH HHHH HHHH HHHH Ea a 10 gt will be per avrdudet rdude ude udet writing Writing RRRA RA A E HHRHH awrdude ingl Seabee HHHH HHHH HHHH I a EEEE avrdude avrdude avrdude safemode Fu avrdude done Thank you Programming the Orangutan with avrdude under Linux If your controller was successfully programmed and you are not using a Baby Orangutan you should hear a sh
61. s bin make exe s Recommended AVR Studio linker options for projects using the Pololu AVR Library 7 Using the Pololu AVR Library for your own projects Page 46 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 8 Additional resources To learn more about programming AVRs and using the Pololu AVR Library see the following list of resources Pololu AVR Library Command Reference http www pololu com does 0J18 detailed information about every function in the library Programming Orangutans and the 3pi Robot from the Arduino Environment http www pololu com docs 0J17 information about using this library to program Orangutans and the 3pi robot from within the Arduino environment Arduino Library for the Pololu QTR Reflectance Sensors http www pololu com docs 0J19 information about using the QTR sensor portion of this library from within the Arduino environment Pololu 3pi Robot User s Guide http www pololu com docs 0J21 Pololu Orangutan SV and LV 168 User s Guide http www pololu com docs 0J27 Pololu Orangutan SVP User s Guide http www pololu com docs 0339 Pololu Baby Orangutan B User s Guide http www pololu com docs 0J14 Pololu USB AVR Programmer User s Guide http www pololu com docs 0J36 Pololu Orangutan USB Programmer User s Guide http www pololu com docs 0J6 WinA VR http winavr sourceforge net AVR Studio http www atmel com avrstudio
62. s likely that avr gcc was installed improperly or in a way that is incompatible with the Makefile Command Prompt 101 x s paul gt cd Desktop libpololu avr pau LSD r gt make LEN L MMCU DrangutanMot 0 tanMotox ILOLU ff Jrangutan wall cal l pr MME 0rangutanAna atanfna aut ant ir Oy Libpololu SLibpololu avr Compiling the Pololu AVR Library from the command prompt in Windows 4 Compiling the Pololu AVR Library Optional Page 7 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 5 Installation of the Pololu AVR Library Automatic installation The automatic installation installs all Pololu AVR Library files in the location of your avr gcc installation This is done by running install bat or by opening a command prompt and typing make install which will put the library s a files and header files in the appropriate WinAVR directories y Windows Vista right click on insta11 bat and select Run as administrator If the automatic installation works you can proceed to Section 6 to try out some example programs You can also find ready to run example AVR Studio projects in the examples directory of the library download 1ibpololu avr examples There are examples preconfigured for all six microcontrollers supported by this library so you should use the appropriate ones for your particular microcontroller For information on using the Pololu AVR library
63. servo then you must choose which free I O lines to connect to the demultiplexer s three output selection lines If you use one I O line you can control two servos If you use two I O lines you can control up to four servos If you use three I O lines then you can control up to eight servos If you need to control more than 8 servos then you can use software PWM to control up to eight more servos for a total of 16 Usage Examples This library comes with several examples in 1ibpololu avr examples 1 svp one servo This example program demonstrates how to control one servo on the Orangutan SVP using PDS include lt pololu orangutan h gt svp one servo for the Orangutan SVP This example uses the OrangutanServos functions to control one servo The servo pulse signal is sent on pin PD5 which is hardwired to the input of the demux The servo signal is available on demux output 0 This example uses the OrangutanPushbuttons functions to take input from the user and the OrangutanLCD functions to display feedback on the LCD http www pololu com docs 0J20 http www pololu com http forum pololu com int main const unsigned char demuxPins servos init demuxPins sizeof demuxPins set_servo_target 0 1300 Make the servo go to a neutral position clear Clear the LCD while 1 Loop forever When the user presses the top button execute a pre programmed
64. t _long note currentldx print integer value of the current note print my overwrite any left over characters currentldx Insert some other useful code here the melody will play normally while the rest of your code executes as long as it executes quickly enough to keep from inserting delays between the notes For example let the top user pushbutton function as a stop reset melody button if button is pressed TOP_BUTTON stop playing silence the buzzer if currentIdx lt MELODY LENGTH currentIdx MELODY LENGTH terminate the melody else currentldx 0 restart the melody wait_for button _release TOP_BUTTON wait here for the button to be released return 0 2 buzzer2 Demonstrates how you can use this library s play function to start a melody playing Once started the melody will play all the way to the end with no further action required from your code and the rest of your program will execute as normal while the melody plays in the background The play function is driven entirely by the Timer overflow interrupt The top user pushbutton will play a fugue by Bach from program memory the middle user pushbutton will quietly play the C major scale 6 Example programs Page 18 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation up and back down from RAM and the bottom user pushbutton will stop any melody that is currently playing or play a single not
65. taining IR phototransistor pairs that allows a robot to detect the difference between shades of color The QTR sensors can be used for following lines on the floor for obstacle or drop off stairway detection and for various other applications Pololu QTR 1RC http www pololu com catalog product 959 and QTR 8RC http www pololu com catalog product 961 reflectance sensors RC a version of the above that is read using digital inputs this is compatible with the Parallax QTI sensors 1 Introduction Page 3 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Encoder for Pololu Wheel 42x19 mm http www pololu com catalog product 1217 a wheel encoder solution that allows a robot to measure how far it has traveled For detailed information about all of the functions available in the library see the command reference http www pololu com docs 0J18 Note that the library is designed for Atmel s AVR based boards like the Orangutans to use it with the QTR sensors your controller must be either an Orangutan or another board built with an ATmega48 168 328P or ATmega324PA 644P 1284P AVR microcontroller This document covers the C C version of the library but it may also be used with Arduino http www arduino ce a popular beginner friendly programming environment for the mega168 328 using simplified C code See our guide to using Arduino with Orangutan controllers http www pololu com docs 0J17
66. te to 9600 bits per second Each byte takes ten bit times so you can get at most 960 bytes per second at this speed serial set _baud _rate 9600 6 Example programs Page 31 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Start receiving bytes in the ring buffer serial receive ring receive buffer sizeof receive buffer while 1 Deal with any new bytes received check for new bytes received If the user presses the middle button send Hi there and wait until the user releases the button if button is pressed MIDDLE BUTTON wait for sending to finish memcpy P send buffer PSTR Hi there r n 11 serial send send buffer 11 Wait for the user to release the button While the processor is waiting the OrangutanSerial library will take care of receiving bytes using the serial reception interrupt But if enough bytes arrive during this period to fill up the receive buffer then the older bytes will be lost and we won t know exactly how many bytes have been received wait_for button release MIDDLE BUTTON 6 k Orangutan Servo Control Functions Overview This section of the library provides commands for generating digital pulses to control servos Complete documentation of these functions can be found in Section 11 of the Pololu AVR Library Command Reference http www pololu com docs 0J18 9 Note The OrangutanServos and OrangutanBu
67. th a working example and gradually add the things that you need one step at a time However if you want to start from scratch there are just a few things that you need to know First to use the library with C you must place one of the following lines C include lt pololu orangutan h gt include lt pololu 3pi h gt include lt pololu qtr h gt include lt pololu encoders h gt at the top of any C file that uses functions provided by the library To use the library with C the equivalent lines are C include lt pololu orangutan gt include lt pololu Pololu3pi h gt include lt pololu PololuQTRSensors h gt include lt pololu PololuWheelEncoders h gt The line or lines that you include depend on which product you are using with the library Second when compiling you must link your object files with the appropriate libpololu_atmegaX a file This is accomplished by passing the 1pololu_atmegax option to avr gcc during the linking step where x can be 48 168 328p 324p 644p Or 1284p To add the 1pololu_atmegax option within AVR studio select Project gt Configuration Options gt Libraries You should see libpololu_atmega48 a libpololu_atmegal68 a libpololu_atmega328p a libpololu_atmega324p a libpololu_atmega644p a and libpololu_atmegal284p a listed as options in the left column Select the file that matches the microcontroller you are programming and click add library to add 1t to your project Note that
68. the 3pi robots with serial numbers less than 035840 use the ATmegal68 microcontroller 3pi robots with serial number 0J5840 or greater use the ATmega328 microcontroller 7 Using the Pololu AVR Library for your own projects Page 44 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation iS Library Search Path alee Ww General Include Directories Available Link Objects Link with These Objects Add Library gt libpololu_atmega328p a Remove Object libpololu_atmega48 a libprintf_flt a libprintf_min a libscanf_flt a Add Object libscanf_min a Move up Move down AVR Studio library settings for using the Pololu AVR library libpololu Finally we also strongly recommend the linker option w1 gc sections This causes unused library functions to not be included resulting in a much smaller code size To include this in AVR Studio select Project gt Configuration Options gt Custom Options Click on Linker options and add W1 gc sections to the list This linker option is included in both the AVR Studio and Linux based example programs described earlier 7 Using the Pololu AVR Library for your own projects Page 45 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation a A i Al gc sections Include Directories Libraries 7 External Tools IV Use Wind R awr gcc CAWinAVR bin avr gce exe E make C AWindVR uti
69. the top of the sketch for more detailed information i FF F FF AA F FF FF FF F FF F FF FF OF OF FF OF FOF FOF OF FF OF F FF FF OF FOF OF OF nclude lt pololu orangutan h gt buzzer3 This example uses the OrangutanBuzzer functions to play a series of notes on the Orangutan s 3pi s buzzer It uses the OrangutanPushbuttons functions to allow the user select which melody plays This example demonstrates the use of the play mode and play check methods which allow you to select whether the melody sequence initiated by play is played automatically in the background by the Timerl interrupt or if the play is driven by the play check method in your main loop Automatic play mode should be used if your code has a lot of delays and is not time critical In this example automatic mode is used to allow the melody to keep playing while we blink the red user LED Play check mode should be used during parts of your code that are time critical In automatic mode the Timerl interrupt is very slow when it loads the next note and this can delay the execution of your Using play check mode allows you to control when the next note is loaded so that it doesn t occur in the middle of some time sensitive measurement In our example we use play check mode to keep the melody going while performing timing measurements using Timer2 After the measurements the maximum time measured is displayed on the LCD Immediately below ar
70. u Corporation 1 Introduction This document is a guide to using the Pololu AVR C C library including installation instructions tutorials and example programs The Pololu AVR Library makes it easy for you to get started with the following Pololu products 1 Introduction Page 2 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation Pololu 3pi robot http www pololu com catalog product 975 a megal68 328 based programmable robot The 3pi robot essentially contains an SV 328 and a 5 sensor version of the QTR 8RC both of which are in the list below ololu Orangutan SVP 1284 based on the megal284 the SVP 1284 is our newest Orangutan robot ontroller It is a super sized version of the SV 328 with a built in AVR ISP programmer more I O lines ore regulated power and more memory It also features hardware that makes it easy to control up to eight servos with a single hardware PWM and almost no processor overhead ololu Orangutan SVP 324 based on the mega324 the SVP 324 is a version of the SVP 1284 with less emory The two versions are completely code compatible the same code will run on both devices as long as it s small enough to fit on the ATmega324PA Pololu Orangutan X2 based on the mega644 the X2 robot controller is the most powerful Orangutan It features an auxiliary microcontroller devoted to controlling much of the integrated hardware it also acts as a built in AVR ISP programmer and
71. u com docs 0J20 http www pololu com http forum pololu com i int main while 1 clear print Waiting wait for either the top or bottom buttons to be pressed store the value of the pressed button in the variable button unsigned char button wait _for button press TOP BUTTON BOTTOM BUTTON clear if button TOP BUTTON display the button that was pressed print top down else print bot down wait_for button _release button wait for that button to be released clear print released display that the button was released delay_ms 1000 6 Example programs Page 29 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 6 j Orangutan Serial Port Communication Functions Overview This section of the library provides routines for accessing the serial port USART on the 3pi robot http www pololu com catalog product 975 Orangutan SV http www pololu com catalog product 1227 Orangutan SVP http www pololu com catalog product 1325 Orangutan LV 168 http www pololu com catalog product 775 and Baby Orangutan B http www pololu com catalog product 1220 The serial port routines normally use the USART_UDRE vect USART_RX vect USARTO_UDRE vect USARTO_RX_vect USART1_UDRE_vect and USART1_RX_vect interrupts so they will conflict with any code that also uses these interrupts On the 3pi robot Orangutan SV Orangutan LV 16
72. utes The LED is flashed so that its brightness appears proportional to the trimpot position This example will work on both the Orangutan LV SV xx8 and Baby Orangutan B http www pololu com docs 0J20 http www pololu com http forum pololu com unsigned int sum unsigned int avg unsigned char samples int main set analog mode MODE 8 BIT 8 bit analog to digital conversions sum 0 samples 0 avg 0 start_analog conversion TRIMPOT start initial conversion while 1 if analog_is_converting if conversion is done sum analog conversion result start_analog conversion TRIMPOT get result start next conversion if 20 samples have been taken if samples 20 i avg sum 20 compute 20 sample average of ADC result samples 0 sum 0 when avg 0 the red LED is almost totally off when avg 255 the red LED is almost totally on brightness should scale approximately linearly in between red_led 0 red LED off delay us 256 avg red led 1 red LED on delay us avg 1 6 Example programs Page 15 of 47 Pololu AVR C C Library User s Guide O 2001 2010 Pololu Corporation 2 analog2 Intended for use on the Orangutan LV 168 Note that it will run on the 3pi robot and Orangutan SV xx8 but the displayed temperature will be incorrect as the analog input connected to the temperature sensor on th
73. xamples The Orangutan Motors examples also make limited use of this section 1 analogl Demonstrates how you can use the methods in this library to read the analog voltage of the trimmer potentiometer in the background while the rest of your code executes Ifthe ADC is free the program starts a conversion on the TRIMPOT analog input channel 7 on all devices except the SVP and then it proceeds to execute the rest of the code in loop while the ADC hardware works Polling of the analog is _converting method allows the program to determine when the conversion is complete and to update its notion of the trimpot value accordingly Feedback is given via the red user LED whose brightness is made to scale with the trimpot position On the Orangutan SVP this example code will work but it is not the recommended way of reading the trimpot The trimpot reading and averaging is done on the auxiliary processor so a simple avg analog_read TRIMPOT is sufficient to get the value of the trimpot and will not burden the CPU significantly You can however change the channel number in the code below from TRIMPOT to a channel number from 0 to 7 in order to measure one of the eight analog ports on the AVR include lt pololu orangutan h gt analogl for the Orangutan LV SV xx8 or Baby Orangutan B This example uses the OrangutanAnalog functions to read the voltage output of the trimpot in the background while the rest of the main loop exec
74. zzer libraries both use Timer 1 so they will conflict with each other and any other code that relies on or reconfigures Timer 1 Servos A servo motor also called hobby servo or RC servo is a device containing a motor that you can command to turn to a specific location To control a servo you must connect its three wires properly The black wire is ground and should be connected to the ground line of your Orangutan The red line is power and should be connected to power supply with a voltage that is within the operating range of your servo and that is capable of supplying all the current that your servo might draw The white line is signal and should be connected to a pin that generates servo pulses such as an I O line on the Orangutan The Orangutan I O header blocks make it easy to connect your servos because each column of the block contains ground power and an I O line in the correct order To make your servo move you must output a high pulse to the signal line every 20 ms The pulse width also called pulse length or pulse duration determines which position the servo will move to Thus every pulse width typically measured in microseconds corresponds to some angle typically measured in degrees of the servo s output shaft Typical servos have a limited range of motion and this entire range of motion can be reached with pulse widths between ms and 2 ms Take care when choosing pulse widths because some servos are capable of break

Download Pdf Manuals

image

Related Search

Related Contents

Untitled - Matco Tools RepairSolutions  TRANSMISOR FM SERIE INTEGRAL  Zurn-Wilkins 1250XL-A Installation Guide  Hyundai D467MLP  « Zen+k » A computer code for phase diagram prediction User's guide  HP 3314A User's Manual  766136  2 Esquema - Support Sagemcom  MC-500  APart E-VOL40 remote control  

Copyright © All rights reserved.
Failed to retrieve file