Life Style Of Motor

Dependencies:   17A mbed

Fork of FRA221_Life_Style_of_Motor by V8

Committer:
bi18rdbi18rd
Date:
Tue Dec 08 14:14:36 2015 +0000
Revision:
1:9caed9f7669d
Parent:
0:f4846f86e36d
Child:
2:2d18320c10b7
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bi18rdbi18rd 1:9caed9f7669d 1 #define PeriodUpdate 0.2
bi18rdbi18rd 0:f4846f86e36d 2
bi18rdbi18rd 1:9caed9f7669d 3 #include "mbed.h"
bi18rdbi18rd 1:9caed9f7669d 4 #include "TextLCD.h"
bi18rdbi18rd 1:9caed9f7669d 5 Serial pc(USBTX, USBRX);
bi18rdbi18rd 1:9caed9f7669d 6
bi18rdbi18rd 1:9caed9f7669d 7 Serial bt(PA_9, PA_10); //Bluetooth : Tx Rx
bi18rdbi18rd 1:9caed9f7669d 8 Ticker tickSend; //Bluetooth : send data every 200 ms and Update RPM
bi18rdbi18rd 1:9caed9f7669d 9 InterruptIn encoder(PB_3); //Encoder : Set interrupt
bi18rdbi18rd 1:9caed9f7669d 10 PwmOut Motor(PB_5); //Motor : command motor
bi18rdbi18rd 0:f4846f86e36d 11 InterruptIn button(PC_13); //Motor Drive : User button to set running state
bi18rdbi18rd 0:f4846f86e36d 12 AnalogIn currentSS(PA_0); //CurrentSensor :
bi18rdbi18rd 1:9caed9f7669d 13 SPI mcp3202(SPI_MOSI, SPI_MISO, SPI_SCK); //Voltage Sensor ADC MCP3202
bi18rdbi18rd 1:9caed9f7669d 14 DigitalOut cs3202a(PB_6); //chip select for mcp3202
bi18rdbi18rd 1:9caed9f7669d 15 DigitalOut cs3202b(PC_7); // chip select for mcp3202 nember 2
bi18rdbi18rd 1:9caed9f7669d 16
bi18rdbi18rd 1:9caed9f7669d 17 I2C i2c_lcd(I2C_SDA,I2C_SCL); // SDA, SCL
bi18rdbi18rd 1:9caed9f7669d 18 TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type
bi18rdbi18rd 1:9caed9f7669d 19
bi18rdbi18rd 0:f4846f86e36d 20
bi18rdbi18rd 0:f4846f86e36d 21 void getRPM();
bi18rdbi18rd 1:9caed9f7669d 22 void countEncoder();
bi18rdbi18rd 0:f4846f86e36d 23 float getVolt();
bi18rdbi18rd 0:f4846f86e36d 24 float getCurr();
bi18rdbi18rd 0:f4846f86e36d 25 float Display();
bi18rdbi18rd 0:f4846f86e36d 26 void switchRunning();
bi18rdbi18rd 0:f4846f86e36d 27 void dataIn();
bi18rdbi18rd 1:9caed9f7669d 28 void updateData();
bi18rdbi18rd 1:9caed9f7669d 29
bi18rdbi18rd 0:f4846f86e36d 30
bi18rdbi18rd 1:9caed9f7669d 31 float volt=0,curr=0,powerMotor =255;
bi18rdbi18rd 1:9caed9f7669d 32 uint8_t countEn=0;
bi18rdbi18rd 1:9caed9f7669d 33 uint16_t rpm=0;
bi18rdbi18rd 0:f4846f86e36d 34 bool runState=0;
bi18rdbi18rd 0:f4846f86e36d 35
bi18rdbi18rd 0:f4846f86e36d 36 int main()
bi18rdbi18rd 0:f4846f86e36d 37 {
bi18rdbi18rd 0:f4846f86e36d 38 //Prepare
bi18rdbi18rd 0:f4846f86e36d 39 bt.baud(9600);
bi18rdbi18rd 1:9caed9f7669d 40 pc.baud(9600);
bi18rdbi18rd 0:f4846f86e36d 41 bt.attach(&dataIn); //Interupt when recieved data
bi18rdbi18rd 1:9caed9f7669d 42 tickSend.attach(&updateData,PeriodUpdate); //Send data every 200 ms
bi18rdbi18rd 1:9caed9f7669d 43 encoder.rise(&countEncoder); //set encoder detect rise edge
bi18rdbi18rd 1:9caed9f7669d 44 button.rise(&switchRunning); //set user button (blue) to switch running mode
bi18rdbi18rd 1:9caed9f7669d 45 Motor.period(0.002f); //set pwm frequency to 500 Hz
bi18rdbi18rd 1:9caed9f7669d 46 mcp3202.frequency(1000000); //set SPI mcp3202
bi18rdbi18rd 1:9caed9f7669d 47 mcp3202.format(8,0); //set SPI mcp3202
bi18rdbi18rd 1:9caed9f7669d 48 lcd.setCursor(TextLCD::CurOff_BlkOff);
bi18rdbi18rd 1:9caed9f7669d 49 lcd.cls();
bi18rdbi18rd 1:9caed9f7669d 50 lcd.printf("RPM:%5d P:%1.2f V :%1.2fV I:%3.0fmA\n",rpm,volt*curr,volt,curr*1000);
bi18rdbi18rd 0:f4846f86e36d 51
bi18rdbi18rd 0:f4846f86e36d 52 while(1) {
bi18rdbi18rd 0:f4846f86e36d 53 if(runState) { //running loop
bi18rdbi18rd 1:9caed9f7669d 54 Motor = powerMotor/100.0f;
bi18rdbi18rd 0:f4846f86e36d 55
bi18rdbi18rd 0:f4846f86e36d 56 } else {
bi18rdbi18rd 1:9caed9f7669d 57 Motor = 0.0f;
bi18rdbi18rd 0:f4846f86e36d 58 }
bi18rdbi18rd 1:9caed9f7669d 59 curr=getCurr();
bi18rdbi18rd 1:9caed9f7669d 60 volt=getVolt();
bi18rdbi18rd 0:f4846f86e36d 61 }
bi18rdbi18rd 0:f4846f86e36d 62 }
bi18rdbi18rd 0:f4846f86e36d 63
bi18rdbi18rd 0:f4846f86e36d 64 void switchRunning()
bi18rdbi18rd 0:f4846f86e36d 65 {
bi18rdbi18rd 1:9caed9f7669d 66 runState = !runState;
bi18rdbi18rd 0:f4846f86e36d 67 }
bi18rdbi18rd 0:f4846f86e36d 68
bi18rdbi18rd 0:f4846f86e36d 69 void dataIn()
bi18rdbi18rd 0:f4846f86e36d 70 {
bi18rdbi18rd 1:9caed9f7669d 71 char inTmp[6];
bi18rdbi18rd 1:9caed9f7669d 72 uint8_t i=0;
bi18rdbi18rd 1:9caed9f7669d 73 do {
bi18rdbi18rd 1:9caed9f7669d 74 inTmp[i++]=bt.getc();
bi18rdbi18rd 1:9caed9f7669d 75 } while(inTmp[i-1]!='\n');
bi18rdbi18rd 1:9caed9f7669d 76 pc.printf("%s\n",inTmp);
bi18rdbi18rd 0:f4846f86e36d 77 if(inTmp[0]=='m') {
bi18rdbi18rd 0:f4846f86e36d 78 powerMotor = atoi(inTmp+2);
bi18rdbi18rd 0:f4846f86e36d 79 }
bi18rdbi18rd 0:f4846f86e36d 80 }
bi18rdbi18rd 0:f4846f86e36d 81
bi18rdbi18rd 1:9caed9f7669d 82 void updateData()
bi18rdbi18rd 0:f4846f86e36d 83 {
bi18rdbi18rd 1:9caed9f7669d 84 getRPM();
bi18rdbi18rd 1:9caed9f7669d 85 if(runState)
bi18rdbi18rd 1:9caed9f7669d 86 {
bi18rdbi18rd 1:9caed9f7669d 87 lcd.printf("RPM:%5d P:%1.2f V :%1.2fV I:%3.0fmA\n",rpm,volt*curr,volt,curr*1000);
bi18rdbi18rd 1:9caed9f7669d 88 bt.printf("s %d\nv %.2f\ni %.2f\np %.2f\n",rpm,volt,curr,volt*curr);
bi18rdbi18rd 1:9caed9f7669d 89 printf("RPM:%5d P:%1.2f\t",rpm,volt*curr);
bi18rdbi18rd 1:9caed9f7669d 90 printf("V :%1.2fV I:%2.0fmA\n",volt,curr*1000);
bi18rdbi18rd 1:9caed9f7669d 91 }
bi18rdbi18rd 0:f4846f86e36d 92 }
bi18rdbi18rd 0:f4846f86e36d 93
bi18rdbi18rd 0:f4846f86e36d 94 void getRPM()
bi18rdbi18rd 0:f4846f86e36d 95 {
bi18rdbi18rd 1:9caed9f7669d 96 rpm = (150*countEn);
bi18rdbi18rd 1:9caed9f7669d 97 countEn=0;
bi18rdbi18rd 1:9caed9f7669d 98 }
bi18rdbi18rd 1:9caed9f7669d 99
bi18rdbi18rd 1:9caed9f7669d 100 void countEncoder()
bi18rdbi18rd 1:9caed9f7669d 101 {
bi18rdbi18rd 1:9caed9f7669d 102 countEn++;
bi18rdbi18rd 0:f4846f86e36d 103 }
bi18rdbi18rd 0:f4846f86e36d 104
bi18rdbi18rd 0:f4846f86e36d 105 float getVolt()
bi18rdbi18rd 0:f4846f86e36d 106 {
bi18rdbi18rd 1:9caed9f7669d 107 uint8_t spi_data[3]={0};
bi18rdbi18rd 1:9caed9f7669d 108 cs3202a=0;
bi18rdbi18rd 1:9caed9f7669d 109 spi_data[0] = mcp3202.write(0x01);
bi18rdbi18rd 1:9caed9f7669d 110 wait_us(1);
bi18rdbi18rd 1:9caed9f7669d 111 spi_data[1] = mcp3202.write(0x20);
bi18rdbi18rd 1:9caed9f7669d 112 spi_data[2] = mcp3202.write(0);
bi18rdbi18rd 1:9caed9f7669d 113 wait_us(1);
bi18rdbi18rd 1:9caed9f7669d 114 cs3202a=1;
bi18rdbi18rd 1:9caed9f7669d 115
bi18rdbi18rd 1:9caed9f7669d 116 return (float)(((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f;
bi18rdbi18rd 0:f4846f86e36d 117 }
bi18rdbi18rd 0:f4846f86e36d 118
bi18rdbi18rd 1:9caed9f7669d 119 float getCurr () //mesure current by voltage during R 1 ohm
bi18rdbi18rd 1:9caed9f7669d 120 {
bi18rdbi18rd 1:9caed9f7669d 121 uint8_t spi_data[3]={0};
bi18rdbi18rd 1:9caed9f7669d 122 cs3202b=0;
bi18rdbi18rd 1:9caed9f7669d 123 spi_data[0] = mcp3202.write(0x01);
bi18rdbi18rd 1:9caed9f7669d 124 wait_us(1);
bi18rdbi18rd 1:9caed9f7669d 125 spi_data[1] = mcp3202.write(0x00);
bi18rdbi18rd 1:9caed9f7669d 126 spi_data[2] = mcp3202.write(0);
bi18rdbi18rd 1:9caed9f7669d 127 wait_us(1);
bi18rdbi18rd 1:9caed9f7669d 128 cs3202b=1;
bi18rdbi18rd 1:9caed9f7669d 129 return (float)((((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f);
bi18rdbi18rd 0:f4846f86e36d 130
bi18rdbi18rd 0:f4846f86e36d 131 }
bi18rdbi18rd 0:f4846f86e36d 132