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

Trajectory generator for Auto mode #10746

Merged
merged 19 commits into from
Nov 6, 2018
Merged

Trajectory generator for Auto mode #10746

merged 19 commits into from
Nov 6, 2018

Conversation

bresch
Copy link
Member

@bresch bresch commented Oct 23, 2018

Based on #10696
Needs #10772 to work with VTOL planes.

Overview

This new FlightTask uses the velocity-based trajectory generator developed for manual control in #10696 to generate a smooth trajectory between waypoints. The strategy is quite simple: for each axis (north-east-down), a velocity target is generated using simple proportional controllers: v_target = K * (x_waypoint - x_trajectory) where x_... is the position. The controller then "drives" the trajectory to the waypoint like a servo controls its position by controlling the velocity of the motor. Furthermore, to ensure that the trajectory asymptotically converges to the line connecting the waypoints, a cross-track controller is added (here also, a simple proportional controller is used).

To robustify the algorithm, a time stretch strategy is used:
At each epoch, the increasing acceleration, constant acceleration and decreasing periods are computed. To generate the trajectory, the jerk is integrated using dt*time_stretch where time_stretch is a value between 0.0 and 1.0. This time stretch value is computed as a function of "how far the drone is behind the position of the trajectory". In summary, we can say that the trajectory will wait for the drone if it's too far from it.

Takeoff and landing

The advantage is that for takeoff and landing waypoints, the trajectory is still continuous. The new AutoMapper2 flight task is used to provide proper position and velocity setpoints given the type of waypoint:

  • Takeoff: xyz position of the setpoint and z velocity limit depending on the altitude above ground
  • Landing: xy position and z velocity limit depending on the agl

The interface used is similar to the basic FlightTask idea:

  • a value in the position setpoint means that the velocity target has to be generated by the P controller, a NAN means that the velocity target is given by the velocity setpoint message.
  • a value in the velocity setpoint means that:
    • if position setpoint is NAN then this is the velocity target
    • if position setpoint is finite then this is the maximum allowed velocity target
  • if both velocity and position setpoints are NAN, then the velocity target is set to zero.

Tests

Raw setpoints

Position along North axis:
normal_smoothing_position
Generated path:
normal_smoothing

Using this trajectory generator

Position along North axis:
trajectory_smoothing_position
Generated path:
trajectory_smoothing

How to use it

  • Set MPC_AUTO_MODE to 1
  • Set MPC_YAW_MODE to 3 (coordinated turns)
  • Tuning parameters:
    MPC_ACC_HOR is used as the maximum acceleration/deceleration for x and y
    MPC_JERK_MIN is used as the maximum jerk for x, y and z
    MPC_XY_CRUISE is the cruise speed, the max velocity of the trajectory
    MPC_XY_VEL_MAX is the max velocity at the input of the velocity controller (has to be bigger than the cruise speed to allow the position loop to do its job)
    MPC_ACC_DOWN_MAX
    MPC_ACC_UP_MAX
    MPC_Z_VEL_MAX_DN
    MPC_Z_VEL_MAX_UP
    MPC_XY_TRAJ_P controls the "aggressiveness" of the horizontal trajectory.
    MPC_Z_TRAJ_Pcontrols the "aggressiveness" of the vertical trajectory.

@ndepal
Copy link
Contributor

ndepal commented Oct 23, 2018

Cool! I see that this causes the vehicle to deviate quite far from the waypoints and the straight lines connecting them. What guarantees can be made about maximum deviation of the vehicle position from the straight lines? How hard will it be to predict the actual trajectory ahead of flight (e.g. drawing the red lines from above in QGC as the waypoints are placed)?

@bresch
Copy link
Member Author

bresch commented Oct 24, 2018

Hi, @ndepal
The tracking can now be adjusted with the MPC_XY_TRAJ_P and MPX_Z_TRAJ_P gains. To adjust the distance at which the drone starts to turn, the acceptance radius had to be adjusted. However, the acceptance radius being fixed, an acute angle might generate an overshoot due to the maximum dynamics of the trajectory (velocity, acceleration and jerk limitations.
Below a plot with the MPC_XY_TRAJ_P gain slightly increased and with smaller acceptance radius.
bokeh_plot 3
(log)

As it directly depends on the parameters of the trajectory and is completely independent of the vehicle's dynamics, the trajectory (and path) is deterministic. The green line from above could then be displayed on QGC.

@dagar
Copy link
Member

dagar commented Oct 24, 2018

CI build failures.

image

@bresch bresch force-pushed the dev-auto-trajectory branch from f478733 to b95323c Compare October 25, 2018 06:38
@ndepal
Copy link
Contributor

ndepal commented Oct 25, 2018

Thanks for the explanation @bresch.
Being able to show the green line in QGC could be valuable, as it can deviate significantly from the straight lines. However, the actual vehicle trajectory deviates quite a bit from the setpoints as well. Is this by design or a question of position control tuning? This cam make it quite difficult to plan missions, especially when the mission is generated algorithmically.

A while ago there was an effort to implement spline/bezier trajectories. These would be smooth and feasible trajectories, allowing for a controller that closely tracks the planned path. And there are algorithms for planning such paths in an obstacle map. Do you know what the plans are in that regard? Does this PR replace those efforts?

@bresch
Copy link
Member Author

bresch commented Oct 25, 2018

@ndepal The bad tracking is mostly due to the poorly tuned model I have in jMAVSim. The tracking will also be improved by adding the acceleration trajectory as a feedforward to the velocity controller.

I'm not 100% sure, but I think the spline/bezier trajectories you're mentioning are in fact paths and not trajectories.

@bresch bresch force-pushed the dev-auto-trajectory branch from 1afd26c to 70bb4a3 Compare October 25, 2018 14:22
@dagar dagar requested a review from a team October 25, 2018 14:38
@dagar
Copy link
Member

dagar commented Oct 25, 2018

We'll need to figure out something to get this to fit in px4fmu-v2, but in the meantime let's test on other platforms.

@PX4/testflights please test this on a few multicopter platforms that aren't px4fmu-v2.

@bresch
Copy link
Member Author

bresch commented Oct 25, 2018

@PX4/testflights Could you please test that on a quad? Something like a F450
It should be a small mission, possibly a survey, for example like this one: https://logs.px4.io/plot_app?log=bf80702d-7046-4e56-9669-51bb7a1c841a

MPC_AUTO_MODE = 3 (enable trajectory generator)
MPC_YAW_MODE = 3 (generate yaw from trajectory velocity vector)

The following parameters should be good for a small quad:
MPC_ACC_HOR_MAX = 3
MPC_JERK_MIN = 5
MPC_XY_CRUISE = 5
MPC_XY_VEL_MAX = 12
MPC_ACC_DOWN_MAX = 5
MPC_ACC_UP_MAX = 8
MPC_Z_VEL_MAX_DN = 2
MPC_Z_VEL_MAX_UP = 3
MPC_LAND_ALT1 = MPC_LAND_ALT2 = 5
MPC_LAND_SPEED = 0.7
MPC_XY_TRAJ_P= 0.5
MPC_Z_TRAJ_P= 0.5

Thanks in advance!

@dagar dagar requested review from RomanBapst and Stifael October 25, 2018 15:29
@bresch
Copy link
Member Author

bresch commented Oct 25, 2018

@Avysuarez Thanks! Could you also try with those specific parameters please: #10746 (comment)
The jerk and accelerations are set too high, that's why you don't see the difference :)
I'll maybe set those as default in the future.

@Avysuarez
Copy link

@bresch
ok I will try again

@bresch
Copy link
Member Author

bresch commented Oct 26, 2018

@bresch bresch force-pushed the dev-auto-trajectory branch from 9edaf84 to 1631789 Compare October 26, 2018 10:02
@bresch
Copy link
Member Author

bresch commented Oct 26, 2018

rebased on Master

@Avysuarez
Copy link

I tested again with the same vehicles but with the suggested parameters. The flight is smoother.
Pixhawk 4.
https://review.px4.io/plot_app?log=4ae93006-95ae-4c09-96b1-0b66f1fb31bf

Pixhawk 4 mini.
https://review.px4.io/plot_app?log=f6bbcc65-59d1-4d6c-a85c-98095cc76be8

@bresch
Copy link
Member Author

bresch commented Oct 26, 2018

@Avysuarez Great! Thanks a lot.

@bresch
Copy link
Member Author

bresch commented Oct 27, 2018

@dannyfpv Awesome, thanks!
It looks even better than in simulation.

@bresch
Copy link
Member Author

bresch commented Oct 29, 2018

Here are some plots of a real flight (extracted from a flight made by @dannyfpv )
bokeh_plot 4
bokeh_plot 10
bokeh_plot 8
Note: the velocity setpoint in the graph above is the sum of the output of the position P controller and the feedforward generated by the trajectory generator.
bokeh_plot 11
bokeh_plot 12

A general good result is that all the discontinuities have been removed up to the rate setpoints.

@bresch bresch removed the request for review from a team October 29, 2018 09:58
bresch added 11 commits November 2, 2018 16:16
- move the update after the integration: a new computed jerk has an impact at the next epoch only
- add jerk reduction in case of too large integration time: when a jerk of "min_jerk" during dt is too much
- add jerk reduction if the integration time is larger than the predicted one and that integrating that jerk would lead to an acceleration overshoot
- rename some variables
@bresch bresch force-pushed the dev-auto-trajectory branch from c9b19a6 to 22d178a Compare November 2, 2018 15:34
@bresch
Copy link
Member Author

bresch commented Nov 2, 2018

@dagar Rebased on Master.
Here is a mission test in SITL: https://logs.px4.io/plot_app?log=aa76fe5b-5ab7-418c-b15c-a3d2b92bc2fc

@bresch
Copy link
Member Author

bresch commented Nov 2, 2018

Could we merge #10745 and #10772 before this PR?

@RomanBapst RomanBapst merged commit 90cee2d into master Nov 6, 2018
@RomanBapst RomanBapst deleted the dev-auto-trajectory branch November 6, 2018 21:17
@VTOLDavid
Copy link
Contributor

Hi, I'm trying to confingure this mode in master but the parameters to configure it are mising. What I am doing wrong?

@bresch
Copy link
Member Author

bresch commented Dec 3, 2018

Hi @VTOLDavid ,
Which parameters are missing? Do you have the latest QGC? Are they not there even if you look for them in the search field?
Most of them are not new.

@VTOLDavid
Copy link
Contributor

This parameters do not show up:
MPC_AUTO_MODE
MPC_YAW_MODE
MPC_XY_TRAJ_P
MPC_Z_TRAJ_P
Should I update Qgroundcontrol? I updated it 1 o 2 months ago.
I tried flashing with Qgroundcontrol stable, developer and beta versions and compiling it doing git clone https://github.com/PX4/Firmware.git and make px4_fmu-v3_default it possible that I'm not getting the current version of the master code?

@hamishwillee
Copy link
Contributor

hamishwillee commented Dec 3, 2018

Should I update Qgroundcontrol? I updated it 1 o 2 months ago.

Yes. Try a daily build. Though you should normally still see the parameters as long as you are using firmware based on the master codeline (ie param metadata is in firmware, not QGC).

The parameters are in master - you can tell because they show up in the docs here: https://docs.px4.io/en/advanced_config/parameter_reference.html#MPC_AUTO_MODE (QGC firmware gets params from the same source).

@VTOLDavid
Copy link
Contributor

Thanks, solved!

@bangyanz
Copy link

@bresch I am a bit confused about your _generateTrajectory() code. It looks like you computed the desired velocity_setpoint in _prepareSetpoints(). But you immediately change that _velocity_setpoint to vel_sp_smooth in generateTrajectory(), which does integration based on the jerk and vel_prev. I failed to see how the computed _velocity_setpoint factors into the new vel_sp_smooth. Can you explain that?

@bresch
Copy link
Member Author

bresch commented Jan 28, 2019

Hi @bangyanz ,
The _velocity_setpoint in _prepareSetpoints is the "desired/target velocity", before smoothing. In _generateTrajectory, this desired velocity is used as an input to the algorithm here.
The variable _velocity_setpoint is then re-used to carry the result of the algorithm (vel_sp_smooth) back to the controller.

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.

10 participants