Professional Documents
Culture Documents
usingaMicrocontroller
ECEN2270
ElectronicsDesignLaboratory
Lab4Plan
Tuesday:finishpartsAandB
Thursday:Lab4demowithabatterypackpoweredrobot
Stop,waitfortheswitchtobeintheONposition
Wait1second
360o clockwiserotationoftherobot(notthewheel)
Stopandwait1second
360o counterclockwiserotationoftherobot(notthewheel)
Stopandwait1second
Accuracy:therobotshouldcomebacktothestartingposition
Thedemocanbecompletedwithjustonewheel,orwithbothwheels
Lab4QuiznextMonday
ECEN2270
ElectronicsDesignLaboratory
Lab4,PartBTopics
1. Robotpositioncontrol,introtohardwareinterrupts
2. CommunicationbetweenPCandArduino whilea
programisrunning
Readingvariablevaluesduringprogramexecution,debugging
SendingcommandsoralteringoperationfromPC
ECEN2270
ElectronicsDesignLaboratory
PositionControl
Robotpositioningtaskexamples:
Turn45o
Do3rotationsofawheel
Moveforward325cm(requiresdrivesonbothwheels)
Lab4demo:360o clockwise,followedby360o counter
clockwise
ECEN2270
ElectronicsDesignLaboratory
PositionControlApproach1:distance=speed*time
ThisiswhatyouareexpectedtodoinLab4PartATask3:
Modifythespeedcontrolcodetoperformthefollowinginvoidloop():
Stop,waitfortheswitchtobeintheONposition
Wait1second
360o clockwiserotationoftherobot(notthewheel)
Stopandwait1second
360o counterclockwiserotationoftherobot(notthewheel)
Solution:pickaspeedreferenceanddoabitofgeometry
tofigureouthowlongthewheelshouldmove(needto
knowthespeedsensorgainKsense =vref/)
Wheeldiameter:2*r =13cm
Distancebetweenwheels:2*rw =28cm
ECEN2270
ElectronicsDesignLaboratory
PositionControlApproach1:distance=speed*time
t
Vref t
K sense
r
Vref(t) = d(t) 5V
distance
r Vref t
time
distance * r * t
K sense
Lab4demowithtwowheels: distance 2 * rw
Lab4demowithonewheel: distance 2 * 2rw
Wheelradius: r =6.5cm
Wheeltocenter:rw =14cm
Whatdoto:
KnowKsense foryourspeedsensor,choosed tosetthewheelspeed,calculate
timerequiredtomovethespecifieddistance
ElectronicsDesignLaboratory
PositionControlApproach1:distance=speed*time
t
Vref t
K sense
r
Vref(t) = d(t) 5V
distance
Thisapproachisprettygoodbut:
DependsontheexactknowledgeofKsense,i.e.ton inthespeedsensor
Speedcontrolisnotinstantaneous;itisdifficulttocompensatefor
errors
delay(time)doesnotallowC todoanyothertask
ECEN2270
ElectronicsDesignLaboratory
PositionControlApproach2:distance encodercount
Encodergenerates12*64=768pulsesforeachwheelrotation
Wheelradius,r =6.5cm
Wheelperimeter,p =2*r =40.85cm
1encoderpulseisworthp/768=0.53mmoflineardistance
Givenatargetdistance,itisveryeasytocalculatethetarget
numberofencoderpulses
Example:do3rotationsofawheel,target=3*12*64=2304pulses
Allweneedtodoistocounttheencoderpulses
Encoderoutput
+1
ECEN2270
ElectronicsDesignLaboratory
+1
PositionControlApproach2
N enc 12 64 turns
r
Vref(t) = d(t) 5V
N enc
distance
2r
12 64
distance
Eachencoderpulserepresentsafractionofawheelturn
0.53mmofforwardmotionperencoderpulse
Distancereaddirectly,withoutguessingspeed/distancerelation
Countingrisingedgesof
encodertotelldistance
+0.53mm
+0.53mm
Thisisbetterbuthowdowereadencoderpulses?
Thesimpleapproachiscalledpolling,orbusywait
Thebetterapproachuseseventdriven(interrupt)programming
ECEN2830
ElectronicsDesignLaboratory
ProgramArduino tocountencoderpulses:PollingApproach
Pollencoderoutput
+1
+1
ECEN2270
ElectronicsDesignLaboratory
10
ProblemswiththePollingApproach
Pollencoderoutput
+1
+1
Cantdoanythingelsewhilepollingandcountingtheencoderpulses!
ECEN2270
ElectronicsDesignLaboratory
11
PositionControlApproach2 EventDriven
Encoderoutput
C executes
othertasks
C executes
othertasks
Increment
C executes
othertasks
Increment
Wantourprogramtodousefulstuffbetweenencoderpulses
Wecantdothisifwespendallourtimewaitingforpulsesthisistheissuewiththe
busywait,orpollingprogrammingapproach
Ideally,whenarisingencoderpulseisseen,ourmicrocontrollerwithswitch
tasksforasecondtoincrementourencodercounter
Thisisdonebyinterrupting themainprogram,switchingtoasmallfunction
tohandlethepulse,thenswitchingbacktothemainprogram
Thesmallfunctioniscalledaninterruptserviceroutine,andhandlinginputs
thiswayiscalledeventdriveninput/outputprogramming
ECEN2830
ElectronicsDesignLaboratory
12
Eventdrivenapproach:HardwareInterrupts
Encoderoutput
+1
+1
C executes
othertasks
C executes
othertasks
C executes
othertasks
ISR
ISR
Risingedgeoftheencoderoutput
triggersexecutionofanInterrupt
ServiceRoutine(ISR)
ECEN2270
ElectronicsDesignLaboratory
13
PositionControlUsingHardwareInterrupt
Seecompleteposition_control_example programpostedontheLab4page
// encoder counter variable
volatile int enc_count_Left = 0; // "volatile means stored in RAM
void setup(){ (other setup 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 interrupt service routine attached to Interrupt 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++;
}
ECEN2270
ElectronicsDesignLaboratory
14
Whatshouldspeedreferencebe
duringpositioning?
Goals:accuracyandspeed
Desired
Position
2r
12
64
Desired
Nenc
Nenc
Motor
Control Vref
Code
Counter
ISR
1
s
1
s
o
o
2
Encoder
SeeErickson'slecturenotesonOptimalPositioningstrategiesontheLab4page
ECEN2270
ElectronicsDesignLaboratory
15
Howtocheckwhatisgoingonduringexecution
ofaprogramonArduino?
PCtoArduinocommunicationviaUSB:
SerialMonitor
ECEN2830
ElectronicsDesignLaboratory
16
PCtoArduino communicationviaUSB
serial_read_example (programpostedontheLab4page)
/*
ECEN2270 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
}
void loop() {
int 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
}
ECEN2270
ElectronicsDesignLaboratory
17
PCtoArduino communicationviaUSB
SerialMonitor
window
0:SwitchisOFF
1:SwitchisON
ECEN2270
ElectronicsDesignLaboratory
18
Lab4Demo
Bepreparedto:
Showhowtherobotpoweredfrom2batterypacksinseries
(approximately10V)canaccomplishthespecifiedPartB
positioningtask
Extracredit willbegivenforexceptionallyfastandaccurate*
positioning
Showyourpositioncontrolprogram
Showcompletespeedcontrolcircuit,andcompleteLTspice
diagramofyourspeedcontrolcircuit
Answerquestionsrelatedtoyourpositioncontrolcodeand
speedcontrolcircuit
* YoumayuseSerialMonitortodisplaytheactualencodercountafter
completionofthepositioningroutine
ECEN2270
ElectronicsDesignLaboratory
19