-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yawsticks on gimbal, vehicle follows gimbal in slowmode
- Loading branch information
Showing
9 changed files
with
290 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/modules/flight_mode_manager/tasks/Utility/Gimbal.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2025 PX4 Development Team. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* 3. Neither the name PX4 nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
****************************************************************************/ | ||
|
||
#include "Gimbal.hpp" | ||
#include <px4_platform_common/events.h> | ||
|
||
using namespace time_literals; | ||
using namespace matrix; | ||
|
||
Gimbal::Gimbal(ModuleParams *parent) : | ||
ModuleParams(parent) | ||
{} | ||
|
||
Gimbal::~Gimbal() | ||
{ | ||
releaseGimbalControlIfNeeded(); | ||
} | ||
|
||
bool Gimbal::checkForTelemetry(const hrt_abstime now) | ||
{ | ||
if (_gimbal_device_attitude_status_sub.updated()) { | ||
gimbal_device_attitude_status_s gimbal_device_attitude_status{}; | ||
|
||
if (_gimbal_device_attitude_status_sub.copy(&gimbal_device_attitude_status)) { | ||
_telemtry_timestamp = gimbal_device_attitude_status.timestamp; | ||
_telemetry_flags = gimbal_device_attitude_status.device_flags; | ||
_telemetry_yaw = Eulerf(Quatf(gimbal_device_attitude_status.q)).psi(); | ||
} | ||
} | ||
|
||
return now < _telemtry_timestamp + 2_s; | ||
} | ||
|
||
void Gimbal::acquireGimbalControlIfNeeded() | ||
{ | ||
if (!_have_gimbal_control) { | ||
_have_gimbal_control = true; | ||
|
||
vehicle_command_s vehicle_command{}; | ||
vehicle_command.command = vehicle_command_s::VEHICLE_CMD_DO_GIMBAL_MANAGER_CONFIGURE; | ||
vehicle_command.param1 = _param_mav_sys_id.get(); | ||
vehicle_command.param2 = _param_mav_comp_id.get(); | ||
vehicle_command.param3 = -1.0f; // Leave unchanged. | ||
vehicle_command.param4 = -1.0f; // Leave unchanged. | ||
vehicle_command.timestamp = hrt_absolute_time(); | ||
vehicle_command.source_system = _param_mav_sys_id.get(); | ||
vehicle_command.source_component = _param_mav_comp_id.get(); | ||
vehicle_command.target_system = _param_mav_sys_id.get(); | ||
vehicle_command.target_component = _param_mav_sys_id.get(); | ||
vehicle_command.from_external = false; | ||
_vehicle_command_pub.publish(vehicle_command); | ||
} | ||
} | ||
|
||
void Gimbal::releaseGimbalControlIfNeeded() | ||
{ | ||
if (_have_gimbal_control) { | ||
_have_gimbal_control = false; | ||
|
||
// Restore default flags, setting rate setpoints to NAN lead to unexpected behavior | ||
publishGimbalManagerSetAttitude(FLAGS_ROLL_PITCH_LOCKED, | ||
Quatf(NAN, NAN, NAN, NAN), | ||
Vector3f(NAN, 0.f, 0.f)); | ||
|
||
// Release gimbal | ||
vehicle_command_s vehicle_command{}; | ||
vehicle_command.command = vehicle_command_s::VEHICLE_CMD_DO_GIMBAL_MANAGER_CONFIGURE; | ||
vehicle_command.param1 = -3.0f; // Remove control if it had it. | ||
vehicle_command.param2 = -3.0f; // Remove control if it had it. | ||
vehicle_command.param3 = -1.0f; // Leave unchanged. | ||
vehicle_command.param4 = -1.0f; // Leave unchanged. | ||
vehicle_command.timestamp = hrt_absolute_time(); | ||
vehicle_command.from_external = false; | ||
vehicle_command.source_system = _param_mav_sys_id.get(); | ||
vehicle_command.source_component = _param_mav_comp_id.get(); | ||
vehicle_command.target_system = _param_mav_sys_id.get(); | ||
vehicle_command.target_component = _param_mav_comp_id.get(); | ||
_vehicle_command_pub.publish(vehicle_command); | ||
} | ||
} | ||
|
||
void Gimbal::publishGimbalManagerSetAttitude(const uint16_t gimbal_flags, | ||
const matrix::Quatf q_gimbal_setpoint, | ||
const matrix::Vector3f gimbal_rates) | ||
{ | ||
gimbal_manager_set_attitude_s gimbal_setpoint{}; | ||
gimbal_setpoint.origin_sysid = _param_mav_sys_id.get(); | ||
gimbal_setpoint.origin_compid = _param_mav_comp_id.get(); | ||
gimbal_setpoint.flags = gimbal_flags; | ||
q_gimbal_setpoint.copyTo(gimbal_setpoint.q); | ||
gimbal_setpoint.angular_velocity_x = gimbal_rates(0); | ||
gimbal_setpoint.angular_velocity_y = gimbal_rates(1); | ||
gimbal_setpoint.angular_velocity_z = gimbal_rates(2); | ||
gimbal_setpoint.timestamp = hrt_absolute_time(); | ||
_gimbal_manager_set_attitude_pub.publish(gimbal_setpoint); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2025 PX4 Development Team. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* 3. Neither the name PX4 nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
****************************************************************************/ | ||
|
||
/** | ||
* @file Gimbal.hpp | ||
* @brief Gimbal interface with flight tasks | ||
* @author Pernilla Wikström <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <px4_platform_common/module_params.h> | ||
#include <systemlib/mavlink_log.h> | ||
#include <uORB/Publication.hpp> | ||
#include <uORB/Subscription.hpp> | ||
#include <uORB/topics/gimbal_manager_set_attitude.h> | ||
#include <uORB/topics/gimbal_device_attitude_status.h> | ||
#include <uORB/topics/vehicle_command.h> | ||
|
||
#include "Sticks.hpp" | ||
|
||
class Gimbal : public ModuleParams | ||
{ | ||
public: | ||
Gimbal(ModuleParams *parent); | ||
~Gimbal(); | ||
|
||
bool checkForTelemetry(const hrt_abstime now); | ||
void publishGimbalManagerSetAttitude(const uint16_t gimbal_flags, | ||
const matrix::Quatf q_gimbal_setpoint, | ||
const matrix::Vector3f gimbal_rates); | ||
void acquireGimbalControlIfNeeded(); | ||
void releaseGimbalControlIfNeeded(); | ||
float getTelemetryYaw() { return _telemetry_yaw; } | ||
bool allAxesLockedConfirmed() { return _telemetry_flags == FLAGS_ALL_AXES_LOCKED; } | ||
|
||
static constexpr uint16_t FLAGS_ALL_AXES_LOCKED = gimbal_manager_set_attitude_s::GIMBAL_MANAGER_FLAGS_ROLL_LOCK | ||
| gimbal_manager_set_attitude_s::GIMBAL_MANAGER_FLAGS_PITCH_LOCK | ||
| gimbal_manager_set_attitude_s::GIMBAL_MANAGER_FLAGS_YAW_LOCK; | ||
static constexpr uint16_t FLAGS_ROLL_PITCH_LOCKED = | ||
gimbal_manager_set_attitude_s::GIMBAL_MANAGER_FLAGS_ROLL_LOCK | ||
| gimbal_manager_set_attitude_s::GIMBAL_MANAGER_FLAGS_PITCH_LOCK; | ||
|
||
private: | ||
bool _have_gimbal_control{false}; | ||
|
||
uORB::Subscription _gimbal_device_attitude_status_sub{ORB_ID(gimbal_device_attitude_status)}; | ||
uint16_t _telemetry_flags{}; | ||
hrt_abstime _telemtry_timestamp{}; | ||
float _telemetry_yaw{}; | ||
|
||
// Publications | ||
uORB::Publication<gimbal_manager_set_attitude_s> _gimbal_manager_set_attitude_pub{ORB_ID(gimbal_manager_set_attitude)}; | ||
uORB::Publication<vehicle_command_s> _vehicle_command_pub{ORB_ID(vehicle_command)}; | ||
|
||
DEFINE_PARAMETERS( | ||
(ParamInt<px4::params::MAV_SYS_ID>) _param_mav_sys_id, | ||
(ParamInt<px4::params::MAV_COMP_ID>) _param_mav_comp_id | ||
) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters