Life Style Of Motor

Dependencies:   17A mbed

Fork of FRA221_Life_Style_of_Motor by V8

Files at this revision

API Documentation at this revision

Comitter:
bi18rdbi18rd
Date:
Tue Dec 08 23:08:17 2015 +0000
Parent:
2:2d18320c10b7
Commit message:
life style of motor

Changed in this revision

TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2d18320c10b7 -r c25094a31616 TextLCD.lib
--- a/TextLCD.lib	Tue Dec 08 18:49:35 2015 +0000
+++ b/TextLCD.lib	Tue Dec 08 23:08:17 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/wim/code/TextLCD/#8552753db5d4
+https://developer.mbed.org/teams/FRA221_2015/code/17A/#8552753db5d4
diff -r 2d18320c10b7 -r c25094a31616 main.cpp
--- a/main.cpp	Tue Dec 08 18:49:35 2015 +0000
+++ b/main.cpp	Tue Dec 08 23:08:17 2015 +0000
@@ -1,23 +1,21 @@
-#define PeriodUpdate 0.2
+#define PeriodUpdate 100
 
 #include "mbed.h"
 #include "TextLCD.h"
 
-Serial pc(USBTX, USBRX);    // set for degugging
+Serial pc(USBTX, USBRX);                            // set for degugging
 
-Serial bt(PA_9, PA_10);     //Bluetooth : Tx Rx
-Ticker tickSend;            //Bluetooth : send data every 200 ms and Update RPM
-InterruptIn encoder(PB_3);  //Encoder : Set interrupt
-PwmOut Motor(PB_5);         //Motor   : command motor
-InterruptIn button(PC_4);   //Motor Drive : User button to set running state
-AnalogIn currentSS(PA_0);   //CurrentSensor :
-SPI mcp3202(SPI_MOSI, SPI_MISO, SPI_SCK);   //Voltage Sensor ADC MCP3202
-DigitalOut cs3202a(PB_6);   //chip select for mcp3202
-DigitalOut cs3202b(PC_7);   // chip select for mcp3202 nember 2
-DigitalIn SwLCD(PB_13);     //Switch LCD Display
-
-I2C i2c_lcd(I2C_SDA,I2C_SCL); // SDA, SCL
-TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type
+Serial bt(PA_9, PA_10);                             //Bluetooth : Tx Rx
+InterruptIn encoder(PB_3);                          //Encoder : Set interrupt
+PwmOut Motor(PB_5);                                 //Motor   : command motor
+InterruptIn button(PC_4);                           //Motor Drive : User button to set running state
+AnalogIn currentSS(PA_0);                           //CurrentSensor :
+SPI mcp3202(SPI_MOSI, SPI_MISO, SPI_SCK);           //Voltage Sensor ADC MCP3202
+DigitalOut cs3202a(PB_6);                           //chip select for mcp3202
+DigitalOut cs3202b(PC_7);                           // chip select for mcp3202 nember 2
+DigitalIn SwLCD(PB_13);                             //Switch LCD Display
+I2C i2c_lcd(I2C_SDA,I2C_SCL);                       // SDA, SCL
+TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2);  // I2C bus, PCF8574 addr, LCD Type, Ctrl Type
 
 void getRPM();
 void countEncoder();
@@ -28,9 +26,9 @@
 void dataIn();
 void updateData();
 
-float maxVolt=0,maxCurr=0,maxPower=0,volt=0,curr=0,powerMotor =255;
+float maxVolt=0,maxCurr=0,maxPower=0,volt=0,curr=0,powerMotor =125;
 uint8_t countEn=0;
-uint16_t rpm=0,maxRPM=0;
+uint16_t rpm=0,maxRPM=0,loopCount=0;
 bool runState=0;
 
 int main()
@@ -39,7 +37,6 @@
     bt.baud(9600);
     pc.baud(9600);
     bt.attach(&dataIn);                 //Interupt when recieved data
-    tickSend.attach(&updateData,PeriodUpdate);   //Send data every 200 ms
     encoder.rise(&countEncoder);        //set encoder detect rise edge
     button.rise(&switchRunning);        //set user button (blue) to switch running mode
     Motor.period(0.002f);               //set pwm frequency to 500 Hz
@@ -50,17 +47,23 @@
 
     while(1) {
 
-        if(runState) { //running loop
+        if(runState) {  //running loop
             Motor = powerMotor/100.0f;
-
+            curr+=getCurr();
+            volt+=getVolt();
 
         } else {
             Motor = 0.0f;
         }
 
-        if(runState) {
-            curr=getCurr();
-            volt=getVolt();
+        if(loopCount>=PeriodUpdate) {
+            
+            curr/=loopCount;
+            volt/=loopCount;
+            
+            getRPM();
+            if(rpm>maxRPM)
+                maxRPM=rpm;
 
             if(volt > maxVolt)
                 maxVolt = volt;
@@ -68,7 +71,13 @@
                 maxCurr = curr;
             if(curr*volt > maxPower)
                 maxPower = curr*volt;
+
+            updateData();
+            loopCount=0;
         }
+
+        wait_ms(1);
+        loopCount++;
     }
 }
 
@@ -79,54 +88,39 @@
 
 void dataIn()
 {
-    char inTmp[4];
-    uint8_t i=0;
+    char inTmp[5]= {0};
 
-    
-        inTmp[0] = bt.getc();
-        inTmp[1]=bt.getc();
-        inTmp[2] = bt.getc();
-        inTmp[3]=bt.getc();
-   
-    pc.printf("%s\n",inTmp);
+    for(uint8_t i=0; i<5; i++) {
+        inTmp[i] = bt.getc();
+        if(inTmp[i] == '\n') break;
+    }
 
-    //do {
-//        inTmp[i++]=bt.getc();
-//    } while(inTmp[i-1]!='\n');
-//    pc.printf("%s\n",inTmp);
-//    if(inTmp[0]=='m') {
-//        powerMotor = atoi(inTmp+2);
-//    }
+    //pc.printf("s %s\td%d \n",inTmp,atoi(inTmp+2));
+    powerMotor = atoi(inTmp+2);
+    return ;
 }
 
 void updateData()
 {
-    getRPM();
-    if(rpm>maxRPM)
-        maxRPM=rpm;
-
     //LCD state 1
     if(SwLCD)
-        lcd.printf("RPM:%5d P:%1.2f    Max:%5d  :%1.2f\n",rpm,volt*curr,maxRPM,maxPower);
+        lcd.printf("RPM:%5d P:%1.2fMax:%5d  :%1.2f",rpm,volt*curr,maxRPM,maxPower);
     //LCD state2
     else
-        lcd.printf("V:%1.2fV I:%4.0fmA    Mx:%1.2fV :%4.0fmA\n",volt,curr*1000,maxVolt,maxCurr);
+        lcd.printf("V:%1.2fV I:%4.0fmAMx:%1.2fV :%4.0fmA",volt,curr*1000,maxVolt,maxCurr*1000);
 
     if(!runState) {
         volt =0;
         curr =0;
         rpm =0;
     }
-
-    //pc.printf("RPM:%5d P:%1.2f    V :%1.2fV I:%3.0fmA\n",rpm,volt*curr,volt,curr*1000);
     bt.printf("s %d\nv %.2f\ni %.2f\np %.2f\n",rpm,volt,curr*1000,volt*curr);
-
 }
 
 void getRPM()
 {
     if(runState)
-        rpm = (countEn/2)/(PeriodUpdate/60);
+        rpm = (countEn/2.0f)/(PeriodUpdate/60000.0f);
     countEn=0;
 }
 
@@ -149,16 +143,17 @@
     return (float)(((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f;
 }
 
-float getCurr ()        //mesure current by voltage during R 1 ohm
+float getCurr ()            //mesure current by voltage during R 1 ohm
 {
     uint8_t spi_data[3]= {0};
     cs3202b=0;
     spi_data[0] = mcp3202.write(0x01);
     wait_us(1);
-    spi_data[1] = mcp3202.write(0x00);
+    spi_data[1] = mcp3202.write(0x20);
     spi_data[2] = mcp3202.write(0);
     wait_us(1);
     cs3202b=1;
+
     return (float)((((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f);
 
 }