FRA221_2015
/
17A_Life_Style_of_Motor
Life Style Of Motor
Fork of FRA221_Life_Style_of_Motor by
main.cpp@3:c25094a31616, 2015-12-08 (annotated)
- Committer:
- bi18rdbi18rd
- Date:
- Tue Dec 08 23:08:17 2015 +0000
- Revision:
- 3:c25094a31616
- Parent:
- 2:2d18320c10b7
life style of motor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bi18rdbi18rd | 3:c25094a31616 | 1 | #define PeriodUpdate 100 |
bi18rdbi18rd | 0:f4846f86e36d | 2 | |
bi18rdbi18rd | 1:9caed9f7669d | 3 | #include "mbed.h" |
bi18rdbi18rd | 1:9caed9f7669d | 4 | #include "TextLCD.h" |
bi18rdbi18rd | 2:2d18320c10b7 | 5 | |
bi18rdbi18rd | 3:c25094a31616 | 6 | Serial pc(USBTX, USBRX); // set for degugging |
bi18rdbi18rd | 1:9caed9f7669d | 7 | |
bi18rdbi18rd | 3:c25094a31616 | 8 | Serial bt(PA_9, PA_10); //Bluetooth : Tx Rx |
bi18rdbi18rd | 3:c25094a31616 | 9 | InterruptIn encoder(PB_3); //Encoder : Set interrupt |
bi18rdbi18rd | 3:c25094a31616 | 10 | PwmOut Motor(PB_5); //Motor : command motor |
bi18rdbi18rd | 3:c25094a31616 | 11 | InterruptIn button(PC_4); //Motor Drive : User button to set running state |
bi18rdbi18rd | 3:c25094a31616 | 12 | AnalogIn currentSS(PA_0); //CurrentSensor : |
bi18rdbi18rd | 3:c25094a31616 | 13 | SPI mcp3202(SPI_MOSI, SPI_MISO, SPI_SCK); //Voltage Sensor ADC MCP3202 |
bi18rdbi18rd | 3:c25094a31616 | 14 | DigitalOut cs3202a(PB_6); //chip select for mcp3202 |
bi18rdbi18rd | 3:c25094a31616 | 15 | DigitalOut cs3202b(PC_7); // chip select for mcp3202 nember 2 |
bi18rdbi18rd | 3:c25094a31616 | 16 | DigitalIn SwLCD(PB_13); //Switch LCD Display |
bi18rdbi18rd | 3:c25094a31616 | 17 | I2C i2c_lcd(I2C_SDA,I2C_SCL); // SDA, SCL |
bi18rdbi18rd | 3:c25094a31616 | 18 | TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type |
bi18rdbi18rd | 1:9caed9f7669d | 19 | |
bi18rdbi18rd | 0:f4846f86e36d | 20 | void getRPM(); |
bi18rdbi18rd | 1:9caed9f7669d | 21 | void countEncoder(); |
bi18rdbi18rd | 0:f4846f86e36d | 22 | float getVolt(); |
bi18rdbi18rd | 0:f4846f86e36d | 23 | float getCurr(); |
bi18rdbi18rd | 0:f4846f86e36d | 24 | float Display(); |
bi18rdbi18rd | 0:f4846f86e36d | 25 | void switchRunning(); |
bi18rdbi18rd | 0:f4846f86e36d | 26 | void dataIn(); |
bi18rdbi18rd | 1:9caed9f7669d | 27 | void updateData(); |
bi18rdbi18rd | 1:9caed9f7669d | 28 | |
bi18rdbi18rd | 3:c25094a31616 | 29 | float maxVolt=0,maxCurr=0,maxPower=0,volt=0,curr=0,powerMotor =125; |
bi18rdbi18rd | 1:9caed9f7669d | 30 | uint8_t countEn=0; |
bi18rdbi18rd | 3:c25094a31616 | 31 | uint16_t rpm=0,maxRPM=0,loopCount=0; |
bi18rdbi18rd | 0:f4846f86e36d | 32 | bool runState=0; |
bi18rdbi18rd | 0:f4846f86e36d | 33 | |
bi18rdbi18rd | 0:f4846f86e36d | 34 | int main() |
bi18rdbi18rd | 0:f4846f86e36d | 35 | { |
bi18rdbi18rd | 0:f4846f86e36d | 36 | //Prepare |
bi18rdbi18rd | 0:f4846f86e36d | 37 | bt.baud(9600); |
bi18rdbi18rd | 1:9caed9f7669d | 38 | pc.baud(9600); |
bi18rdbi18rd | 0:f4846f86e36d | 39 | bt.attach(&dataIn); //Interupt when recieved data |
bi18rdbi18rd | 1:9caed9f7669d | 40 | encoder.rise(&countEncoder); //set encoder detect rise edge |
bi18rdbi18rd | 1:9caed9f7669d | 41 | button.rise(&switchRunning); //set user button (blue) to switch running mode |
bi18rdbi18rd | 1:9caed9f7669d | 42 | Motor.period(0.002f); //set pwm frequency to 500 Hz |
bi18rdbi18rd | 1:9caed9f7669d | 43 | mcp3202.frequency(1000000); //set SPI mcp3202 |
bi18rdbi18rd | 1:9caed9f7669d | 44 | mcp3202.format(8,0); //set SPI mcp3202 |
bi18rdbi18rd | 1:9caed9f7669d | 45 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
bi18rdbi18rd | 1:9caed9f7669d | 46 | lcd.cls(); |
bi18rdbi18rd | 0:f4846f86e36d | 47 | |
bi18rdbi18rd | 0:f4846f86e36d | 48 | while(1) { |
bi18rdbi18rd | 2:2d18320c10b7 | 49 | |
bi18rdbi18rd | 3:c25094a31616 | 50 | if(runState) { //running loop |
bi18rdbi18rd | 1:9caed9f7669d | 51 | Motor = powerMotor/100.0f; |
bi18rdbi18rd | 3:c25094a31616 | 52 | curr+=getCurr(); |
bi18rdbi18rd | 3:c25094a31616 | 53 | volt+=getVolt(); |
bi18rdbi18rd | 2:2d18320c10b7 | 54 | |
bi18rdbi18rd | 0:f4846f86e36d | 55 | } else { |
bi18rdbi18rd | 1:9caed9f7669d | 56 | Motor = 0.0f; |
bi18rdbi18rd | 0:f4846f86e36d | 57 | } |
bi18rdbi18rd | 2:2d18320c10b7 | 58 | |
bi18rdbi18rd | 3:c25094a31616 | 59 | if(loopCount>=PeriodUpdate) { |
bi18rdbi18rd | 3:c25094a31616 | 60 | |
bi18rdbi18rd | 3:c25094a31616 | 61 | curr/=loopCount; |
bi18rdbi18rd | 3:c25094a31616 | 62 | volt/=loopCount; |
bi18rdbi18rd | 3:c25094a31616 | 63 | |
bi18rdbi18rd | 3:c25094a31616 | 64 | getRPM(); |
bi18rdbi18rd | 3:c25094a31616 | 65 | if(rpm>maxRPM) |
bi18rdbi18rd | 3:c25094a31616 | 66 | maxRPM=rpm; |
bi18rdbi18rd | 2:2d18320c10b7 | 67 | |
bi18rdbi18rd | 2:2d18320c10b7 | 68 | if(volt > maxVolt) |
bi18rdbi18rd | 2:2d18320c10b7 | 69 | maxVolt = volt; |
bi18rdbi18rd | 2:2d18320c10b7 | 70 | if(curr > maxCurr) |
bi18rdbi18rd | 2:2d18320c10b7 | 71 | maxCurr = curr; |
bi18rdbi18rd | 2:2d18320c10b7 | 72 | if(curr*volt > maxPower) |
bi18rdbi18rd | 2:2d18320c10b7 | 73 | maxPower = curr*volt; |
bi18rdbi18rd | 3:c25094a31616 | 74 | |
bi18rdbi18rd | 3:c25094a31616 | 75 | updateData(); |
bi18rdbi18rd | 3:c25094a31616 | 76 | loopCount=0; |
bi18rdbi18rd | 2:2d18320c10b7 | 77 | } |
bi18rdbi18rd | 3:c25094a31616 | 78 | |
bi18rdbi18rd | 3:c25094a31616 | 79 | wait_ms(1); |
bi18rdbi18rd | 3:c25094a31616 | 80 | loopCount++; |
bi18rdbi18rd | 0:f4846f86e36d | 81 | } |
bi18rdbi18rd | 0:f4846f86e36d | 82 | } |
bi18rdbi18rd | 0:f4846f86e36d | 83 | |
bi18rdbi18rd | 0:f4846f86e36d | 84 | void switchRunning() |
bi18rdbi18rd | 0:f4846f86e36d | 85 | { |
bi18rdbi18rd | 1:9caed9f7669d | 86 | runState = !runState; |
bi18rdbi18rd | 0:f4846f86e36d | 87 | } |
bi18rdbi18rd | 0:f4846f86e36d | 88 | |
bi18rdbi18rd | 0:f4846f86e36d | 89 | void dataIn() |
bi18rdbi18rd | 0:f4846f86e36d | 90 | { |
bi18rdbi18rd | 3:c25094a31616 | 91 | char inTmp[5]= {0}; |
bi18rdbi18rd | 2:2d18320c10b7 | 92 | |
bi18rdbi18rd | 3:c25094a31616 | 93 | for(uint8_t i=0; i<5; i++) { |
bi18rdbi18rd | 3:c25094a31616 | 94 | inTmp[i] = bt.getc(); |
bi18rdbi18rd | 3:c25094a31616 | 95 | if(inTmp[i] == '\n') break; |
bi18rdbi18rd | 3:c25094a31616 | 96 | } |
bi18rdbi18rd | 2:2d18320c10b7 | 97 | |
bi18rdbi18rd | 3:c25094a31616 | 98 | //pc.printf("s %s\td%d \n",inTmp,atoi(inTmp+2)); |
bi18rdbi18rd | 3:c25094a31616 | 99 | powerMotor = atoi(inTmp+2); |
bi18rdbi18rd | 3:c25094a31616 | 100 | return ; |
bi18rdbi18rd | 0:f4846f86e36d | 101 | } |
bi18rdbi18rd | 0:f4846f86e36d | 102 | |
bi18rdbi18rd | 1:9caed9f7669d | 103 | void updateData() |
bi18rdbi18rd | 0:f4846f86e36d | 104 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 105 | //LCD state 1 |
bi18rdbi18rd | 2:2d18320c10b7 | 106 | if(SwLCD) |
bi18rdbi18rd | 3:c25094a31616 | 107 | lcd.printf("RPM:%5d P:%1.2fMax:%5d :%1.2f",rpm,volt*curr,maxRPM,maxPower); |
bi18rdbi18rd | 2:2d18320c10b7 | 108 | //LCD state2 |
bi18rdbi18rd | 2:2d18320c10b7 | 109 | else |
bi18rdbi18rd | 3:c25094a31616 | 110 | lcd.printf("V:%1.2fV I:%4.0fmAMx:%1.2fV :%4.0fmA",volt,curr*1000,maxVolt,maxCurr*1000); |
bi18rdbi18rd | 2:2d18320c10b7 | 111 | |
bi18rdbi18rd | 2:2d18320c10b7 | 112 | if(!runState) { |
bi18rdbi18rd | 2:2d18320c10b7 | 113 | volt =0; |
bi18rdbi18rd | 2:2d18320c10b7 | 114 | curr =0; |
bi18rdbi18rd | 2:2d18320c10b7 | 115 | rpm =0; |
bi18rdbi18rd | 1:9caed9f7669d | 116 | } |
bi18rdbi18rd | 2:2d18320c10b7 | 117 | bt.printf("s %d\nv %.2f\ni %.2f\np %.2f\n",rpm,volt,curr*1000,volt*curr); |
bi18rdbi18rd | 0:f4846f86e36d | 118 | } |
bi18rdbi18rd | 0:f4846f86e36d | 119 | |
bi18rdbi18rd | 0:f4846f86e36d | 120 | void getRPM() |
bi18rdbi18rd | 0:f4846f86e36d | 121 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 122 | if(runState) |
bi18rdbi18rd | 3:c25094a31616 | 123 | rpm = (countEn/2.0f)/(PeriodUpdate/60000.0f); |
bi18rdbi18rd | 1:9caed9f7669d | 124 | countEn=0; |
bi18rdbi18rd | 1:9caed9f7669d | 125 | } |
bi18rdbi18rd | 1:9caed9f7669d | 126 | |
bi18rdbi18rd | 1:9caed9f7669d | 127 | void countEncoder() |
bi18rdbi18rd | 1:9caed9f7669d | 128 | { |
bi18rdbi18rd | 1:9caed9f7669d | 129 | countEn++; |
bi18rdbi18rd | 0:f4846f86e36d | 130 | } |
bi18rdbi18rd | 0:f4846f86e36d | 131 | |
bi18rdbi18rd | 0:f4846f86e36d | 132 | float getVolt() |
bi18rdbi18rd | 0:f4846f86e36d | 133 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 134 | uint8_t spi_data[3]= {0}; |
bi18rdbi18rd | 1:9caed9f7669d | 135 | cs3202a=0; |
bi18rdbi18rd | 1:9caed9f7669d | 136 | spi_data[0] = mcp3202.write(0x01); |
bi18rdbi18rd | 1:9caed9f7669d | 137 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 138 | spi_data[1] = mcp3202.write(0x20); |
bi18rdbi18rd | 1:9caed9f7669d | 139 | spi_data[2] = mcp3202.write(0); |
bi18rdbi18rd | 1:9caed9f7669d | 140 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 141 | cs3202a=1; |
bi18rdbi18rd | 2:2d18320c10b7 | 142 | |
bi18rdbi18rd | 1:9caed9f7669d | 143 | return (float)(((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f; |
bi18rdbi18rd | 0:f4846f86e36d | 144 | } |
bi18rdbi18rd | 0:f4846f86e36d | 145 | |
bi18rdbi18rd | 3:c25094a31616 | 146 | float getCurr () //mesure current by voltage during R 1 ohm |
bi18rdbi18rd | 1:9caed9f7669d | 147 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 148 | uint8_t spi_data[3]= {0}; |
bi18rdbi18rd | 1:9caed9f7669d | 149 | cs3202b=0; |
bi18rdbi18rd | 1:9caed9f7669d | 150 | spi_data[0] = mcp3202.write(0x01); |
bi18rdbi18rd | 1:9caed9f7669d | 151 | wait_us(1); |
bi18rdbi18rd | 3:c25094a31616 | 152 | spi_data[1] = mcp3202.write(0x20); |
bi18rdbi18rd | 1:9caed9f7669d | 153 | spi_data[2] = mcp3202.write(0); |
bi18rdbi18rd | 1:9caed9f7669d | 154 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 155 | cs3202b=1; |
bi18rdbi18rd | 3:c25094a31616 | 156 | |
bi18rdbi18rd | 1:9caed9f7669d | 157 | return (float)((((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f); |
bi18rdbi18rd | 2:2d18320c10b7 | 158 | |
bi18rdbi18rd | 0:f4846f86e36d | 159 | } |
bi18rdbi18rd | 0:f4846f86e36d | 160 |