HU YANG
/
AnalogOut-HelloWorld
aa
Fork of AnalogOut-HelloWorld by
Revision 5:e6536d2d5aba, committed 2016-10-10
- Comitter:
- fewerhy
- Date:
- Mon Oct 10 20:40:05 2016 +0000
- Parent:
- 4:351236622c35
- Commit message:
- stable
Changed in this revision
diff -r 351236622c35 -r e6536d2d5aba main.cpp --- a/main.cpp Fri Mar 27 18:30:13 2015 +0000 +++ b/main.cpp Mon Oct 10 20:40:05 2016 +0000 @@ -1,36 +1,10 @@ -/* mbed Example Program - * Copyright (c) 2006-2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbed.h" - -// Initialize a pins to perform analog and digital output fucntions -AnalogOut aout(p18); -DigitalOut dout(LED1); - -int main(void) -{ - while (1) { - // change the voltage on the digital output pin by 0.1 * VCC - // and print what the measured voltage should be (assuming VCC = 3.3v) - for (float i = 0.0f; i < 1.0f; i += 0.1f) { - aout = i; - printf("aout = %1.2f volts\n", aout.read() * 3.3f); - // turn on the led if the voltage is greater than 0.5f * VCC - dout = (aout > 0.5f) ? 1 : 0; - wait(1.0f); - } +#include "mdeviceCtrl.h" + +int main() { + printf("Press 'g' to turn LED1 brightness up, 'd' to turn it down\n"); + mdeviceCtrl *myApp = new mdeviceCtrl(); + while(1) + { + myApp->run(); } }
diff -r 351236622c35 -r e6536d2d5aba mdevice.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mdevice.cpp Mon Oct 10 20:40:05 2016 +0000 @@ -0,0 +1,144 @@ +#include "mdevice.h" + +mdevice::mdevice() +{ + mSpeedPort0.period(0.00002857142f); + mSpeedPort1.period(0.00002857142f); + mIndexSig0=true; + mIndexSig1=true; + mPos0=0; + mPos1=0; + mDir0=1; + mDir1=1; + mButton=0; + mPreButton=0; + mButtonEdge=0; +} + +void mdevice::updateButton() +{ + if (buttonPort>0.3f) + { + mButton=1; + } + else + { + mButton=0; + } + + //detect up edge + if (mPreButton==0&& mButton==1) + { + mButtonEdge=1; + } + //detect down edge + else if (mPreButton==1&& mButton==0) + { + mButtonEdge=-1; + } + else + { + mButtonEdge=0; + } + mPreButton=mButton; +} + +void mdevice::runMotor0(float s) +{ + if (s>0.001) + { + mDir0=1; + mDirPort0=1; + mSpeedPort0.write(s); + } + else if (s<-0.001) + { + mDir0=-1; + mDirPort0=0; + mSpeedPort0.write(-s); + } + else + { + mSpeedPort0.write(0.0f); + } +} + +void mdevice::runMotor1(float s) +{ + if (s>0.001) + { + mDir1=1; + mDirPort1=1; + mSpeedPort1.write(s); + } + else if (s<-0.001) + { + mDir1=-1; + mDirPort1=0; + mSpeedPort1.write(-s); + } + else + { + mSpeedPort1.write(0.0f); + } +} + +void mdevice::updateEncoder0() +{ + if (encoderPort0<0.3f ) + { + if (mIndexSig0==true) + { + mIndexSig0=false; + mPos0=mPos0+mDir0; + } + } + else + { + mIndexSig0=true; + } +} + +void mdevice::updateEncoder1() +{ + if (encoderPort1<0.3f ) + { + if (mIndexSig1==true) + { + mIndexSig1=false; + mPos1=mPos1+mDir1; + } + } + else + { + mIndexSig1=true; + } +} + +void mdevice::updateDeviceStatus() +{ + updateEncoder0(); + updateEncoder1(); + updateButton(); +} + +int mdevice::getButton() +{ + return mButton; +} + +int mdevice::getMotorPos0() +{ + return mPos0; +} + +int mdevice::getMotorPos1() +{ + return mPos1; +} + +int mdevice::getButtonEdge() +{ + return mButtonEdge; +} +
diff -r 351236622c35 -r e6536d2d5aba mdevice.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mdevice.h Mon Oct 10 20:40:05 2016 +0000 @@ -0,0 +1,41 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include "mbed.h" + +static AnalogIn buttonPort(p18); +static AnalogIn encoderPort0(p20); +static AnalogIn encoderPort1(p19); +static DigitalOut mDirPort0(p30); +static DigitalOut mDirPort1(p29); +static PwmOut mSpeedPort0(p21); +static PwmOut mSpeedPort1(p22); + +class mdevice +{ +public: + mdevice (); + void updateDeviceStatus(); + int getButton(); + int getMotorPos0(); + int getMotorPos1(); + void runMotor0(float s); + void runMotor1(float s); + int getButtonEdge(); +private: + bool mIndexSig0; + bool mIndexSig1; + int mPos0; + int mPos1; + int mDir0; + int mDir1; + int mButton; + int mPreButton; + int mButtonEdge; + void updateButton(); + void updateEncoder0(); + void updateEncoder1(); + +}; + +#endif \ No newline at end of file
diff -r 351236622c35 -r e6536d2d5aba mdeviceCtrl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mdeviceCtrl.cpp Mon Oct 10 20:40:05 2016 +0000 @@ -0,0 +1,87 @@ +#include "mdeviceCtrl.h" + +mdeviceCtrl::mdeviceCtrl() +{ + myDevice= new mdevice(); + needleStatus=0; +} + +void mdeviceCtrl::run() +{ + myDevice->updateDeviceStatus(); + int tempB=myDevice->getButtonEdge(); + + int mp0=myDevice->getMotorPos0(); + int mp1=myDevice->getMotorPos1(); + + cout<< mp0 <<" "<< mp1<<endl; + if (tempB==1) + { + myDevice->runMotor0(0.2); + myDevice->runMotor1(-0.02); + } + else if (tempB==-1) + { + myDevice->runMotor0(0.0); + myDevice->runMotor1(0.0); + } +} + +bool mdeviceCtrl::inserNeedle( ) +{ + myDevice->runMotor0(1.0); + int tempPos=myDevice->getMotorPos0(); + if (tempPos>200) + { + return true; + } + else + { + return false; + } +} + +bool mdeviceCtrl::retractNeedle( ) +{ + myDevice->runMotor0(-1.0); + int tempPos=myDevice->getMotorPos0(); + if ( tempPos<10) + { + return true; + } + else + { + return false; + } +} + +bool mdeviceCtrl::switchNeedle1() +{ + needleStatus++; + if (needleStatus%2==0) + { + myDevice->runMotor1(-0.2); + int tempPos=myDevice->getMotorPos1(); + if (tempPos>1) + { + return true; + } + else + { + return false; + } + } + else + { + myDevice->runMotor1(0.2); + int tempPos=myDevice->getMotorPos1(); + if (tempPos<0) + { + return true; + } + else + { + return false; + } + } +}
diff -r 351236622c35 -r e6536d2d5aba mdeviceCtrl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mdeviceCtrl.h Mon Oct 10 20:40:05 2016 +0000 @@ -0,0 +1,20 @@ +#ifndef DEVICECTRL_H +#define DEVICECTRL_H +#include <iostream> +#include "mdevice.h" +using namespace std; + +class mdeviceCtrl +{ +public: + mdeviceCtrl(); + void run(); +private: + bool switchNeedle1(); + bool retractNeedle(); + bool inserNeedle(); + mdevice *myDevice; + int needleStatus; +}; + +#endif \ No newline at end of file