150 likes | 304 Views
CSC-2700 – (3) Introduction to Robotics. Robotics Research Laboratory Louisiana State University. What we learned in last class. Analog to Digital (ADC, A/D or A to D) Converting an analog voltage to a digital value that can be used by a microcontroller.
E N D
CSC-2700 – (3) Introduction to Robotics Robotics Research Laboratory Louisiana State University
What we learned in last class • Analog to Digital (ADC, A/D or A to D) • Converting an analog voltage to a digital value that can be used by a microcontroller. • There are many sources of analog signals to be measured such as light intensity, temperature, distance, position, etc. • ATMega128 ADC has 10 bits resolution (0~1024) • Has 8 channels through a multiplexer • 8 pins on PORTF • Need to set PORTF as input without pull-up • Has own power supply (labeled AVCC) • Allows measuring voltages from 0 to 5 volts with a resolution of 5/1024 volts, or 4.88 mV
IR sensor (line detector) & ADC 0V drop = Xi mA x 0 Ω 5 V S1 = 0 ~ 20000 Ω + Connect to ADC X V drop = Xi mA x S1 Ω 5 V - R2 = 1000 Ω X v+ restV drop = Xi mA x 1000Ω 0 V Resistance value of S1(IR sensor) can be changed by sensing
A2D function uint16_t a2d_10( uint8_t Channel ){ // Select the channel in a manner which leaves REFS0 and REFS1 un touched. ADMUX = ( ADMUX & (( 1 << REFS1 ) | ( 1 << REFS0 ))) | Channel; // Start the conversion ADCSR = ADCSR | ( 1 << ADSC ); // Wait for it to complete while ( ADCSR & ( 1 << ADSC )); return ADC; // ADC defined at avr/iom128.h ( special function register: SFR_IO16) } // a2d_10 /home/csc2700/csc2700/40-ADC-01
Connection configuration for UART • Our programmer has 2 serial port • ttyACM0 : ISP programming port • ttyACM1 : UART serial port • Wire connection • PE0 Yellow wire • PE1 Green wire • GND Black wire • Open Gtk-term • Set port : /dev/ttyACM1 • Speed : 57600 for ttyACM1 9600 for Bluetooth connection
Send A2D values through UART • Config.h • Set : #define CFG_USE_UART0 1 • Hardware.h • Set : #define UART0_BAUD_RATE 57600 • ADC_test.c • Add : #include "UART.h” • Create file pointer : FILE *u0; // for UART0 • Open u0 • if defined( __AVR_LIBC_VERSION__ ) • u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio ); • #else • u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio, 0 ); • #endif • Send values using fprintf(u0,”your message %d”, variable); /home/csc2700/csc2700/40-ADC-02
Receiving values from UART • Check the UART buffer first • int UART0_IsCharAvailable() • Read a character from UART buffer • int UART0_GetChar() int counter; char tmpChar; While(1){ if ( UART0_IsCharAvailable() ) { tmpChar = UART0_GetChar(); if ( tmpChar == ‘s'){ // start moving }else if ( tmpChar == ‘c'){ // clear counter }else if ( tmpChar == ‘r’){ // report counter number } } }
Let’s make simple UART program • Make a led0 on when ‘0’ message is received from UART • Make a led0 off when button0 is pressed • Make a led1 on when ‘1’ message is received from UART • Make a led1 off when button1 is pressed • Send “!!!good bye!!!” message to UART tx when “bye” message is received from UART rx
Sensors IR photo Temperature GAS Acceleration Humidity Sonar
Interrupt • Internal Interrupt • Timer interrupt • Counter interrupt • External Interrupt • INT0 = SCL [PD0] • INT1 = SDA [PD1] • INT2 = RXD1 [PD2] • INT3 = TXD1 [PD3] • INT4 = OC3B [PE4] • INT5 = OC3C [PE5] • INT6= T3 [PE6] • INT7= ICP3 [PE7] • RESET= RESET [RESET] • SPI, UART, etc
Interrupt Procedure Main program Main program Interrupt occur Interrupt service routine Load PC from Stack then, return to Main program Execute Interrupt Service Routine Interrupt Register Bit check Jump to Triggered Interrupt Vector Interrupt Trigger check Save Main program PC at stack
External Interrupt trigger 1) Falling edge 2) Rising edge High: + 4 V 3) Low level Low: 0.9 V
Interrupt Setting // Grab the rising edge. EICRB |= (( 1 << ISC71 )|( 1 << ISC70 )|( 1 << ISC61 )|( 1 << ISC60 )); // External interrupt control register B EIFR = (( 1 << INTF7 ) | ( 1 << INTF6 )); // External interrup flag register EIMSK |= (( 1 << INT7 ) | ( 1 << INT6 )); // External interrup mask register DDRE &= ~(( 1 << 6 ) | ( 1 << 7 )); // PE6 & PE7 set input PORTE |= (( 1 << 6 ) | ( 1 << 7 )); // pullup for input sei(); // set interrupt cli(); // unset interrupt
Interrupt Service Routine intleftCounter = 0; intprevLeft = 0; intrunFlag = 0; long cnt = 9000000; main(1){ // Grab the rising edge. EICRB |= (( 1 << ISC71 )|( 1 << ISC70 )|( 1 << ISC61 )|( 1 << ISC60 )) // External interrupt control register B EIFR = (( 1 << INTF7 ) | ( 1 << INTF6 )); // External interrup flag register EIMSK |= (( 1 << INT7 ) | ( 1 << INT6 )); // External interrup mask register while(1){ if (leftCounter != prevLeft){ prevLeft = leftCounter; PIN(LED,1,SET_TOGGLE); } while (cnt-- > 0){ } cnt = 9000000; } } // interrupt service routine for int6 SIGNAL(SIG_INTERRUPT6){ leftCounter++; PIN(LED,0,SET_TOGGLE); } // SIG_INTERRUPT6
Let’s make distance measuring program • Make a counter for the optical switches on wheels • Make a clear the counter when ‘c’ message is received from UART • Make a report the counter when ‘r’ message is received from UART • Make a robot move forward when ‘s’ message is received from UART until the counter value is 90