Вы находитесь на странице: 1из 20

Project3:CalPolySkateboard

EthanArchibald&ChristinaSardo
CPE32913|LABCPE32914
Spring2015
Dr.Oliver

SystemRequirements
Thegoalofthisprojectwastocreateanelectricallypoweredskateboardwherethe
speedoftheskateboardwouldbecontrolledbytheridersbodypositionandweight
distributionontheboard.Theskateboardsensestheweightdistributionoftheriderand
fromthisdistributionactuatesamotor.

SystemSpecifications
Toaccomplishthistaskcertainspecificationswereset..Theskateboardneededtobe
abletowithstandtheweightofapersonof200pounds.Theskateboardneededto
achieveamaxvelocityoftenmilesperhour.Anotherspecificationofthissystemwas
thataridershouldhavefifteenminutesofridetimeandbeabletotravelmorethan
twentyfeet.

SystemArchitecture
Thispressuresensitiveskateboardusedamicrocontrollerwithvarioushardware
componentsinterfacedtocapturethechangingofaridersweightdistributiononthe
boardintoaviablecomputerprogram.Inordertodoso,twoforcesensitiveresistors
(FSR)wereusedtomeasureandthensendtothemicrocontrolleravaryinganalog
voltage.Thecomparisonofthetwovalueswouldbeusedtodeterminethespeedof
theskateboard.

Whenariderstandsontheskateboard,his/herweightisrecordedanditisassumed
thathe/sheisstandingwithanevenweightdistribution(halfofweightisdistributedon
eachleg).Whenariderleansforward,theFSRwillregisterthefrontfootholdingmore
weightthantheotherandtheskateboardwillaccelerate.Whenariderleansbackward,
thebackfootwillholdthemajorityoftheridersweight,andtheskateboardwill
decelerate

AftermeasuringtheridersweightdistributiondividedamongstafrontandbackFSR,
programmingembeddedintotheskateboardsmicrocontrollerwouldfirstchangeeach
FSRsanalogvoltagereadingintoadirectcurrentvoltageviathemicrocontrollersbuilt
inADCconverter.Thatweightdistributionreading,wouldthenbescaledtoaPulse
WidthModulation(PWM)dutycyclevaluewhichwouldacceleratethemotortoan
appropriatedegree.

TheoverallflowchartfortheprogramisshowninFigure1.Afterinitializationofthe
ADCandPWM,theprogrampollsforreadingsfromtheFSRandwillthenincreaseor
decreasethePWMdutycyclevaluedependingonwhetherthevoltagereadingislarger
inthefrontorback(asmoreloadisappliedtotheFSR,theresistancedecreasesthus
increasingthevoltage).

Figure1.OverallFlowchartfortheCalPolySkateboard(CPS).

Figure2showstheoverallcircuitdiagramofthesystem.ThefrontandbackFSRwere
connectedtotheADCchannelsoftheATmega328pmicrocontrolleronpinsPC0and
PC1respectively.ThePWMoutputsignalsPWMandDIRweresenttothemotor
controller(CytronMD10C)viapinsPD5andPD6.Themotorcontrollerwouldthen
amplifythePWMsignalfromanexternal24VDCbatterysourcetoahighervoltage
andcurrentandsendthatincreasedsignaltoa24VDCmotor(MY69).

Figure2.OverallCircuitDiagramofCPS.

ComponentDesign
Therewereseven(microcontroller,FSR,100Kresistor,motorcontroller,9Vbattery,
24Vbattery,and24VDCmotor)differentelectricalhardwarecomponentsneededfor
thispressuresensitiveelectricalskateboardwhicharedescribedbelow.

Microcontroller
ThemicrocontrollerusedwasanATmega328p.TheATmega328pwasselected
becauseofitsextensivecompatiblebuiltinperipherals,capabilities,andextended
onlineusergeneratedhelpandhowtoforumsasitisfoundonthepopularArduino.
TheATmega328phadnecessaryfeaturesincludingsufficientADCchannelstoread
theFSRs.Also,thebuiltintimerswithinthemicrocontrollercanbesettoPWMmodes.

ForceSensitiveResistor(FSR)
Flexiforce force sensitive resistor pads were chosen due to ease of packaging and
relative ease of implementation, see Figure 3.. The FSRs were capableof measuring
loadsofupto50kg.

Figure3.FlexiForceForceSenstiveResistor.

MotorController
For simplicityandease oftroubleshooting,aCytron 10Amotorcontrollerboard10ADC
motordriverwasselectedandisshownbelowinFigure4.

Figure4.Cytron10Amotorcontrollerboard

Features include bidirectional control for a single motor, and continuous 13A control.
and able to accept voltages ranging from 5 to 25 V. The interfacing is done with a
directionallogicsignalandpulsewidthmodulation(PWM)forspeedcontrol.

24VPowerizerDCbatteryNiMHbattery
The 24 V DC battery provided sufficient power needed by the 24 V DC motor, see
Figure 5. NiMH was a suitable battery type since it is better forhigher dischargetype
applications and allows for recharging while still being less expensive than more
efficientLibatterybasedtypes.


Figure5.24VDC3300mAHNiMHbatterypack

9VDCBattery
An external 9V DC type battery was used tosupply powerto the microcontroller.(this
componentisnotshowninthesystemcircuitdiagramshownonFigure2).

24VDCBrushed100Wmotor
This motor (model: MY69) was selected for its relative low cost and adequate torque
outputforaprototypepurpose,seeFigure6.

Figure6.24V100WDCBrushPermanentMagnetMotor

ProjectCodeListing
Figure7

istheinitializationfunctionforthePulseWidthModulationusedtooutputtothe
4
motor.ThegroupbeganbyusingthecodegiveninthePWMtutorial
inclassasa
baseandthenediteditinordertohaveitfitthisprojectsneeds.Afterlookingatthe
ATmegadatasheet(p.108)thegroupdecidedtoleavetheTCCR0AvaluesettoFast
PWMmodeinordertohavetimercountfrom0255(the255valueisduetousingthe
8bittimer0).Thealternative,PhaseCorrectPWMmode,wouldhavethetimercount
upto255andthenbackdownagain,whichisnotwhattheprojectneeded,asthegroup
wantedtheskateboardtoacceleratetoitsmaximumspeedandthenremainthereuntil
theriderchosetodecelerate.

FortheTCCR0Bvalue,thegroupinitiallystartedwiththeprescalarsetto64(TCCR0B
=3),butafterridetestingdeterminedthatdividingtheclockbythatfactorsloweddown
theaccelerationtoapointwhereitwasdifficultfortheridertostartfromrest,sothe
grouploweredtheprescalarto8inordertoincreasetheaccelerationatafasterrate
andhaveahighermaximumvelocity(p.112).FinallythegroupinitializedtheOCR0A
valueto0tohavetheboardstartatrestandremainthereuntiltheriderchoseto
accelerateandbeginincreasingtheOCR0Avalue.Thegroupalsooriginallyincluded
anOCR0BvaluethatwaslinkedtoPD6tomovethemotorbackwards,butaftertesting
thegroupdetermineditwastoodifficultfortherider(andthusimpractical)tomakethe
skateboardmovebackwardsandtheyremovedtheOCR0Bvaluefromthecode.

Figure7.

InitializationoftheregistersforPWM

Figure

8showstheinitializationofregistersrelatedtotheanalogtodigitalconverter.
ThefunctionbeginsbyinitializingtheADCSRA(ADCControlandStatusRegisterA).
ThebitsaresettoenabletheADC(bit7),andsettheADCprescalartosystemclock/
128(p266).NotethatthetheADCstartconversionbit(bit6)isnotyetset,andwill
onlybesettohighinthe
main
functionwhentheprogramisreadingfromthesensors.

Forthisproject,enablingandusingADCinterruptswasnotnecessary.Becauseofthis,
ADCSRBisalsoclearedbecausethereisnointerruptflagthatwilltriggeraconversion
(p267)asitistriggeredmanuallyinthemain.TheADMUXregisterisinitializedto
readingforchannelADC0,andwillbeswappedeverytimethe
Swap_ADC()
functionis
called(seefollowingFigure
##
formoreinformation).

Figure8.

InitializationoftheregistersforADC

OneproblemthattheteamranintowhencreatingtheprojectwasthattheATMegacan
onlyreadfromoneADCinputatatime.Becausetheskateboardrequiredtwosensors
(afrontandaback),aMUX(seeFigure

9)wasneededtoquicklyalternatereadingfrom
twodifferentpins.Thegroupusedaglobalvariable
swap
thattoggleseverytimethe
Swap_ADC()f
unctioniscalledthatspecifieswhichanaloginputpintoreadfrom.The
pinisspecifiedwiththeADMUXregister.Becausethetwosensorswereconnectedto
pinsADC0andADC1,thetwovaluesusedforADMUXis0x00and0x01(p265).In
addition,theDigitalInputDisableRegister0(DIDR0)isadjustedaccordingtowhich
channeliscurrentlynotbeingreadinordertohavethepintoalwaysbereadaszero
whenthebitissetandthedigitalinputfromthepinisnotneeded(p268).


Figure9.

FunctiontoswappinsforADCinput

TheaccelerateMotoranddecelerateMotorfunctions(Figure10)areusedtochangethe
speedofthemotor.BecauseaDCmotorsuchastheoneusedinthisprojectiseither
fullyonorfullyoff,pulsewidthmodulationwasneededinordertocontroltheamount
ofpoweroutputtoitthusincreaseitsspeed.Theteamdecidedtoonlyhavethemotor
moveforward,thusonlyonetimervalue(OCR0A)wasneeded.Toacceleratethe
motor,theOCR0Aisincremented,untilitreachesitsmaxvalueof255,whereit
remainsconstantuntilthedeceleratefunctioniscalled.

Originally,theOCR0Avaluewasalwaysincrementedby1,howeveraftertestingthe
boardbyridingitthegroupfoundthattheboardacceleratedfromresttooslowly,thus
makingitdifficultfortheridertoremainbalanced.Tocounterthis,aconditionalwas
addedthatincreasedtheOCR0Aataquickerrate(+=10)whentheOCR0Avaluesare
lower(lessthan100)inordertoacceleratetheskateboardquicklyandthenreturnedto
onlyincreasingatafactorof+=1oncetheskateboardgainedsomemomentum.

Similarly,fordecelerateMotor,thegroupstartedbydecrementingtheOCR0Avalueby
1eachtimethefunctionwascalled,butaftertestridingtheboardtheteamquickly
realizedthattheitwasdeceleratingtooslowlyandtheriderwasunabletostopintime
topreventfromcrashing.Thus,thedecrementvaluewasincreasedto=5untilthe
OCR0Avaluebecomessolowthatitisunabletodecrementanymorewithout
becomingnegative,whereitissetto0(boardisatrest).


Figure10.

Functionstoaccelerateanddeceleratemotors

Themajorityoftheprogramlogicisfoundwithinthewhileloopofthemainfunction
(Figure11).Theloopconstantlypollsforareadingfromeitheroneofthesensors
basedontheglobal
swap
variablesetinthe
Swap_ADC()
function.Theloopbeginsby
startingtheAnalogtoDigitalconversionbysettingtheADCSRA(ADCControland
StatusRegisterA).ThebitsaresettoenabletheADC(bit7),starttheADCconversion
(bit6),andsettheADCprescalartosystemclock/128(p266).Forthisproject,
enablingandusingADCinterruptswasnotnecessary.

TheADCSRAregisterinitializationisthenfollowedbyasoftwaredelayof260usin
ordertoensureasufficientsamplingtimefortheADCchannelandreferenceselection
tobereadbeforepotentiallyattemptingtoreadthenextconversionwithdifferent
settings(p257).Thegroupdecidedtokeepthe260usvaluethatDr.Oliverusedinthe
ADCtutorialbecauseitwasalreadyproventobeasufficientamountoftimethrough
completingthetutorial.Thevoltagefromoneofthesensorsisthenreadusingthe
bottom12bitsoftheADCandthevalueisstoredinthe16bitunsignedintvalue
voltage
.Then,basedonwhateverthe
swap
valueissetto(whichissetinthe
Swap_ADC()
functionthatdetermineswhichsensortheinputwasreadfrom)thevalue
of
voltage
issettoeither
forwardVoltage
(voltagereadfromthefrontsensor)or
backwardVoltage
(voltagereadfromthebacksensor).Theforwardandbackward

voltagesarethencomparedandbasedonwhicheverisgreatertheprogrameithercalls
the
accelerateMotor()
or
decelerateMotor()
functiontochangethespeedof
themotor.

Theusart_send(char)functionwasusedfordebuggingpurposeswhenattemptingto
readinputfromthetwosensorsateverystageoftheprojectsdevelopment.Thegroup
usedthePuTTYterminalandoutputtedeitheranAforaccelerate,Dfordecelerate,or
Nforneutraltohelpdeterminewhatthesensorswerereadingandwhattheoutputto
3
themotorshouldbe.Theusart_sendfunctionwastakenfromtheUSARTtutorial

unedited.

Figure11.

Thewhileloopinthemainpollingforsensorinput

OtherNotesontheCCode:
Boththe
usart_init()
andthe
usart_send()
functionsweretakenstraightfrom
theUSARTTutorialfordebuggingpurposesandnotusedinthefinalcode.Because
theusart_sendfunctionsendstheoutputthroughtheUSBport,nocodeeditswere
neededforpinstobechanged.BasedonthecodethevalueisalsooutputtedtoPD1,
butbecausethepinwasnotusedatanyotherpointinthecode,itdidnotaffectthefinal
project.

SystemIntegration
SKATEBOARDDESIGN
Theskateboardwasshapedandbuiltfrom3/2pinewood.Aonewheelrearchain
geareddrivetrainwasmainlychosenforitseccentricreasons.TheFSRsaremounted
ontopoftheboardbelowarectangularcutoff.Therectangularcutouthasstandoffs
mountedunderneathinordertoensurethetotalloadoftheriderisplacedontheFSRs,
seeFigure12and13.

Theremaininghardwarewasmountedbelowtheskateboardtoprovideacleanerfinish
andallowtheridertostepfreelyonthetopoftheboard,seeFigure14.

Figure12.TopViewoftheskateboardwithrectangularloadplate.


Figure13.FSRmountingunderneathrectangularloadplate.

Figure14.Viewfrombelowshowingallthehardware(exceptFSRs).

PROGRAMDESIGN
Thecodingportionoftheprojectwasbrokenupintotwopieces:readingtheinputfrom
thesensorandthenoutputtingtheappropriatevoltagetothemotor.Theteambegan
bysimplyusingasingleFlexiForceA201PressureSensorasaninputandputtingthe
2
voltagereadinaPuTTY.Aftercheckingthepressuresensorsdatasheet
,thegroup
foundthattherewasnoinitializationsequencenecessarytoreadtheinputfromthe
sensor.Thus,alltheteamhadtodowasreadthevoltageinputfromthesensorusing

3
ADC.
Todoso,firsttheteamusedtheADCtutorial
suppliedinclassasabaseand
4
thencombinedthattutorialwiththeUARTtutorial.Oneissuetheteamfacedwasthat
theyforgottoconnect5VtotheAREFpinontheATMega,andspentnumeroushours

attemptingtodebugboththehardwareandthecodebecausethesensorwasnot
readinganyinput.AfterdiscoveringtheissuebyspeakingwithDr.Oliver,theteamwas
abletoreaddatafromthesensorwiththecodecreatedfromthecombinationofthe
ADCandUSARTtutorials.

Thenextportionofthecodewasoutputtingasignaltothemotor.Becausethe
ATMega328pcanonlyoutput5V,theteamdiscoveredthatamotordriverwasneeded
inordertosendanadequatesignaltothemotor.Theteamdecidedthatthebestdriver
tousewastheCytron10Amotorcontrollerboard10ADCmotordriver
.
Tocontrolthe
speedofthethemotortheteamrealizedthatitwasnecessarytousepulsewidth
modulation(PWM)afterreadingaboutthebenefitsandusesofPWMinthePWM
4
tutorial
.Thegroupthenphasedinportionsofthetutorialtothesensorinputcodeand
testedtheoutputwithanLED.AfterverifyingthecodeworkedwiththeLED,thegroup
thenconnectedtheoutputtothemotordriver,whichwasalsoconnectedtothemotor
itself.Afterridingtheskateboardtotestoutthetiming,thegroupdeterminedthatusing
the8bitTIMER0wassufficientanditwasnotnecessarytodividetheclockfurther
usingthe16bitTIMER1.Infact,theteamrealizedthattheacceleration(atleastwhen
acceleratingtheboardfrom0)wasabitslowandthusdifficultfortheridertostay
balanced,sotheteameditedthecodetoaccelerateatafasterrateforthelower
OCR0Avalues.

Conclusion
Thisproject,althoughchallengingattimes,taughttheteamabouthowanalogtodigital
conversioncanbeusedtoreadinputvaluesfromanysensorandfurtherreinforcedthe
advantagesofpulsewidthmodulationwhenitcomestocontrollingtheamountofpower
deliveredtoaloadsuchasamotor.
Thelargestsetbacktheteamfacedwasthedaybeforetheprojectwasduethemotor
driverfriedduetothebatteryusedtopowerthemotoroutputtingtoohighofavoltage
forthedrivertohandle(thedrivercouldsupportupto

25V,andthebatterysupplied
27V).Thegroupdidnothavesuitablepowerprotection/voltageregulationbetweenthat
andthedriver,thusinhindsightthedriverfryingshouldnothavecomeasasurprise.
However,becausetheteamhavebeentestingtheskateboardupuntilthatpointwith
goodresults,theydecidedtoorderanotherdriverandrecordavideodemoatalater
datewiththenewreplacementdriver.Luckily,intheendthegroupdideventuallyget
theskateboardfunctionalagainandavideodemowasrecordedandsubmittedtoDr.
Oliver.

Appendices


CCode:
/*
*Project3FInalProject:CalPolySkateboard
*
*Thiscodereadsinsensorinputfromtwopressuresensorsusing
*ADCandthenoutputsasignaltoamotorusingPWM.
*
*Authors:EthanArchibaldandChristinaSardo
*/

#defineBAUD_PRESCALE103
#defineF_CPU 16000000

#include<stdlib.h>
//StandardClibrary
#include<avr/io.h>
#include<util/delay.h>

//prototypes
voidInitialize_ADC0(void)
voidSwap_ACDC(void)
voidusart_init(uint16_tbaudin,uint32_tclk_speedin)
voidusart_send(uint8_tdata)
voidinitPWM()
voidaccelerateMotor(void)
voiddecelerateMotor(void)

//globalvariables
uint8_tswap

intmain(void)
{
uint8_tforwardVoltage=0,backwardVoltage=0
uint16_tvoltage=0
swap=0
DDRB=1<<5//SetPB5output

Initialize_ADC0()//InitializeADC0formanualtrigger
usart_init(9600,16000000)
//initializeusart
initPWM()
OCR0B=0

while(1)
{
ADCSRA=0xC7//startconversion

_delay_us(260) //ensuremaxsamplingratenotexceeded

voltage=ADC&0x3FF//readvoltage

//pollingforinputvoltage

if(swap==0)
forwardVoltage=voltage
else
backwardVoltage=voltage

if(forwardVoltage>backwardVoltage){//accelerate
usart_send('A')
accelerateMotor()
}
elseif(backwardVoltage>forwardVoltage){//decelerate
usart_send('D')
decelerateMotor()
}
else{//Elsethepressuresareequal,holdsamespeed
usart_send('N')
}

Swap_ACDC()

_delay_ms(50)
}
return0
}

voidInitialize_ADC0(void)
{

ADCSRA=0x87 //TurnOnADCandsetprescaler(CLK/128)

ADCSRB=0x00 //turnoffautotrigger

ADMUX=0x00//SetADCchannelADC0

voidSwap_ACDC(void)
{
if(swap==1){
ADMUX=0x00//SetADCchannelADC0
DIDR0=0x01
swap=0
}
else{
ADMUX=0x01//SetADCchannelADC1
DIDR0=0x02
swap=1
}
}

voidusart_init(uint16_tbaudin,uint32_tclk_speedin)
{

uint32_tubrr=(clk_speedin/16UL)/baudin1

UBRR0H=(unsignedchar)(ubrr>>8)

UBRR0L=(unsignedchar)ubrr

/*UBRR0H=(BAUD_PRESCALE>>8)

UBRR0L=BAUD_PRESCALE*/

/*Enablereceiverandtransmitter*/

UCSR0B=(1<<RXEN0)|(1<<TXEN0)

/*Setframeformat:8data,1stopbit*/

UCSR0C=(0<<USBS0)|(3<<UCSZ00)

UCSR0A&=~(1<<U2X0)

/*thesendfunctionwillput8bitsonthetransline.*/

voidusart_send(uint8_tdata)
{

/*Waitforemptytransmitbuffer*/

while(!(UCSR0A&(1<<UDRE0)))

/*Putdataintobuffer,sendsthedata*/

UDR0=data

voidinitPWM()//initializesmotors
{
TCCR0A=0b10100011
//timersettofastpwm
TCCR0B=2
//timerclk=systemclk/8
//outputs16E6/64/255=980HzPWM
OCR0A=0
//comparevalue=>20%dutycycletoPD5
}

voidaccelerateMotor(void){
if(OCR0A+1>255){
OCR0A=255
}
else{
if(OCR0A<100)
OCR0A+=10
else
OCR0A+=1
}
}

voiddecelerateMotor(void){
if(OCR0A5<0){
OCR0A=0
}
else{
OCR0A=5
}
}

USERMANUAL
ThefollowingisasuccinctusermanualonhowtooperatetheCalPoly
Skateboard(CPSwhatcanafreeridedoforyou).
DISCLAIMER
:The
writersofthismanualareinnowayresponsibleforthemisuseofthis
transportationsystem,includingmishandlingand/orfallingfromtheskateboard
itself.Anexpertineachrespectiveindividualcomponentshouldbeconsulted
beforeuse.

ToRide:
Inserttheexternalpowerjackconnectedtothe9VbatteryintotheATmega328p
microcontroller.Thenstandupontheboard.

Toaccelerate/decelerate:
Whilestandingontheboard,shiftyourweightontoyourfrontfoottoaccelerate
orshiftyourweighttoyourbackfoottodecelerate.

Iftheskateboarddoesnotmoveafterattemptingtheabove,followthegiven
procedure:
1stChecktoseeifthe9Vbatteryisstillmaintainingcharge.Thisisdone
byattachingamultimeterandmeasuringifthebatteryisstilloutputtingnear9V.
Ifnot,replacethebatterywithanequivalentbatteryandthenreattempttoride
andacceleratetheskateboard.
2ndIfafterverifyingthepropervoltageforthe9Vexternal
microcontrollerbatteryandiftheskateboardstilldoesnotmove,checktosee
whetherthe24Vbatteryischarged.
Warning
:Donotallowthepositiveand
negativewiresofthe24Vbatterytoconnectandmakecontactwitheachother.
Disconnectthe24Vbatterypackfromthemotorcontrollerscrewterminalsand
measurethevoltage.Chargethebatterypackwithasmartchargerthatiscapable
ofcharginga24VNiMHbatterypackwith20cells.Reattempttorideand
actuatetheskateboard.
3rdIftheskateboardstilldoesnotmove,sendyourskateboardtoa
certifiedCPSrepairshopforafreerepairunderwarranty*.

References:
[1]
ATmega328pDatasheet
[2]
FlexiForceA201Datasheet
[2]
ADCTutorial
[3]
USARTTutorial
[4]
PWMTutorial

Вам также может понравиться