c13 embedded systems

11
Chapter 13: Analog to Digital Conversion, Data Acquisition and Control Throughout this course we have seen that an embedded system uses its input/output devices to interact with the external world. In this chapter we will focus on input devices that we use to gather information about the world. More specifically, we present a technique for the system to measure analog inputs using an analog to digital converter (!"#. $e will use periodic interrupts to sample the !" at a fixed rate. $e will then combine sensors, the !", software , %$M output and motor interfaces to implement intelligent control on our robot car. Learning Objectives: !evelop a means for a digital computer to sense its analog world. &eview digiti'ation )uanti'ation, range, precision and resolution. *xtend the +yquist Theorem to cases the !" is used to sense information. tudy the basics of transducers conversion of physical to electrical. -se an optical sensor to measure distance to an obect. 14.1. Analog to Digital Conversion n analog to digital converter (!"# converts an analog signal into digital form, shown in igure 01.0. n embedded system uses the !" to collect information about the external world (data acquisition system.# The input signal is usually an analog voltage, and the output is a binary number. The !" precision is the number of distinguishable !" inputs (e.g., 1234 alternatives, 05 bits#. The !" range is the maximum and minimum !" input (e.g., 2 to 67.78#. The !" resolution is the smallest distinguishable change in input (e.g., 7.78/1234, which is about 2.90 m8#. The resolution is the change in input that causes the digital output to change by 0. &ange(volts# : %recision(a lternatives# x &esolution(vol ts#  Figure 14.1 . A 12- bit ADC conve rts 0 to 3.3V o n its input into a di gital numbe r from 0 to 40!. The most pervasive method for !" conversion is the successive approi!ation technique, as illustrated in igure 01.5. 05;bit successive approximation !" is cloc<ed 05 times. t each cloc< another bit is determined, starting with the most significant bit. or each cloc<, the successive approximation hardwar e issues a new =guess= on V dac by setting the bit under test to a =0=. If V dac is now higher than the un<nown input, V in, then the bit under test is cleared. If V dac is less than V in, then the bit under test is remains 0. In this description, bit  is an unsigned integer that

Upload: anonymous-gqjhhsx9y

Post on 25-Feb-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 1/11

Chapter 13: Analog to Digital Conversion,

Data Acquisition and Control

Throughout this course we have seen that an embedded system uses its input/output devices to interact with

the external world. In this chapter we will focus on input devices that we use to gather information about the world.

More specifically, we present a technique for the system to measure analog inputs using an analog to digitalconverter (!"#. $e will use periodic interrupts to sample the !" at a fixed rate. $e will then combine sensors,

the !", software, %$M output and motor interfaces to implement intelligent control on our robot car.

Learning Objectives:

• !evelop a means for a digital computer to sense its analog world.

• &eview digiti'ation )uanti'ation, range, precision and resolution.

• *xtend the +yquist Theorem to cases the !" is used to sense information.

• tudy the basics of transducers conversion of physical to electrical.

• -se an optical sensor to measure distance to an obect.

14.1. Analog to Digital Conversion

n analog to digital converter (!"# converts an analog signal into digital form, shown in igure 01.0. n

embedded system uses the !" to collect information about the external world (data acquisition system.# The input

signal is usually an analog voltage, and the output is a binary number. The !" precision is the number of distinguishable !" inputs (e.g., 1234 alternatives, 05 bits#. The !" range is the maximum and minimum !"

input (e.g., 2 to 67.78#. The !" resolution is the smallest distinguishable change in input (e.g., 7.78/1234, which

is about 2.90 m8#. The resolution is the change in input that causes the digital output to change by 0.

&ange(volts# : %recision(alternatives# x &esolution(volts#

 Figure 14.1. A 12-bit ADC converts 0 to 3.3V on its input into a digital number from 0 to 40!.

The most pervasive method for !" conversion is the successive approi!ation technique, as illustratedin igure 01.5. 05;bit successive approximation !" is cloc<ed 05 times. t each cloc< another bit is determined,

starting with the most significant bit. or each cloc<, the successive approximation hardware issues a new =guess= on

V dac by setting the bit under test to a =0=. If V dac is now higher than the un<nown input, V in, then the bit under test iscleared. If V dac is less than V in, then the bit under test is remains 0. In this description, bit  is an unsigned integer that

Page 2: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 2/11

specifies the bit under test. or a 05;bit !", bit  goes 5219, 0251, >05, 5>4,...,0. Dout  is the !" digital output, and

 "  is the binary input that is true if V dac is greater than V in.

 Figure 14.2. A 12-bit successive appro#imation ADC.

Observation: The speed of a successive approximation !" relates linearly with its precision in bits.

 +ormally we don?t specify accuracy for ust the !", but rather we give the accuracy of the entire system (includingtransducer, analog circuit, !" and software#. n !" is !onotonic if it has no missing codes as the analog input

slowly rises. This means if the analog signal is a slowly rising voltage, then the digital output will hit all values one

at a time, always going up, never going down. The  "igure o" !erit of an !" involves three factors precision(number of bits#, speed (how fast can we sample#, and power (how much energy does it ta<e to operate#. @ow fastwe can sample involves both the !" conversion time (how long it ta<es to convert#, and the bandwidth (what

frequency components can be recogni'ed by the !"#. The !" cost is a function of the number and quality of 

internal components. Two 05;bit !"s are built into the TM1"057/AM1052 microcontroller. Bou will use !"2 to

collect data and we will use !"0 and the %!7 pin to implement a voltmeter and oscilloscope.

14.#. ADC on the $%4C1#3&L%4'1#(

Table 01.0 shows the !"2 register bits required to perform sampling on a single channel. There are two !"sC you

will use !"2 and the grader uses !"0. or more complex configurations refer to the specific data sheet. Dits 9

and 3 of the )*)C$L+C-C(+  specify the maximum sampling rate, see Table 01.5. The TM1"057 can sample

up to 0 million samples per second. Dits 9 and 3 of the )*)C$L+C-C(+  specify how fast it "E-A! sampleCthe actual sampling rate is determined by the rate at which we trigger the !". In this chapter we will use software

trigger mode, so the actual sampling rate is determined by the ysTic< periodic interrupt rateC the ysTic< I& will

ta<e one !" sample. En the TM1"057, we will need to set bits in the A%)L register to activate the analoginterface.

ddress 70;0F 04 0>;02 3 9 F;2 +ame

2x122.*022 !" MG!"%! B"TAH&""2H& 

 70;01 07;05 00;02 3;9 F;4 >;1 7;5 0;2

2x1227.9252 7 5 0 2 !"2H%&IH&  

 

70;04 0>;05 00;9 F;1 7;2

2x1227.9201 *M7 *M5 *M0 *M2 !"2H*M-GH&   

70;1 7 5 0 2

2x1227.9222 *+7 *+5 *+0 *+2 !"2H"TH& 

2x1227.922 M-G2 !"2HM-G7H&  

2x1227.921 T2 I*2 *+!2 !2 !"2H"TA7H&  

2x1227.9259 7 5 0 2 !"2H%IH&  

2x1227.9221 I+&7 I+&5 I+&0 I+&2 !"2H&IH&  

2x1227.922" I+7 I+5 I+0 I+2 !"2HI"H&  

Page 3: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 3/11

 70;05 00;2

2x1227.929 !T !"2HIE7

$able 14.1. $he $%4C ADC( registers. ach register is 3# bits /ide. *ou /ill use ADC( and /e /ill use ADC1

"or the grader and to i!ple!ent the oscilloscope "eature.

Value Description

2x7 0M samples/second2x5 >22J samples/second

2x0 5>2J samples/second

2x2 05>J samples/second

$able 14.#. $he ADC %A0ADC)D bits in the )*)C$L+C-C(+ register.

Table 01.7 shows which I/E pins on the TM1"057 can be used for !" analog input channels.

IE in 2 0 5 7 1 > 4 F 9 3 01

%D1 in02 %ort I5"l< M2%$M5 T0""%2 "+2&x

%D> in00 %ort I5ss M2%$M7 T0""%0 "+2Tx%!2 inF %ort I7"l< I0"l< I5"7"A M2%$M4 M0%$M2 $T5""%2

%!0 in4 %ort I7ss I0ss I5"7! M2%$MF M0%$M0 $T5""%0

%!5 in> %ort I7&x I0&x M2ault2 $T7""%2 -D2epen

%!7 in1 %ort I7Tx I0Tx I!G2 $T7""%0 -D2pflt

%*2 in7 %ort -F&x

%*0 in5 %ort -FTx

%*5 in0 %ort

%*7 in2 %ort

%*1 in3 %ort ->&x I5"5"A M2%$M1 M0%$M5 "+2&x

%*> in9 %ort ->Tx I5"5! M2%$M> M0%$M7 "+2Tx

$able 14.3. $/elve di""erent pins on the L%4'&$%4C can be used to sa!ple analog inputs. *ou /ill use ADC(

and # to sa!ple analog input. 2" our # pin is broen, ou /ill have the option to per"or! Lab 14 /ith3 or 5. 6e use ADC1 and D3 to i!ple!ent the oscilloscope "eature.

The !" has four sequencers, but you will use only sequencer 7 in Aabs 01 and 0>. $e set theADC(+))2+ register to 2x2057 to ma<e sequencer 7 the highest priority. Decause we are using ust one

sequencer, we ust need to ma<e sure each sequencer has a unique priority. $e set bits 0>K05 (%3# in theADC(+%70+  register to specify how the !" will be triggered. Table 01.1 shows the various ways to trigger an !" conversion. More advanced !" triggering techniques are presented in the boo< *mbedded ystems &eal;

Time Interfacing to &ML "ortex;M Microcontrollers. @owever in this course, we use software start

(%3:2x2#. The software writes an 9 ())3# to the ADC(+))2+  to initiate a conversion on sequencer 7. $e can

enable and disable the sequencers using the ADC(+AC$))+  register. There are twelve !" channels on theAM1052/TM1"057. $hich channel we sample is configured by writing to the ADC(+))%703+  register. The

mapping between channel number and the port pin is shown in Table 01.7. or example channel 3 is connected to the pin %*1. The ADC(+))C$L3+ register specifies the mode of the !" sample. $e set $)( to measure

temperature and clear it to measure the analog voltage on the !" input pin. $e set 2( so that the 283 bit is setwhen the !" conversion is complete, and clear it when no flags are needed. $hen using sequencer 7, there is only

one sample, so 8D(  will always be set, signifying this sample is the end of the sequence. In this class,

the se$uencewill be ust one !" conversion. $e set the D( bit to activate differential sampling, such as measuring

the analog difference between two !" pins. In our example, we clear D( to sample a single;ended analog input.Decause we set the 2( bit, the 283 flag in the ADC(+2)+  register will be set when the !" conversion is

complete, $e clear the 283 bit by writing an 9 to the 9 to the ADC(+2)C+  register.

Page 4: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 4/11

Value %vent  

2x2 oftware start

2x0 nalog "omparator 2

2x5 nalog "omparator 02x7 nalog "omparator 5

2x1 *xternal (%IE %D1#2x> Timer  

2x4 %$M22xF %$M0

2x9 %$M5

2x3 %$M7

2x lways (continuously sample#

$able 14.4. $he ADC %3, %#, %1, and %( bits in the ADC+%70+ register.

Ene can perform the following steps to configure the !" for software start on one channel. %rogram 01.0 shows a

specific details for sampling %*1, which is channel 3. The function ADC(+2n)eq3 will sample %*1 using softwarestart and use busy;wait synchroni'ation to wait for completion.

)tep 1. $e enable the port cloc< for the pin that we will be using for the !" input.

)tep #. Ma<e that pin an input by writing 'ero to the D2 register.

)tep 3. *nable the alternative function on that pin by writing one to the A')L register.

)tep 4. !isable the digital function on that pin by writing 'ero to the D8 register.

)tep 5. *nable the analog function on that pin by writing one to the A%)L register.

)tep 9. $e enable the !" cloc< by setting bit 04 of the )*)C$L+C-C(+  register.

)tep . Dits 9 and 3 of the )*)C$L+C-C(+  register specify the maximum sampling rate of the !". In thisexample, we will sample slower than 05> <@', so the maximum sampling rate is set at 05> <@'. This will require

less power and produce a longer sampling time, creating a more accurate conversion.

)tep ;. $e will set the priority of each of the four sequencers. In this case, we are using ust one sequencer, so the priorities are irrelevant, except for the fact that no two sequencers should have the same priority.

)tep <. Defore configuring the sequencer, we need to disable it. To disable sequencer 7, we write a 2 to bit 7

(A)83# in the ADC+AC$))+ register. !isabling the sequencer during programming prevents erroneousexecution if a trigger event were to occur during the configuration process.

)tep 1(. $e configure the trigger event for the sample sequencer in the ADC+%70+ register. or this example,

we write a 2222 to bits 0>K05 (%3# specifying software start mode for sequencer 7.

)tep 11. "onfigure the corresponding input source in the ADC))%70n register. In this example, we write the

channel number to bits 7K2 in the ADC+))%703+ register. In this example, we sample channel 3, which is %*1.

)tep 1#. "onfigure the sample control bits in the corresponding nibble in the ADC())C$Ln register. $hen

 programming the last nibble, ensure that the 8D bit is set. ailure to set the 8D bit causes unpredictable behavior.equencer 7 has only one sample, so we write a 2002 to the ADC+))C$L3+ register. Dit 7 is the $)( bit, which

we clear because we are not measuring temperature. Dit 5 is the 2( bit, which we set because we want to the 2)

 bit to be set when the sample is complete. Dit 0 is the 8D( bit, which is set because this is the last (and only#

sample in the sequence. Dit 2 is the D( bit, which we clear because we do not wish to use differential mode.

)tep 13. $e enable the sample sequencer logic by writing a 0 to the corresponding A)8n. To enable sequencer 7,we write a 0 to bit 7 (A)83# in the ADC+AC$))+ register.

void ADC(+2nit)6$rigger)eq3+Ch<=void>? volatile unsigned long dela@

  )*)C$L+C-C#+ B (((((((1(@ && 1> activate cloc "or ort

Page 5: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 5/11

  dela B )*)C$L+C-C#+@ && allo/ ti!e "or cloc to stabilie

  -2O+O$+D2+ B E((4@ && #> !ae 4 input

  -2O+O$+A')L+ B ((4@ && 3> enable alternate "unction on #

  -2O+O$+D8+ B E((4@ && 4> disable digital 2&O on #

  -2O+O$+A%)L+ B ((4@ && 5> enable analog "unction on #

  )*)C$L+C-C(+ B ((((1((((@ && 9> activate ADC(

  dela B )*)C$L+C-C#+@

)*)C$L+C-C(+ B E((((((3((@ && > con"igure "or 1#5F 

  ADC(+))2+ B ((1#3@ && ;> )equencer 3 is highest priorit

  ADC(+AC$))+ B E((((;@ && <> disable sa!ple sequencer 3

  ADC(+%70+ B E('(((@ && 1(> seq3 is so"t/are trigger

  ADC(+))%703+ B E(((('@ && 11> clear ))3 "ield

  ADC(+))%703+ GB <@ && set channel Ain< =4>

  ADC(+))C$L3+ B ((((9@ && 1#> no $)( D(, es 2( 8D(

  ADC(+AC$))+ B ((((;@ && 13> enable sa!ple sequencer 3

H

 &rogram 14.1. 'nitiali(ation of t)e ADC using soft*are start and bus+-*ait ,C14ADC/rigger.

%rogram 01.5 gives a function that performs an !" conversion. There are four steps required to perform a

software;start conversion. The range is 2 to 7.78. If the analog input is 2, the digital output will be 2, and if the

analog input is 7.78, the digital output will be 123>.

!igital ample : (nalog Input (volts# x 123># / 7.78(volts#)tep 1. The !" is started using the software trigger. The channel to sample was specified earlier in theinitiali'ation.

)tep #. The function waits for the !" to complete by polling the &I register bit 7.

)tep 3. The 05;bit digital sample is read out of sequencer 7.

)tep 4. The &I bit is cleared by writing to the I" register.

 Figure 14.3. )e four steps of analog to digital conversion 1 initiate conversion 2 *ait for t)e ADC to

 finis) 3 read t)e digital result and 4 clear t)e completion flag.

&&IIIIIIIIIIIIADC+2n)eq3IIIIIIIIIIII

Page 6: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 6/11

&& JusI/ait analog to digital conversion

&& 2nput: none

&& Output: 1#Ibit result o" ADC conversion

unsigned long ADC(+2n)eq3=void>? unsigned long result@

  ADC(+))2+ B ((((;@ && 1> initiate ))3

  /hile==ADC(+2)+((;>BB(>?H@ && #> /ait "or conversion done

  result B ADC(+))'2'O3+('''@ && 3> read result

  ADC(+2)C+ B ((((;@ && 4> acno/ledge co!pletion

  return result@

H

 &rogram 14.2. ADC sampling using soft*are start and bus+-*ait ,C14ADC/rigger.

There is software in the boo< *mbedded ystems &eal;Time Interfacing to &ML "ortex;M

Microcontrollers showing you how to configure the !" to sample a single channel at a periodic rate using a timer trigger. The most accurate sampling method is timer;triggered sampling (%3:2x>#.

Checpoint 14.1: If the input voltage is 0.>8, what value will the TM1" 05;bit !" returnN

Checpoint 14.#: If the input voltage is 2.>8, what value will the TM1" 05;bit !" returnN

14.3. 8quist $heore!

To collect information from the external world into the computer we must convert it from analog into digital

form. This conversion process is called sampling and because the output of the conversion is one digital number at

one point in time, there must be a finite time in between conversions,  t . If we use ysTic< periodic interrupts, then

this t  is the time between ysTic< interrupts. $e define the sampling rate as

 f  s : 0/ t 

If this information oscillates at frequency f , then according to the 8quist $heore!, we must sample that signal at

 f  s O 5 f 

urthermore, the 8quist $heore! states that if the signal is sampled with a frequency of f  s, then the digitalsamples only contain frequency components from 2 to P  f  s. "onversely, if the analog signal does contain frequency

components larger than P  f  s, then there will be an aliasing error during the sampling process (performed with a

frequency of  f  s#. Aliasing is when the digital signal appears to have a different frequency than the original analog

signal.

igure 01.1 shows what happens when the +yquist Theorem is violated. In both cases a signal was sampledat 5222 @' (every 2.> ms#. In the first figure the 522 @' signal is properly sampled, which means the digital samples

accurately describe the analog signal. @owever, in the second figure, the 5522 @' signal is not sampled properly,

which means the digital samples do not accurately describe the analog signal. This error is called aliasing. liasing

occurs when the input signal oscillates faster than the sampling rate and it characteri'ed by the digital samplesQloo<ing li<eR it is oscillating at a different rate than the original analog signal. or these two sets of sampled data,

notice the digital data are exactly the same.

Page 7: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 7/11

 

 Figure 14.4. Aliasing occurs *)en t)e input analog signal oscillates faster t)an t)e rate of t)e ADC sampling.

Observation If f max is the largest frequency component of the analog signal, then you must sample more than ten

times f max  in order for the reconstructed digital samples to loo< li<e the original signal when plotted on a voltage

versus time graph.

14.4. Data Acquisition and Control )ste!s

The !easurand is a real world signal of interest li<e sound, distance, temperature, force, mass, pressure,

flow, light and acceleration. igure 01.> shows the data flow graph for a data acquisition system or control system.

The control sste! uses an actuator to drive an output in the real world to a desired value while the data

acquisition sste! has no actuator because it simply measures the output in a nonintrusive manner.

Page 8: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 8/11

 Figure 14.!. ignal pat)s a data ac$uisition s+stem.

The input or measurand is  #. The output is +. transducer converts # into +. *xamples include

ound Microphone%ressure, mass, force train gauge, force sensitive resistor 

Temperature Thermistor, thermocouple, integrated circuits

!istance -ltrasound, lasers, infrared lightlow !oppler ultrasound, flow probecceleration ccelerometer  

Aight "amera

Diopotentials ilver;ilver "hloride electrode

non!onotonic transducer is an input/output function that does not have a mathematical inverse. or example, if two or more input values yield the same output value, then the transducer is nonmonotonic. oftware will

have a difficult time correcting a nonmonotonic transducer. or example, the harp %5B250BJ I& distance

sensor has a transfer function as shown in igure 01.4. If you read a transducer voltage of 5 8, you cannot tell if the

obect is 7 cm away or 05 cm away. @owever, if we assume the distance is always greater than 02cm, then thistransducer can be used.

 

 Figure 14.5. )e )arp '6 distance sensor e#)ibits nonmonotonic be)avior.

!etails about transducers and actuators can be found in *mbedded ystems &eal;Time Interfacing to

&ML "ortex;M Microcontrollers, 5207, ID+ 3F9;0147>320>1.

Page 9: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 9/11

14.5. obot Car Controller

The goal is to drive a robot car autonomously down a road. utonomous driving is a difficult problem, andwe have greatly simplified it and will use this simple problem to illustrate the components of a control system. *very

control system has real;world parameters that it wishes to control. These parameters are called state variables. In

our system we wish to drive down the middle of the road, so our state variables will be the distance to the left side of 

the road and the distance to the right side of the road as illustrated in igure 01.F. $hen we are in the middle of theroad these two distances will be equal. o, let?s define %rror  as

 %rror : Dleft K Drig)t 

If %rror  is 'ero we are in the middle of the road, so the controller will attempt to drive the %rror  parameter to 'ero.

 Figure 14.7. &)+sical la+out of t)e autonomous robot as is drives do*n t)e road.

$e will need sensors and a data acquisition system to measure  Dleft   and Drig)t . In order to simplify the

 problem we will place pieces of wood to create walls along both sides of the road, and ma<e the road the same width

at all places along the trac<. The harp %5B250BJ2 infrared obect detector can measure distance(http//www.sharpsma.com# from the robot to the wood. This sensor creates a continuous analog voltage between 2

and 678 that depends inversely on distance to obect, see igure 01.4. $e will avoid the 2 to 02 cm range where the

sensor has the nonmonotonic behavior. $e will use two !" channels (%*1 and %*># to convert the two analog

voltages to digital numbers. Aet Le"t and ight be the !" digital samples measured from the two sensors. $e canassume distance is linearly related to 0/voltage, we can implement software functions to calculate distance in mm as

a function of the !" sample (2 to 123>#. The 510901 constant was found empirically, which means we collecteddata comparing actual distance to measured !" values.

Dle"t : 510901/Le"t Dright : 510901/ight

igure 01.9 shows the accuracy of this data acquisition system, where the estimated distance, using the

above equation, is plotted versus the true distance.

 Figure 14.8. 9easurement accurac+ of t)e )arp :&2;0A21;<0F distance sensor used to measure distance

to *all.

Page 10: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 10/11

 +ext we need to extend the robot built in *xample 05.5. irst we build two motor drivers and connect one

to each wheel, as shown in igure 01.3. There will be two %$M outputs %4 controls the right motor attached to the

right wheel, and %> controls the left motor attached to the left wheel. The motors are classified as actuators because

they exert force on the world. imilar to *xample 05.5 we will write software to create two %$M outputs so we canindependently adust power to each motor. If the friction is constant, the resistance of the motor, 6, will be fixed and

the power is

 &o*er : (9.15/ 6#S = /(@6A#

$hen creating %$M, the period (@6A# is fixed and the duty cycle is varied by changing @. o we see the robot

controller changes @, it has a linear effect on delivered power to the motor.

 Figure 14.. Circuit diagram of t)e robot car. >ne motor is *ire reversed from t)e ot)er because to move

 for*ard one motor must spin cloc?*ise *)ile t)e ot)er spins countercloc?*ise.

The currents can range from >22m to 0 , so TI%052 !arlington transistors are used, because they can sin< up to 7. +otice the dar< blac< lines in igure 01.3C these lines signify the paths of these large currents. +otice also the

currents do not pass into or out of the Aaunch%ad. igure 01.02 shows the robot car. The two I& sensors are

 positioned in the front at about 1> degrees.

 Figure 14.10. &)oto of t)e robot car.

Page 11: C13 Embedded Systems

7/25/2019 C13 Embedded Systems

http://slidepdf.com/reader/full/c13-embedded-systems 11/11

igure 01.00 illustrates the feedbac< loop of the control system. The state variables are  Dleft  and Drig)t . The

two sensors create voltages that depend on these two state variables. The !" samples these two voltages, and

software calculates the estimates Dle"t  and Dright. rror is the difference between Dle"t and Dright. The right

motor is powered with a constant duty cycle of 12, while the duty cycle of the left motor is adusted in an attemptto drive down the middle of the road. $e will constrain the duty cycle of the left motor to between 72 and >2, so

it doesn?t over compensate and spin in circles. If the robot is closer to the left wall (Dle"t U Dright# the error will be

negative and more power will be applied to the left motor, turning it right. "onversely, if the robot is closer to the

right wall (Dle"t O Dright# the error will be positive and less power will be applied to the left motor, turning it left.Ence the robot is in the middle of the road, error will be 'ero, and power will not be changed. This control algorithm

can be written as a set of simple equations. The number Q522R is the controller gain and is found by trial and error 

once the robot is placed on the road. If it is slow to react, then we increase gain. If it reacts too quic<ly, we decrease

the gain.

rror B Dle"t I Dright

 Le"tK B Le"tK #((Mrror@

i"=Le"tK N 3(M;((> Le"tKB3(M;((@ && 3( !in

i"=Le"tK P 5(M;((> Le"tKB5(M;((@ && 5( !a

Le"tL B ;(((( I Le"tK@ && constant period

Observation: In the field of control systems, a popular approach is called %I! control, which stands for proportional

integral derivative. The above simple algorithm actually implements the integral term of a %I! controller.

urthermore, the two i"  statements in the control software implement a feature called antiIreset /indup.

These controller equations are executed in the ysTic< I& so the controller runs at a periodic rate.

 Figure 14.11. @loc? diagram of t)e closed loop used in t)e robot car.