Life Style Of Motor

Dependencies:   17A mbed

Fork of FRA221_Life_Style_of_Motor by V8

Committer:
bi18rdbi18rd
Date:
Sat Dec 05 18:23:34 2015 +0000
Revision:
0:f4846f86e36d
Child:
1:9caed9f7669d
draft structure

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bi18rdbi18rd 0:f4846f86e36d 1 #include "mbed.h"
bi18rdbi18rd 0:f4846f86e36d 2
bi18rdbi18rd 0:f4846f86e36d 3 Serial bt(PA_2, PA_3); //Bluetooth : Tx Rx
bi18rdbi18rd 0:f4846f86e36d 4 Ticker tickSend; //Bluetooth : send data every 200 ms
bi18rdbi18rd 0:f4846f86e36d 5 Timer t; //Encoder : set timer
bi18rdbi18rd 0:f4846f86e36d 6 InterruptIn encoder(PA_10); //Encoder : Set interupt
bi18rdbi18rd 0:f4846f86e36d 7 PwmOut Motor(PA_10); //Motor : command motor
bi18rdbi18rd 0:f4846f86e36d 8 InterruptIn button(PC_13); //Motor Drive : User button to set running state
bi18rdbi18rd 0:f4846f86e36d 9 AnalogIn currentSS(PA_0); //CurrentSensor :
bi18rdbi18rd 0:f4846f86e36d 10
bi18rdbi18rd 0:f4846f86e36d 11 void getRPM();
bi18rdbi18rd 0:f4846f86e36d 12 float getVolt();
bi18rdbi18rd 0:f4846f86e36d 13 float getCurr();
bi18rdbi18rd 0:f4846f86e36d 14 float Display();
bi18rdbi18rd 0:f4846f86e36d 15 void switchRunning();
bi18rdbi18rd 0:f4846f86e36d 16 void dataIn();
bi18rdbi18rd 0:f4846f86e36d 17 void sendData();
bi18rdbi18rd 0:f4846f86e36d 18
bi18rdbi18rd 0:f4846f86e36d 19 float rpm=0,volt=0,curr=0,powerMotor =0;
bi18rdbi18rd 0:f4846f86e36d 20 bool runState=0;
bi18rdbi18rd 0:f4846f86e36d 21
bi18rdbi18rd 0:f4846f86e36d 22 int main()
bi18rdbi18rd 0:f4846f86e36d 23 {
bi18rdbi18rd 0:f4846f86e36d 24 //Prepare
bi18rdbi18rd 0:f4846f86e36d 25 bt.baud(9600);
bi18rdbi18rd 0:f4846f86e36d 26 bt.attach(&dataIn); //Interupt when recieved data
bi18rdbi18rd 0:f4846f86e36d 27 tickSend.attach(&sendData,0.2); //Send data every 200 ms
bi18rdbi18rd 0:f4846f86e36d 28 encoder.fall(&getRPM); //set encoder detect rise edge
bi18rdbi18rd 0:f4846f86e36d 29 button.rise(&switchRunning); //set user button (blueX to switch running mode
bi18rdbi18rd 0:f4846f86e36d 30
bi18rdbi18rd 0:f4846f86e36d 31
bi18rdbi18rd 0:f4846f86e36d 32 while(1) {
bi18rdbi18rd 0:f4846f86e36d 33 if(runState) { //running loop
bi18rdbi18rd 0:f4846f86e36d 34 Motor = powerMotor/255;
bi18rdbi18rd 0:f4846f86e36d 35
bi18rdbi18rd 0:f4846f86e36d 36 } else {
bi18rdbi18rd 0:f4846f86e36d 37
bi18rdbi18rd 0:f4846f86e36d 38 }
bi18rdbi18rd 0:f4846f86e36d 39 }
bi18rdbi18rd 0:f4846f86e36d 40 }
bi18rdbi18rd 0:f4846f86e36d 41
bi18rdbi18rd 0:f4846f86e36d 42 void switchRunning()
bi18rdbi18rd 0:f4846f86e36d 43 {
bi18rdbi18rd 0:f4846f86e36d 44 runState = ~runState;
bi18rdbi18rd 0:f4846f86e36d 45 }
bi18rdbi18rd 0:f4846f86e36d 46
bi18rdbi18rd 0:f4846f86e36d 47 void dataIn()
bi18rdbi18rd 0:f4846f86e36d 48 {
bi18rdbi18rd 0:f4846f86e36d 49 char inTmp[5];
bi18rdbi18rd 0:f4846f86e36d 50 for(int i = 0; bt.readable() ; i++) {
bi18rdbi18rd 0:f4846f86e36d 51 inTmp[i]=bt.getc();
bi18rdbi18rd 0:f4846f86e36d 52 }
bi18rdbi18rd 0:f4846f86e36d 53 if(inTmp[0]=='m') {
bi18rdbi18rd 0:f4846f86e36d 54 powerMotor = atoi(inTmp+2);
bi18rdbi18rd 0:f4846f86e36d 55 }
bi18rdbi18rd 0:f4846f86e36d 56 }
bi18rdbi18rd 0:f4846f86e36d 57
bi18rdbi18rd 0:f4846f86e36d 58 void sendData()
bi18rdbi18rd 0:f4846f86e36d 59 {
bi18rdbi18rd 0:f4846f86e36d 60 bt.printf("s %d\nv %d\ni %d\np %d",(int)rpm,(int)volt,(int)curr,(int)(volt*curr));
bi18rdbi18rd 0:f4846f86e36d 61 }
bi18rdbi18rd 0:f4846f86e36d 62
bi18rdbi18rd 0:f4846f86e36d 63 void getRPM()
bi18rdbi18rd 0:f4846f86e36d 64 {
bi18rdbi18rd 0:f4846f86e36d 65 float time;
bi18rdbi18rd 0:f4846f86e36d 66 t.stop();
bi18rdbi18rd 0:f4846f86e36d 67 time=t.read();
bi18rdbi18rd 0:f4846f86e36d 68 rpm = (60/(time*90)); // 60second / (timeTakkenPerSlit * NumberOfSlit)
bi18rdbi18rd 0:f4846f86e36d 69 t.reset();
bi18rdbi18rd 0:f4846f86e36d 70 t.start();
bi18rdbi18rd 0:f4846f86e36d 71 }
bi18rdbi18rd 0:f4846f86e36d 72
bi18rdbi18rd 0:f4846f86e36d 73 float getVolt()
bi18rdbi18rd 0:f4846f86e36d 74 {
bi18rdbi18rd 0:f4846f86e36d 75 return 0;
bi18rdbi18rd 0:f4846f86e36d 76 }
bi18rdbi18rd 0:f4846f86e36d 77
bi18rdbi18rd 0:f4846f86e36d 78 float getCurr()
bi18rdbi18rd 0:f4846f86e36d 79 {
bi18rdbi18rd 0:f4846f86e36d 80 float curTmp=0;
bi18rdbi18rd 0:f4846f86e36d 81 for(int i=0; i<20 ;i++)
bi18rdbi18rd 0:f4846f86e36d 82 curTmp+=currentSS;
bi18rdbi18rd 0:f4846f86e36d 83
bi18rdbi18rd 0:f4846f86e36d 84 return curTmp-10;
bi18rdbi18rd 0:f4846f86e36d 85 }
bi18rdbi18rd 0:f4846f86e36d 86
bi18rdbi18rd 0:f4846f86e36d 87