“mc_att_control” is responsible for PX4 multicopter attitude control, this application can be found in “Firmware/src/modules/mc_att_control”. Here is what I understand about this application.
Similar to “mc_pos_control”, this application is started by script “rc.mc_apps” on line 32:
The source code for the application exists in “mc_att_control_main.cpp”, it follows a very similar architecture as “mc_pos_control_main.cpp”. You will find the call hierarchy fmailiar:
- line 992: function “start()” is called by the script
- line 1001: function “task_main_trampoline” is called by “start()”
- line 791: seems like “task_main_trampoline” is only used to access “task_main”
- line 797: function “task_main()” begins, subscribes to a bunch of topics
- line 822: enter while loop, while application is not stopped
- line 857: copy attitude and control state topics
- line 870-944: control attitude (outer loop, control angular error)
- line 882: function “control_attitude(dt)” is called
- line 905-910: “_rates_sp_id” topic is advertised and published
- line 946-981: control attitude rates (inter loop, control angular rate error)
- line 947: function “control_attitude_rates(dt)” is called
- line 962-069: “_actuators_id” topic is advertised and published (which contains _att_control vector)
In summary, the application starts->control attitude->publish and advertise->control attitude rates->publish and advertise.
“mc_att_control” has some small useful functions that are different to the ones found in “mc_pos_control”:
- Poll functions
- vehicle_control_mode_poll(): check for changes in vehicle control mode, looks like vehicle mode is stored in the topic “vehicle_control_mode”
- vehicle_manual_poll(): check for changes in manual inputs, need to investigate what this means
- vehicle_attitude_setpoint_poll(): check for attitude setpoint updates
- vehicle_rates_setpoint_poll(): check for rates setpoint updates
- arming_status_poll(): check for arming status updates
- vehicle_status_poll(): check for vehicle status updates, need to investigate what is vehicle status
- vehicle_motor_limits_poll(): check for vehicle motor limits status, need to investigate what are motor limits
I think for the sake of understanding source code, I will try to implement a new mode similar to the APM flip mode. Based on my understanding so far, the following tasks have to be done in order to create a flip mode:
- create a “_v_control_mode.flag_control_flip_enabled” flag
- in task_main, create an if statement that skips control_attitude and execute my custom code
Outcome of this experiment will be recorded in later blogs.
What is going on in task_main?
If you see ?? it means I don’t know what this line is doing, please fill in.
- 803-811: subscribe to topics
- 813-814: update parameters
- 816-820: set up poll topic, fd stands for file descriptor
- 822: enter while loop, loop continues while attitude control is required
- 824-840: wait for data
- 842-982: run controller if there is data coming in
- 857: copy attitude and control state data
- 867-875: in rattitude, if pitch or roll is above threshold, no need to control
- 877-944: run control_attitude if attitude control is enabled, otherwise just poll rates setpoint topic
- 946-981: run control_attitude_rates if rate control is enabled, otherwise do nothing