FRA221_2015
/
17A_Life_Style_of_Motor
Life Style Of Motor
Fork of FRA221_Life_Style_of_Motor by
main.cpp
- Committer:
- bi18rdbi18rd
- Date:
- 2015-12-08
- Revision:
- 1:9caed9f7669d
- Parent:
- 0:f4846f86e36d
- Child:
- 2:2d18320c10b7
File content as of revision 1:9caed9f7669d:
#define PeriodUpdate 0.2 #include "mbed.h" #include "TextLCD.h" Serial pc(USBTX, USBRX); 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_13); //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 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 void getRPM(); void countEncoder(); float getVolt(); float getCurr(); float Display(); void switchRunning(); void dataIn(); void updateData(); float volt=0,curr=0,powerMotor =255; uint8_t countEn=0; uint16_t rpm=0; bool runState=0; int main() { //Prepare 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 mcp3202.frequency(1000000); //set SPI mcp3202 mcp3202.format(8,0); //set SPI mcp3202 lcd.setCursor(TextLCD::CurOff_BlkOff); lcd.cls(); lcd.printf("RPM:%5d P:%1.2f V :%1.2fV I:%3.0fmA\n",rpm,volt*curr,volt,curr*1000); while(1) { if(runState) { //running loop Motor = powerMotor/100.0f; } else { Motor = 0.0f; } curr=getCurr(); volt=getVolt(); } } void switchRunning() { runState = !runState; } void dataIn() { char inTmp[6]; uint8_t i=0; do { inTmp[i++]=bt.getc(); } while(inTmp[i-1]!='\n'); pc.printf("%s\n",inTmp); if(inTmp[0]=='m') { powerMotor = atoi(inTmp+2); } } void updateData() { getRPM(); if(runState) { lcd.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,volt*curr); printf("RPM:%5d P:%1.2f\t",rpm,volt*curr); printf("V :%1.2fV I:%2.0fmA\n",volt,curr*1000); } } void getRPM() { rpm = (150*countEn); countEn=0; } void countEncoder() { countEn++; } float getVolt() { uint8_t spi_data[3]={0}; cs3202a=0; spi_data[0] = mcp3202.write(0x01); wait_us(1); spi_data[1] = mcp3202.write(0x20); spi_data[2] = mcp3202.write(0); wait_us(1); cs3202a=1; return (float)(((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f; } 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[2] = mcp3202.write(0); wait_us(1); cs3202b=1; return (float)((((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f); }