Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Legacy auto manual replacement #9368

Closed
wants to merge 253 commits into from

Conversation

Stifael
Copy link
Contributor

@Stifael Stifael commented Apr 25, 2018

This Pr is the follow up of #9253.
Eventually this PR will remove the entire legacy code of the multicopter position control.

@Stifael Stifael mentioned this pull request Apr 25, 2018
1 task
@Stifael Stifael force-pushed the Legacy-Auto-Manual-replacement branch from a3ff249 to 1bf9803 Compare April 25, 2018 09:23
@LorenzMeier
Copy link
Member

@RomanBapst Could you please review? Given the current flash limitations, I think we will make a full switch.

@Stifael Stifael force-pushed the Legacy-Auto-Manual-replacement branch 4 times, most recently from 64c4e35 to b261530 Compare May 1, 2018 17:09
@MaEtUgR MaEtUgR self-requested a review May 2, 2018 11:40
@Stifael Stifael force-pushed the Legacy-Auto-Manual-replacement branch from 27c5079 to d04b1ae Compare May 4, 2018 05:31
@Stifael
Copy link
Contributor Author

Stifael commented May 4, 2018

About this PR:
This PR separates the legacy mc_pos_control_main into three sections:

  • FlightTask
  • mc_pos_control_main
  • PositionControl

FlightTask

The details about FlightTask were already covered in other PRs and discussions (for instance here).
To keep it short: FlightTask is a framework library that is vehicle INDEPENDENT for generating kinematic SETPOINTS (position-, velocity-, acceleration- (not supported yet), thrust-, yaw- and yawspeed-setpoints).
The FlightTaks library can be found here.

Flight-mode specific functionality were moved from mc_pos_control_main to a new task within FlightTask. The tasks are the following:

  • FlightTaskManualStabilized replaces the manual attitude mode
  • FlightTaskManualAltitude replaces the manual altitude mode without smoothing
  • FlightTaskManualAltitudeSmooth replaces the manual altitude mode with smoothing
  • FlightTaskManualPosition replaces the manual position control mode without smoothing
  • FlightTaskManualPositionSmooth replaces the manual position control mode with smoothing
  • FlightTaskAutoLine replaces the auto logic
  • FlightTaskAutoFollowMe replaces the follow-me logic
  • FlightTaskOffboard replaces the offboard logic

All tasks should be identical to the legacy code except with some changes to make it better fit into the FlightTask-framework:

  • Terrain-following:
    • If terrain-following is enabled, instead of overwriting the velocity/position estimate the velocity/position setpoints are varied to keep the desired distance to ground.
  • Follow-me:
    • the logic here was removed because in the legacy code there is no support for velocity and position setpoints simultaneously. However, in the code section above it sets velocity setpoints without setting the boolean flag run_pos_control to false and therefore these velocity setpoints will never be used.
  • speed limit downwards/upwards close to home altitude: In the legacy code, the speed in vertical direction during ANY mode is adjusted based on the relative altitude to the home-altitude. This functionality is still missing for manual controlled mode.
  • offboard lock: in the legacy code if the velocity setpoint is below a certain threshold, a position lock is applied. However, this is really a hack and nothing more. If the user wants to lock position, then a position setpoint can be sent.

PositionControl

This class contains the CORE-position/velocity controllers. The inputs are the position-, velocity-, acceleration- (not supported), thrust-, yaw- and yawspeed setpoints and outputs a thrust vector and yaw-/yawspeed-septoint.

mc_pos_control_main

This module is basically the interface between FlightTask and PositionControl. It makes adjustments to FlightTask setpoints based on vehicle state. For instance, it enables a smooth-takeoff, it keeps throttle low when being landed, it adjusts the thrust setpoint based on the landdetector state and so on. It also converts the thrust and yaw setpoint from PositionControl to a desired attitude and publishes it.

Flight-tests

So far I have tested the following modes:

  • SITL terrain following
  • SITL follow-me
  • SITL offboard position setpoints
  • SITL/HITL and OUTDOOR stabilized-, altitude- (without smoothing), position- (without smoothing), auto- mode.
  • SITL altitude and position mode with smoothing
  • SITL flow

It would be great if we can start to collect more flight hours for all different modes. When testing position and altitude mode, please keep in mind that NO smoothing is active. This is on purpose to make it easier to track down any unexpected bug. As I mentioned above, in any manual controlled mode the speed downwards/upwards close to ground is only limited by the global limits MPC_Z_VEL_MAX_DN/UP.

Conventions

I started to use a different style method for parameters and comments. It is not consistent yet and If I am the only one who sees the benefit in it, then I will revert it of course.

comments

the doxygen style that I started to use is the one already present in most modules:

  • for methods
/**
 *  Short description.
 *  Long desrciption
 *  @param
 *  @return
 */
  • for member variables

/**< bla bla */

  • comments in the code:

// bla bla
// bla bla

parameter naming

Instead of creating a name for a parameter, I just reused the meta-data parameter name. For instance, the parameter within a module that uses the MPC_XY_CRUISE parameter would be called MPC_XY_CRUISE as well. In my opinion, this has the following benefits:

  • no need to invent a new name
  • a parameter will have the same name in any module that uses that parameter
  • a clear distinction between a class member parameter (which usually should not be modified) and a class member variable that changes regularly (such as a setpoint)
  • from the prefix it is clear which module is the owner of the parameter
  • it is simpler to look up the full description (https://dev.px4.io/en/advanced/parameter_reference.html)
  • less prone to errors during coding
  • saves time during coding and during reading the code

Independent of this PR, I think it could be highly beneficial if we have more style related guidelines which are not covered by astyle. I can open a new issue to discuss possible code guidelines if desired.

Currently this branch fails because of:
../../src/systemcmds/tests/test_controlmath.cpp:106:1: internal compiler error: in trunc_int_for_mode, at explow.c:55

I saw that this error has popped up in the past already (https://github.com/PX4/Firmware/blob/master/src/systemcmds/tests/test_matrix.cpp#L628-L629).

@Stifael
Copy link
Contributor Author

Stifael commented May 4, 2018

the ci fails for one of the unit-tests. In the worst case I can just remove the unit test to make the ci pass

@dagar
Copy link
Member

dagar commented May 4, 2018

the ci fails for one of the unit-tests. In the worst case I can just remove the unit test to make the ci pass

It's a compiler issue, if you drill down into ControlMathTest::testPrioritizeVector() it's likely only 1 or 2 offending lines.

@Stifael
Copy link
Contributor Author

Stifael commented May 4, 2018

it's likely only 1 or 2 offending lines

I found now the line that caused the compilation problem.

// until he toggles the switch to avoid retracting the gear immediately on takeoff.
int8_t gear_switch = _sub_manual_control_setpoint->get().gear_switch;

if (!_constraints.landing_gear) {
Copy link
Member

@MaEtUgR MaEtUgR May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think _evaluateSticks() should only get the gear switch state and all the logic executing on it should be in a different function, here _applyGearSwitch().


} else {
// altitude based on locale coordinate system
_dist_to_ground_lock = NAN; // reset boolean since not used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a boolean...

@MaEtUgR
Copy link
Member

MaEtUgR commented May 16, 2018

Altitude mode takeoff and altitude hold near the floor is sadly broken. I had the following cases tested with make posix_sitl_default gazebo_iris_opt_flow now:

  • Doesn't take off at all in altitude mode
  • Does takeoff immediately after arming to the minimum altitude for a usable range sensor even if the sticks didn't command anything yet
  • Everything works

I'm trying to find out what the root cause is but it needs to have something to do with _respectMinAltitude() introduced in 51ea83d#diff-5eabb1783ad4b7c01186be612b61631bR86 and how the vehicle is lock idle on the ground as long as there's no takeoff commanded.

EDIT:
I think I found the problem for the automatic jump without stick input:

Still doesn't explain why somethimes I cannot take off with my SITL. I'll try to reproduce with some conditions hardcoded like a unit test...

EDIT2:
I found the reason why it doesn't take off at all:
The thrust setpoint vector which gets to the evaluation here: https://github.com/PX4/Firmware/pull/9368/files#diff-d241541147a774870439717a5b9145e4R107 still contains NANs for x and y in the optical flow and altitude mode case.

EDIT3:
It doesn't take off because:
The position controller runs through velocity and position control at any time and if the states are not available/valid they get set to NAN here: https://github.com/PX4/Firmware/pull/9368/files#diff-fc77c3ef569029d45764664c75d4b0c1R389 and this NAN propagates through the controller to the output of x and y and the thrust length() calculation here https://github.com/PX4/Firmware/pull/9368/files#diff-d241541147a774870439717a5b9145e4R107 is NAN and no thurst gets executed.

I didn't have a valid local position with flow because my computer ran the simulation too slow and the data didn't pass the checks but the same problem applies if you loose your last position estimate source which is usually GPS. -> @Stifael That's a problem.


float32 yawspeed # in radians/sec
float32 speed_xy # in meters/sec
float32 speed_up # in meters/sec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the setpoint which gets constrained is called vz so why not call the constraint constraint.vz_up?


matrix::Dcmf _R_setpoint;
hrt_abstime _last_warn = 0; /**< timer when the last warn message was sent out */
static constexpr uint64_t IDLE_BOFORE_TAKEOFF_TIME_US =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we wait for a hardcoded 2.5 seconds until the position controller realizes that the vehicle was armed?

_vel_sp_prev(0) = _vel(0);
_vel_sp_prev(1) = _vel(1);
// Fill attitude setpoint. Attitude is computed from yaw and thrust setpoint.
_att_sp = ControlMath::thrustToAttitude(thr_sp, _control.getYawSetpoint());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thrustToAttitude() holds its own copy of the complete attitude setpoint message. That overhead can be saved by reseting the outside attitude setpoint message and passing a reference to the function such that it can fill the fields.

matrix::Eulerf euler = R_sp;
att_sp.roll_body = euler(0);
att_sp.pitch_body = euler(1);

/* fill and publish att_sp message */
// fill and publish att_sp message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's actually not published here...

@Stifael
Copy link
Contributor Author

Stifael commented May 22, 2018

@MaEtUgR thanks for the testing and comments.
For future collaboration, I think it is most efficient if contributors are pushing directly onto this branch for smaller issues/fixes and open a PR for more involved issues.
I will now address your comments. In addition, I will add code to protect against invalid states that can occur due to commander-position-control race condition.

@Stifael Stifael force-pushed the Legacy-Auto-Manual-replacement branch 2 times, most recently from 91205bb to 7952da6 Compare May 22, 2018 16:32
@dagar
Copy link
Member

dagar commented May 25, 2018

Rebase?

@Stifael Stifael force-pushed the Legacy-Auto-Manual-replacement branch from d9e1e79 to 45d5767 Compare June 5, 2018 08:54
@Stifael
Copy link
Contributor Author

Stifael commented Jun 5, 2018

@santiago3dr @Avysuarez thanks again for testing.
The Stabilized takeoff should now be fixed. If there is still an issue, please can you try to the same with Manual takeoff. Although Manual should be the same as Stabilized, it is still worth to test.

Would it also be possible to test position control with flow enabled?

@Avysuarez
Copy link

couple flights with the new commit .
pixhawk 4 (V5).
https://review.px4.io/plot_app?log=25a00713-f2c9-4f85-88f5-143d586d93d8
took-off in stabilized mode and the throttle respond correctly
pixhawk 2.1 (V3).
https://review.px4.io/plot_app?log=56624300-632a-4aa0-8108-3a8bb8be1b56
took-off in stabilized mode and the throttle respond correctly

This is currently required because it can cause conflit with offboard attitude control
where attitude setpoint are sent from mavlink_receiver and from mc_pos_control simultaneously.
@MaEtUgR
Copy link
Member

MaEtUgR commented Jun 20, 2018

I'm starting to rebase with a similar scheme like the flight task introduction pr from last time. We (@Stifael @RomanBapst) dicussed this yesterday in a call. The functionality should not change except for the yawspeed setpoint rescaling (see https://github.com/YUNEEC/Firmware/pull/2069/commits/500ffe5a622f85c0de409f0c9da233b8b5be3d7b for reference) which I will point out afterwards.

@MaEtUgR
Copy link
Member

MaEtUgR commented Jun 21, 2018

I pushed first rebase work to a new branch https://github.com/PX4/Firmware/tree/pr-flight-task-iteration to reduce confusion because the it's in terms of history quite different. The principle is the same as with the first flight task pr: I rebase all the files except for mc_pos_control_main.cpp because that is the file generating the huge pile of conflicts. Now I'll adjust that file to match the diff and not loose any of the changes in between.

@MaEtUgR
Copy link
Member

MaEtUgR commented Jun 21, 2018

Rebase pr with only small things open is now here: #9731

@MaEtUgR MaEtUgR deleted the Legacy-Auto-Manual-replacement branch August 29, 2018 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants