V8
/
FRA221_Life_Style_of_Motor
-
main.cpp@2:2d18320c10b7, 2015-12-08 (annotated)
- Committer:
- bi18rdbi18rd
- Date:
- Tue Dec 08 18:49:35 2015 +0000
- Revision:
- 2:2d18320c10b7
- Parent:
- 1:9caed9f7669d
- Child:
- 3:c25094a31616
update bug serial;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:2d18320c10b7 | 5 | |
bi18rdbi18rd | 2:2d18320c10b7 | 6 | Serial pc(USBTX, USBRX); // set for degugging |
bi18rdbi18rd | 1:9caed9f7669d | 7 | |
bi18rdbi18rd | 1:9caed9f7669d | 8 | Serial bt(PA_9, PA_10); //Bluetooth : Tx Rx |
bi18rdbi18rd | 1:9caed9f7669d | 9 | Ticker tickSend; //Bluetooth : send data every 200 ms and Update RPM |
bi18rdbi18rd | 1:9caed9f7669d | 10 | InterruptIn encoder(PB_3); //Encoder : Set interrupt |
bi18rdbi18rd | 1:9caed9f7669d | 11 | PwmOut Motor(PB_5); //Motor : command motor |
bi18rdbi18rd | 2:2d18320c10b7 | 12 | InterruptIn button(PC_4); //Motor Drive : User button to set running state |
bi18rdbi18rd | 0:f4846f86e36d | 13 | AnalogIn currentSS(PA_0); //CurrentSensor : |
bi18rdbi18rd | 1:9caed9f7669d | 14 | SPI mcp3202(SPI_MOSI, SPI_MISO, SPI_SCK); //Voltage Sensor ADC MCP3202 |
bi18rdbi18rd | 2:2d18320c10b7 | 15 | DigitalOut cs3202a(PB_6); //chip select for mcp3202 |
bi18rdbi18rd | 2:2d18320c10b7 | 16 | DigitalOut cs3202b(PC_7); // chip select for mcp3202 nember 2 |
bi18rdbi18rd | 2:2d18320c10b7 | 17 | DigitalIn SwLCD(PB_13); //Switch LCD Display |
bi18rdbi18rd | 1:9caed9f7669d | 18 | |
bi18rdbi18rd | 1:9caed9f7669d | 19 | I2C i2c_lcd(I2C_SDA,I2C_SCL); // SDA, SCL |
bi18rdbi18rd | 1:9caed9f7669d | 20 | TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type |
bi18rdbi18rd | 1:9caed9f7669d | 21 | |
bi18rdbi18rd | 0:f4846f86e36d | 22 | void getRPM(); |
bi18rdbi18rd | 1:9caed9f7669d | 23 | void countEncoder(); |
bi18rdbi18rd | 0:f4846f86e36d | 24 | float getVolt(); |
bi18rdbi18rd | 0:f4846f86e36d | 25 | float getCurr(); |
bi18rdbi18rd | 0:f4846f86e36d | 26 | float Display(); |
bi18rdbi18rd | 0:f4846f86e36d | 27 | void switchRunning(); |
bi18rdbi18rd | 0:f4846f86e36d | 28 | void dataIn(); |
bi18rdbi18rd | 1:9caed9f7669d | 29 | void updateData(); |
bi18rdbi18rd | 1:9caed9f7669d | 30 | |
bi18rdbi18rd | 2:2d18320c10b7 | 31 | float maxVolt=0,maxCurr=0,maxPower=0,volt=0,curr=0,powerMotor =255; |
bi18rdbi18rd | 1:9caed9f7669d | 32 | uint8_t countEn=0; |
bi18rdbi18rd | 2:2d18320c10b7 | 33 | uint16_t rpm=0,maxRPM=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 | 0:f4846f86e36d | 50 | |
bi18rdbi18rd | 0:f4846f86e36d | 51 | while(1) { |
bi18rdbi18rd | 2:2d18320c10b7 | 52 | |
bi18rdbi18rd | 0:f4846f86e36d | 53 | if(runState) { //running loop |
bi18rdbi18rd | 1:9caed9f7669d | 54 | Motor = powerMotor/100.0f; |
bi18rdbi18rd | 0:f4846f86e36d | 55 | |
bi18rdbi18rd | 2:2d18320c10b7 | 56 | |
bi18rdbi18rd | 0:f4846f86e36d | 57 | } else { |
bi18rdbi18rd | 1:9caed9f7669d | 58 | Motor = 0.0f; |
bi18rdbi18rd | 0:f4846f86e36d | 59 | } |
bi18rdbi18rd | 2:2d18320c10b7 | 60 | |
bi18rdbi18rd | 2:2d18320c10b7 | 61 | if(runState) { |
bi18rdbi18rd | 2:2d18320c10b7 | 62 | curr=getCurr(); |
bi18rdbi18rd | 2:2d18320c10b7 | 63 | volt=getVolt(); |
bi18rdbi18rd | 2:2d18320c10b7 | 64 | |
bi18rdbi18rd | 2:2d18320c10b7 | 65 | if(volt > maxVolt) |
bi18rdbi18rd | 2:2d18320c10b7 | 66 | maxVolt = volt; |
bi18rdbi18rd | 2:2d18320c10b7 | 67 | if(curr > maxCurr) |
bi18rdbi18rd | 2:2d18320c10b7 | 68 | maxCurr = curr; |
bi18rdbi18rd | 2:2d18320c10b7 | 69 | if(curr*volt > maxPower) |
bi18rdbi18rd | 2:2d18320c10b7 | 70 | maxPower = curr*volt; |
bi18rdbi18rd | 2:2d18320c10b7 | 71 | } |
bi18rdbi18rd | 0:f4846f86e36d | 72 | } |
bi18rdbi18rd | 0:f4846f86e36d | 73 | } |
bi18rdbi18rd | 0:f4846f86e36d | 74 | |
bi18rdbi18rd | 0:f4846f86e36d | 75 | void switchRunning() |
bi18rdbi18rd | 0:f4846f86e36d | 76 | { |
bi18rdbi18rd | 1:9caed9f7669d | 77 | runState = !runState; |
bi18rdbi18rd | 0:f4846f86e36d | 78 | } |
bi18rdbi18rd | 0:f4846f86e36d | 79 | |
bi18rdbi18rd | 0:f4846f86e36d | 80 | void dataIn() |
bi18rdbi18rd | 0:f4846f86e36d | 81 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 82 | char inTmp[4]; |
bi18rdbi18rd | 1:9caed9f7669d | 83 | uint8_t i=0; |
bi18rdbi18rd | 2:2d18320c10b7 | 84 | |
bi18rdbi18rd | 2:2d18320c10b7 | 85 | |
bi18rdbi18rd | 2:2d18320c10b7 | 86 | inTmp[0] = bt.getc(); |
bi18rdbi18rd | 2:2d18320c10b7 | 87 | inTmp[1]=bt.getc(); |
bi18rdbi18rd | 2:2d18320c10b7 | 88 | inTmp[2] = bt.getc(); |
bi18rdbi18rd | 2:2d18320c10b7 | 89 | inTmp[3]=bt.getc(); |
bi18rdbi18rd | 2:2d18320c10b7 | 90 | |
bi18rdbi18rd | 1:9caed9f7669d | 91 | pc.printf("%s\n",inTmp); |
bi18rdbi18rd | 2:2d18320c10b7 | 92 | |
bi18rdbi18rd | 2:2d18320c10b7 | 93 | //do { |
bi18rdbi18rd | 2:2d18320c10b7 | 94 | // inTmp[i++]=bt.getc(); |
bi18rdbi18rd | 2:2d18320c10b7 | 95 | // } while(inTmp[i-1]!='\n'); |
bi18rdbi18rd | 2:2d18320c10b7 | 96 | // pc.printf("%s\n",inTmp); |
bi18rdbi18rd | 2:2d18320c10b7 | 97 | // if(inTmp[0]=='m') { |
bi18rdbi18rd | 2:2d18320c10b7 | 98 | // powerMotor = atoi(inTmp+2); |
bi18rdbi18rd | 2:2d18320c10b7 | 99 | // } |
bi18rdbi18rd | 0:f4846f86e36d | 100 | } |
bi18rdbi18rd | 0:f4846f86e36d | 101 | |
bi18rdbi18rd | 1:9caed9f7669d | 102 | void updateData() |
bi18rdbi18rd | 0:f4846f86e36d | 103 | { |
bi18rdbi18rd | 1:9caed9f7669d | 104 | getRPM(); |
bi18rdbi18rd | 2:2d18320c10b7 | 105 | if(rpm>maxRPM) |
bi18rdbi18rd | 2:2d18320c10b7 | 106 | maxRPM=rpm; |
bi18rdbi18rd | 2:2d18320c10b7 | 107 | |
bi18rdbi18rd | 2:2d18320c10b7 | 108 | //LCD state 1 |
bi18rdbi18rd | 2:2d18320c10b7 | 109 | if(SwLCD) |
bi18rdbi18rd | 2:2d18320c10b7 | 110 | lcd.printf("RPM:%5d P:%1.2f Max:%5d :%1.2f\n",rpm,volt*curr,maxRPM,maxPower); |
bi18rdbi18rd | 2:2d18320c10b7 | 111 | //LCD state2 |
bi18rdbi18rd | 2:2d18320c10b7 | 112 | else |
bi18rdbi18rd | 2:2d18320c10b7 | 113 | lcd.printf("V:%1.2fV I:%4.0fmA Mx:%1.2fV :%4.0fmA\n",volt,curr*1000,maxVolt,maxCurr); |
bi18rdbi18rd | 2:2d18320c10b7 | 114 | |
bi18rdbi18rd | 2:2d18320c10b7 | 115 | if(!runState) { |
bi18rdbi18rd | 2:2d18320c10b7 | 116 | volt =0; |
bi18rdbi18rd | 2:2d18320c10b7 | 117 | curr =0; |
bi18rdbi18rd | 2:2d18320c10b7 | 118 | rpm =0; |
bi18rdbi18rd | 1:9caed9f7669d | 119 | } |
bi18rdbi18rd | 2:2d18320c10b7 | 120 | |
bi18rdbi18rd | 2:2d18320c10b7 | 121 | //pc.printf("RPM:%5d P:%1.2f V :%1.2fV I:%3.0fmA\n",rpm,volt*curr,volt,curr*1000); |
bi18rdbi18rd | 2:2d18320c10b7 | 122 | bt.printf("s %d\nv %.2f\ni %.2f\np %.2f\n",rpm,volt,curr*1000,volt*curr); |
bi18rdbi18rd | 2:2d18320c10b7 | 123 | |
bi18rdbi18rd | 0:f4846f86e36d | 124 | } |
bi18rdbi18rd | 0:f4846f86e36d | 125 | |
bi18rdbi18rd | 0:f4846f86e36d | 126 | void getRPM() |
bi18rdbi18rd | 0:f4846f86e36d | 127 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 128 | if(runState) |
bi18rdbi18rd | 2:2d18320c10b7 | 129 | rpm = (countEn/2)/(PeriodUpdate/60); |
bi18rdbi18rd | 1:9caed9f7669d | 130 | countEn=0; |
bi18rdbi18rd | 1:9caed9f7669d | 131 | } |
bi18rdbi18rd | 1:9caed9f7669d | 132 | |
bi18rdbi18rd | 1:9caed9f7669d | 133 | void countEncoder() |
bi18rdbi18rd | 1:9caed9f7669d | 134 | { |
bi18rdbi18rd | 1:9caed9f7669d | 135 | countEn++; |
bi18rdbi18rd | 0:f4846f86e36d | 136 | } |
bi18rdbi18rd | 0:f4846f86e36d | 137 | |
bi18rdbi18rd | 0:f4846f86e36d | 138 | float getVolt() |
bi18rdbi18rd | 0:f4846f86e36d | 139 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 140 | uint8_t spi_data[3]= {0}; |
bi18rdbi18rd | 1:9caed9f7669d | 141 | cs3202a=0; |
bi18rdbi18rd | 1:9caed9f7669d | 142 | spi_data[0] = mcp3202.write(0x01); |
bi18rdbi18rd | 1:9caed9f7669d | 143 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 144 | spi_data[1] = mcp3202.write(0x20); |
bi18rdbi18rd | 1:9caed9f7669d | 145 | spi_data[2] = mcp3202.write(0); |
bi18rdbi18rd | 1:9caed9f7669d | 146 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 147 | cs3202a=1; |
bi18rdbi18rd | 2:2d18320c10b7 | 148 | |
bi18rdbi18rd | 1:9caed9f7669d | 149 | return (float)(((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f; |
bi18rdbi18rd | 0:f4846f86e36d | 150 | } |
bi18rdbi18rd | 0:f4846f86e36d | 151 | |
bi18rdbi18rd | 2:2d18320c10b7 | 152 | float getCurr () //mesure current by voltage during R 1 ohm |
bi18rdbi18rd | 1:9caed9f7669d | 153 | { |
bi18rdbi18rd | 2:2d18320c10b7 | 154 | uint8_t spi_data[3]= {0}; |
bi18rdbi18rd | 1:9caed9f7669d | 155 | cs3202b=0; |
bi18rdbi18rd | 1:9caed9f7669d | 156 | spi_data[0] = mcp3202.write(0x01); |
bi18rdbi18rd | 1:9caed9f7669d | 157 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 158 | spi_data[1] = mcp3202.write(0x00); |
bi18rdbi18rd | 1:9caed9f7669d | 159 | spi_data[2] = mcp3202.write(0); |
bi18rdbi18rd | 1:9caed9f7669d | 160 | wait_us(1); |
bi18rdbi18rd | 1:9caed9f7669d | 161 | cs3202b=1; |
bi18rdbi18rd | 1:9caed9f7669d | 162 | return (float)((((spi_data[1]&0x0F)*256.0f)+spi_data[2])/819.0f); |
bi18rdbi18rd | 2:2d18320c10b7 | 163 | |
bi18rdbi18rd | 0:f4846f86e36d | 164 | } |
bi18rdbi18rd | 0:f4846f86e36d | 165 |