Professional Documents
Culture Documents
using a Microcontroller
using a Microcontroller
ECEN 2830 Electronics Design Laboratory 1
Lab 4, Part B Topics
1. Robot position control, intro to hardware interrupts
2. Communication between PC and Arduino while a
program is running
program is running
• Reading variable values during program execution, debugging
• Sending commands or altering operation from PC
Sending commands or altering operation from PC
ECEN 2830 Electronics Design Laboratory 2
Position Control
• Robot positioning task examples:
Robot positioning task examples
– Turn 45o
– Do 3 rotations of a wheel
Do 3 rotations of a wheel
– Move forward 325 cm (requires drives on both wheels)
ECEN 2830 Electronics Design Laboratory 3
Position Control Approach 1: distance = speed*time
• This is what you are expected to do in Lab 4 Part A Task 3:
This is what you are expected to do in Lab 4 Part A Task 3:
– Modify the speed control code to perform the following in void loop():
• Stop, wait for the switch to be in the ON position
• Wait 1 second
Wait 1 second
• 360o clockwise rotation of the robot (not the wheel)
• Stop and wait 1 second
• 360o counter clockwise rotation of the robot (not the wheel)
( )
• Solution: pick a speed reference and do a little bit of
geometry to figure out how long the wheel should move
(need to know the speed sensor gain Ksense = v
(need to know the speed sensor gain K = vref/)
• This approach is pretty good but:
– Depends on the knowledge of K
p g sense,, i.e. ton in the speed sensor
p
– Speed control is not instantaneous; it is more difficult to
compensate for errors
delay(time) does not allow C to do other tasks
– delay(time) does not allow C to do other tasks
ECEN 2830 Electronics Design Laboratory 4
Position Control Approach 2: distance encoder count
• Encoder generates 12*64 = 768 pulses for each wheel rotation
Encoder generates 12*64 = 768 pulses for each wheel rotation
• Wheel radius, rw = 6.5 cm
• Wheel perimeter, p = 2
Wheel perimeter, p 2*rrw = 40.85 cm
40.85 cm
• 1 encoder pulse is worth p/768 = 0.53 mm of linear distance
• Given a target distance, it is very easy to calculate the target
number of encoder pulses
• Example: do 3 rotations of a wheel, target = 3*12*64 = 2304 pulses
• All we need to do is to count the encoder pulses
ll d d i h d l
Encoder output
Encoder output
+1 +1
ECEN 2830 Electronics Design Laboratory 5
Lab 4 Part B Position Control Setup
Pin 7
1K
1K
Pin 8
Pin 8
Pin 9
+5 V
ON Pin 2
Power supplies: 1K
Pin 6 ON/OFF switch
1K +10V from bench power supply (or from battery)
0 f b h l ( f b )
OFF
+5V from Arduino board
ECEN 2830 Electronics Design Laboratory 3
Program Arduino to count encoder pulses: Polling Approach
Poll encoder output
+1 +1
ECEN 2830 Electronics Design Laboratory 6
Problems with the Polling Approach
Poll encoder output
+1 +1
Can’t do anything else while polling and counting the encoder pulses!
ECEN 2830 Electronics Design Laboratory 7
Much better approach: Hardware Interrupt
Encoder output
+1 +1
ISR ISR
Rising edge of the encoder output
triggers execution of an Interrupt
Service Routine (ISR)
ECEN 2830 Electronics Design Laboratory 8
Position Control Using Hardware Interrupt
See complete position_control_example program posted on the Lab 4 page
void setup(){
p(){ (
(other setup
p lines not shown here)
)
/*
Connect left-wheel encoder output to pin 2 (external Interrupt 0)
Rising edge of the encoder signal triggers an interrupt
count_Left is the interruptp service routine attached to Interrupt
p 0
*/
attachInterrupt(0, count_Left, RISING);
}
/*
Interrupt 0 service routine:
Increment enc_count_Left on each rising edge of the encoder signal
*/
void count_Left(){
enc_count_Left++;
}
ECEN 2830 Electronics Design Laboratory 9
PC‐to‐Arduino communication via USB
serial_read_example (program posted on the Lab 4 page)
/*
ECEN2830 example of displaying a value using Serial Monitor
Open the Serial Monitor window: Tools > Serial Monitor
*/
// define pins
const int pinON = 6; // connect pin 6 to ON/OFF switch
// via 1k resistor, active HIGH
void setup() {
Serial.begin(9600); // start serial communication
// via USB at 9600 bits per second (baud)
pinMode(pinON INPUT); // set on/off switch pin as input
pinMode(pinON,
}
void loop() {
int onoff_switch
onoff switch = digitalRead(pinON);// read switch on/off state
Serial.println(onoff_switch); // send value as
// ASCII-encoded decimal
delay(500); // wait 0.5 seconds
}
ECEN 2830 Electronics Design Laboratory 10
PC‐to‐Arduino communication via USB
Serial Monitor
window
0: Switch is OFF
1: Switch is ON
ECEN 2830 Electronics Design Laboratory 11
Lab 4 Demo
Be prepared to:
Be prepared to:
• Show how the robot powered from 2 battery packs in series
(approximately 10 V) can accomplish the specified Part B
positioning task. Extra credit will be given for exceptionally
fast and accurate* positioning
• Show your position control program
Show your position control program
• Show complete speed control circuit, and complete LTspice
g y p
diagram of your speed control circuit
• Answer questions related to your position control code and
speed control circuit
* You
You may use Serial Monitor to display the actual encoder count after
may use Serial Monitor to display the actual encoder count after
completion of the positioning routine
ECEN 2830 Electronics Design Laboratory 12