Projekt code für Hfecter
Dependencies: PM2_Libary PM2_Example_PES_board
Diff: main.cpp
- Revision:
- 13:35f55464048e
- Parent:
- 12:b87a1e165fb8
- Child:
- 14:cfe1311a63f0
--- a/main.cpp Mon Apr 19 09:37:44 2021 +0000 +++ b/main.cpp Thu May 06 11:52:53 2021 +0000 @@ -22,29 +22,32 @@ /* create analog input object */ AnalogIn adc_vbat(ADC_VBAT); -AnalogIn analogIn(PC_2); -float dist = 0.0f; +DigitalIn digitalIn(D5); +AnalogIn analogIn(PA_1); + +float movement = 0.0f; float batt = 0.0f; /* create enable dc motor digital out object */ DigitalOut enable_motors(PB_15); /* create pwm objects */ -FastPWM pwmOut_M1(PB_13); +//FastPWM pwmOut_M1(PB_13); FastPWM pwmOut_M2(PA_9); -FastPWM pwmOut_M3(PA_10); +//FastPWM pwmOut_M3(PA_10); double Ts_pwm_s = 0.00005; // this needs to be a double value (no f at the end) /* create encoder read objects */ -EncoderCounter encoderCounter_M1(PA_6, PC_7); +//EncoderCounter encoderCounter_M1(PA_6, PC_7); EncoderCounter encoderCounter_M2(PB_6, PB_7); -EncoderCounter encoderCounter_M3(PA_0, PA_1); +//EncoderCounter encoderCounter_M3(PA_0, PA_1); /* create speed controller objects, only M1 and M2, M3 is used open-loop */ float counts_per_turn = 20.0f*78.125f; // counts/turn * gearratio float kn = 180.0f/12.0f; // (RPM/V) float max_voltage = 12.0f; // adjust this to 6.0f if only one batterypack is used -SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwmOut_M1, encoderCounter_M1); +//SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwmOut_M1, encoderCounter_M1); SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwmOut_M2, encoderCounter_M2); - +/* analog output */ + AnalogOut BatteryLED (PA_5); int main() @@ -56,58 +59,71 @@ /* enable hardwaredriver dc motors */ enable_motors = 1; /* initialize pwm for motor M3*/ - pwmOut_M3.period(Ts_pwm_s); + //pwmOut_M3.period(Ts_pwm_s); /* set pwm output zero at the beginning, range: 0...1 -> u_min...u_max */ - pwmOut_M3.write(0.5); + //pwmOut_M3.write(0.5); - +float movementold = 0; while (true) { batt = adc_vbat.read()* (3.3f/AUFLOESUNG); loop_timer.reset(); /* ------------- start hacking ------------- -------------*/ - if (executeMainTask) { + +movement = roundf(analogIn.read()*1.2); +// if ( movement > 1) movement = 1; +// else movement = 0; /* read analog input */ - dist = analogIn.read() * 3.3f; - + BatteryLED = 1; + //ceil() + if (movementold > movement)//WIP + { /* command a speed to dc motors M1 and M2*/ - speedController_M1.setDesiredSpeedRPS( 2.0f); - // speedController_M2.setDesiredSpeedRPS(-0.5f); + //speedController_M1.setDesiredSpeedRPS( 2.0f); + speedController_M2.setDesiredSpeedRPS(2.0f); + enable_motors = 1; + + thread_sleep_for(3000);// int Wert in ms /* write output voltage to motor M3 */ - pwmOut_M3.write(0.75); - + //pwmOut_M3.write(1.0); + } /* visual feedback that the main task is executed */ - led = !led; + - } else { + else { + + //movement = 0.0f; - dist = 0.0f; - - speedController_M1.setDesiredSpeedRPS(0.0f); - // speedController_M2.setDesiredSpeedRPS(0.0f); - pwmOut_M3.write(0.5); + // speedController_M1.setDesiredSpeedRPS(0.0f); + // speedController_M2.setDesiredSpeedRPS(0.0f); + // BatteryLED = 0; + enable_motors = 0; + // pwmOut_M3.write(-1); led = 0; } + led = !led; /* do only output via serial what's really necessary (this makes your code slow)*/ - printf("%3.3f, %3.3f\n, %3d, %3.3f, %3.3f;\r\n", - dist, + printf("%3.3f\n, %3.3f\n, %3.3f;\r\n", + movement, batt, - encoderCounter_M3.read(), - speedController_M1.getSpeedRPS(), + //encoderCounter_M3.read(), + //speedController_M1.getSpeedRPS(), speedController_M2.getSpeedRPS()); /* ------------- stop hacking ------------- -------------*/ - int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); - int dT_loop_ms = Ts_ms - T_loop_ms; - thread_sleep_for(dT_loop_ms); + //int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); + //int dT_loop_ms = Ts_ms - T_loop_ms; + //thread_sleep_for(dT_loop_ms); + thread_sleep_for(10); + movementold = movement; } }