Yves Waldspurger
/
PM2_Example_PES_board
test
Diff: main.cpp
- Revision:
- 11:ed1d7d9959f0
- Parent:
- 10:c5d85e35758c
--- a/main.cpp Wed Apr 07 12:13:50 2021 +0000 +++ b/main.cpp Mon Apr 12 08:47:06 2021 +0000 @@ -42,13 +42,15 @@ 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); + /* create servo objects */ Servo servo_S1(PB_2); Servo servo_S2(PC_8); -// Servo servo_S3(PC_6); // not needed in this example +Servo servo_S3(PC_6); // not needed in this example int servoPeriod_mus = 20000; int servoOutput_mus_S1 = 0; int servoOutput_mus_S2 = 0; +int servoOutput_mus_S3 = 0; int servo_counter = 0; int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)Ts_ms))); @@ -68,6 +70,7 @@ /* enable servos, you can also disable them */ servo_S1.Enable(servoOutput_mus_S1, servoPeriod_mus); servo_S2.Enable(servoOutput_mus_S2, servoPeriod_mus); + servo_S3.Enable(servoOutput_mus_S3, servoPeriod_mus); while (true) { @@ -81,7 +84,7 @@ dist = analogIn.read() * 3.3f; /* command a speed to dc motors M1 and M2*/ - speedController_M1.setDesiredSpeedRPS( 1.0f); + speedController_M1.setDesiredSpeedRPS( -0.1f); speedController_M2.setDesiredSpeedRPS(-0.5f); /* write output voltage to motor M3 */ pwmOut_M3.write(0.75); @@ -89,12 +92,16 @@ /* command servo position via output time, this needs to be calibrated */ servo_S1.SetPosition(servoOutput_mus_S1); servo_S2.SetPosition(servoOutput_mus_S2); + servo_S3.SetPosition(servoOutput_mus_S3); if (servoOutput_mus_S1 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { servoOutput_mus_S1 += 100; } if (servoOutput_mus_S2 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { servoOutput_mus_S2 += 100; } + if (servoOutput_mus_S3 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { + servoOutput_mus_S3 += 100; + } servo_counter++; /* visual feedback that the main task is executed */ @@ -110,8 +117,10 @@ servoOutput_mus_S1 = 0; servoOutput_mus_S2 = 0; + servoOutput_mus_S3 = 0; servo_S1.SetPosition(servoOutput_mus_S1); servo_S2.SetPosition(servoOutput_mus_S2); + servo_S3.SetPosition(servoOutput_mus_S3); led = 0; } @@ -121,6 +130,7 @@ dist, servoOutput_mus_S1, servoOutput_mus_S2, + servoOutput_mus_S3, encoderCounter_M3.read(), speedController_M1.getSpeedRPS(), speedController_M2.getSpeedRPS());