2024-11-08 19:29:06 -04:00
|
|
|
#include "AP_Mount_config.h"
|
2022-06-14 01:56:34 -03:00
|
|
|
|
|
|
|
#if HAL_MOUNT_STORM32SERIAL_ENABLED
|
2024-11-08 19:29:06 -04:00
|
|
|
|
|
|
|
#include "AP_Mount_SToRM32_serial.h"
|
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
2016-01-11 18:21:54 -04:00
|
|
|
#include <GCS_MAVLink/include/mavlink/v2.0/checksum.h>
|
2015-05-26 08:43:13 -03:00
|
|
|
|
|
|
|
// update mount position - should be called periodically
|
|
|
|
void AP_Mount_SToRM32_serial::update()
|
|
|
|
{
|
|
|
|
// exit immediately if not initialised
|
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_incoming(); // read the incoming messages from the gimbal
|
|
|
|
|
2023-12-29 03:10:52 -04:00
|
|
|
// change to RC_TARGETING mode if RC input has changed
|
|
|
|
set_rctargeting_on_rcinput_change();
|
|
|
|
|
2015-05-26 08:43:13 -03:00
|
|
|
// flag to trigger sending target angles to gimbal
|
|
|
|
bool resend_now = false;
|
|
|
|
|
|
|
|
// update based on mount mode
|
|
|
|
switch(get_mode()) {
|
|
|
|
// move mount to a "retracted" position. To-Do: remove support and replace with a relaxed mode?
|
2022-06-23 00:39:41 -03:00
|
|
|
case MAV_MOUNT_MODE_RETRACT: {
|
2022-08-30 08:15:10 -03:00
|
|
|
const Vector3f &target = _params.retract_angles.get();
|
2023-07-21 03:35:42 -03:00
|
|
|
mnt_target.angle_rad.set(target*DEG_TO_RAD, false);
|
|
|
|
mnt_target.target_type = MountTargetType::ANGLE;
|
2015-05-26 08:43:13 -03:00
|
|
|
break;
|
2022-06-23 00:39:41 -03:00
|
|
|
}
|
2015-05-26 08:43:13 -03:00
|
|
|
|
|
|
|
// move mount to a neutral position, typically pointing forward
|
2022-06-23 00:39:41 -03:00
|
|
|
case MAV_MOUNT_MODE_NEUTRAL: {
|
2022-08-30 08:15:10 -03:00
|
|
|
const Vector3f &target = _params.neutral_angles.get();
|
2023-07-21 03:35:42 -03:00
|
|
|
mnt_target.angle_rad.set(target*DEG_TO_RAD, false);
|
|
|
|
mnt_target.target_type = MountTargetType::ANGLE;
|
2015-05-26 08:43:13 -03:00
|
|
|
break;
|
2022-06-23 00:39:41 -03:00
|
|
|
}
|
2015-05-26 08:43:13 -03:00
|
|
|
|
|
|
|
// point to the angles given by a mavlink message
|
|
|
|
case MAV_MOUNT_MODE_MAVLINK_TARGETING:
|
2023-07-21 03:35:42 -03:00
|
|
|
// mnt_target should have already been filled in by set_angle_target() or set_rate_target()
|
|
|
|
if (mnt_target.target_type == MountTargetType::RATE) {
|
|
|
|
update_angle_target_from_rate(mnt_target.rate_rads, mnt_target.angle_rad);
|
2022-06-23 00:39:41 -03:00
|
|
|
}
|
2018-12-20 05:01:48 -04:00
|
|
|
resend_now = true;
|
2015-05-26 08:43:13 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
// RC radio manual angle control, but with stabilization from the AHRS
|
2024-11-08 23:47:30 -04:00
|
|
|
case MAV_MOUNT_MODE_RC_TARGETING:
|
|
|
|
update_mnt_target_from_rc_target();
|
2015-05-26 08:43:13 -03:00
|
|
|
resend_now = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// point mount to a GPS point given by the mission planner
|
|
|
|
case MAV_MOUNT_MODE_GPS_POINT:
|
2023-07-21 03:35:42 -03:00
|
|
|
if (get_angle_target_to_roi(mnt_target.angle_rad)) {
|
|
|
|
mnt_target.target_type = MountTargetType::ANGLE;
|
2015-05-26 08:43:13 -03:00
|
|
|
resend_now = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-07-21 03:35:42 -03:00
|
|
|
// point mount to Home location
|
2020-12-21 05:07:43 -04:00
|
|
|
case MAV_MOUNT_MODE_HOME_LOCATION:
|
2023-07-21 03:35:42 -03:00
|
|
|
if (get_angle_target_to_home(mnt_target.angle_rad)) {
|
|
|
|
mnt_target.target_type = MountTargetType::ANGLE;
|
2020-12-21 05:07:43 -04:00
|
|
|
resend_now = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-07-21 03:35:42 -03:00
|
|
|
// point mount to another vehicle
|
2019-03-16 04:06:02 -03:00
|
|
|
case MAV_MOUNT_MODE_SYSID_TARGET:
|
2023-07-21 03:35:42 -03:00
|
|
|
if (get_angle_target_to_sysid(mnt_target.angle_rad)) {
|
|
|
|
mnt_target.target_type = MountTargetType::ANGLE;
|
2019-03-16 04:06:02 -03:00
|
|
|
resend_now = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-26 08:43:13 -03:00
|
|
|
default:
|
|
|
|
// we do not know this mode so do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resend target angles at least once per second
|
2015-11-19 23:12:50 -04:00
|
|
|
resend_now = resend_now || ((AP_HAL::millis() - _last_send) > AP_MOUNT_STORM32_SERIAL_RESEND_MS);
|
2015-05-26 08:43:13 -03:00
|
|
|
|
2015-11-19 23:12:50 -04:00
|
|
|
if ((AP_HAL::millis() - _last_send) > AP_MOUNT_STORM32_SERIAL_RESEND_MS*2) {
|
2015-05-26 08:43:13 -03:00
|
|
|
_reply_type = ReplyType_UNKNOWN;
|
|
|
|
}
|
|
|
|
if (can_send(resend_now)) {
|
|
|
|
if (resend_now) {
|
2023-07-21 03:35:42 -03:00
|
|
|
send_target_angles(mnt_target.angle_rad);
|
2015-05-26 08:43:13 -03:00
|
|
|
get_angles();
|
|
|
|
_reply_type = ReplyType_ACK;
|
|
|
|
_reply_counter = 0;
|
|
|
|
_reply_length = get_reply_size(_reply_type);
|
|
|
|
} else {
|
|
|
|
get_angles();
|
|
|
|
_reply_type = ReplyType_DATA;
|
|
|
|
_reply_counter = 0;
|
|
|
|
_reply_length = get_reply_size(_reply_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 05:07:22 -03:00
|
|
|
// get attitude as a quaternion. returns true on success
|
|
|
|
bool AP_Mount_SToRM32_serial::get_attitude_quaternion(Quaternion& att_quat)
|
2015-05-26 08:43:13 -03:00
|
|
|
{
|
2022-07-11 05:07:22 -03:00
|
|
|
att_quat.from_euler(radians(_current_angle.x * 0.01f), radians(_current_angle.y * 0.01f), radians(_current_angle.z * 0.01f));
|
|
|
|
return true;
|
2015-05-26 08:43:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Mount_SToRM32_serial::can_send(bool with_control) {
|
|
|
|
uint16_t required_tx = 1;
|
|
|
|
if (with_control) {
|
|
|
|
required_tx += sizeof(AP_Mount_SToRM32_serial::cmd_set_angles_struct);
|
|
|
|
}
|
2024-05-22 00:41:17 -03:00
|
|
|
return (_reply_type == ReplyType_UNKNOWN) && (_uart->txspace() >= required_tx);
|
2015-05-26 08:43:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-26 08:44:33 -03:00
|
|
|
// send_target_angles
|
2022-06-23 00:39:41 -03:00
|
|
|
void AP_Mount_SToRM32_serial::send_target_angles(const MountTarget& angle_target_rad)
|
2015-05-26 08:43:13 -03:00
|
|
|
{
|
|
|
|
|
|
|
|
static cmd_set_angles_struct cmd_set_angles_data = {
|
2015-08-24 22:14:48 -03:00
|
|
|
0xFA,
|
2015-05-26 08:43:13 -03:00
|
|
|
0x0E,
|
|
|
|
0x11,
|
|
|
|
0, // pitch
|
|
|
|
0, // roll
|
|
|
|
0, // yaw
|
|
|
|
0, // flags
|
|
|
|
0, // type
|
|
|
|
0, // crc
|
|
|
|
};
|
|
|
|
|
|
|
|
// exit immediately if not initialised
|
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:41:17 -03:00
|
|
|
if ((size_t)_uart->txspace() < sizeof(cmd_set_angles_data)) {
|
2015-05-26 08:43:13 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 00:39:41 -03:00
|
|
|
// send CMD_SETANGLE (Note: reversed pitch and yaw)
|
|
|
|
cmd_set_angles_data.pitch = -degrees(angle_target_rad.pitch);
|
|
|
|
cmd_set_angles_data.roll = degrees(angle_target_rad.roll);
|
2023-03-25 00:50:56 -03:00
|
|
|
cmd_set_angles_data.yaw = -degrees(angle_target_rad.get_bf_yaw());
|
2015-05-26 08:43:13 -03:00
|
|
|
|
|
|
|
uint8_t* buf = (uint8_t*)&cmd_set_angles_data;
|
|
|
|
|
|
|
|
cmd_set_angles_data.crc = crc_calculate(&buf[1], sizeof(cmd_set_angles_data)-3);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i != sizeof(cmd_set_angles_data) ; i++) {
|
2024-05-22 00:41:17 -03:00
|
|
|
_uart->write(buf[i]);
|
2015-05-26 08:43:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// store time of send
|
2015-11-19 23:12:50 -04:00
|
|
|
_last_send = AP_HAL::millis();
|
2015-05-26 08:43:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Mount_SToRM32_serial::get_angles() {
|
|
|
|
// exit immediately if not initialised
|
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:41:17 -03:00
|
|
|
if (_uart->txspace() < 1) {
|
2015-05-26 08:43:13 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:41:17 -03:00
|
|
|
_uart->write('d');
|
2015-05-26 08:43:13 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t AP_Mount_SToRM32_serial::get_reply_size(ReplyType reply_type) {
|
|
|
|
switch (reply_type) {
|
|
|
|
case ReplyType_DATA:
|
|
|
|
return sizeof(SToRM32_reply_data_struct);
|
|
|
|
break;
|
|
|
|
case ReplyType_ACK:
|
|
|
|
return sizeof(SToRM32_reply_ack_struct);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AP_Mount_SToRM32_serial::read_incoming() {
|
|
|
|
uint8_t data;
|
|
|
|
int16_t numc;
|
|
|
|
|
2024-05-22 00:41:17 -03:00
|
|
|
numc = _uart->available();
|
2015-05-26 08:43:13 -03:00
|
|
|
|
2023-11-04 06:03:14 -03:00
|
|
|
if (numc < 0 ) {
|
2015-05-26 08:43:13 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int16_t i = 0; i < numc; i++) { // Process bytes received
|
2024-05-22 00:41:17 -03:00
|
|
|
data = _uart->read();
|
2015-05-26 08:43:13 -03:00
|
|
|
if (_reply_type == ReplyType_UNKNOWN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-06 10:50:53 -03:00
|
|
|
_buffer[_reply_counter++] = data;
|
2015-05-26 08:43:13 -03:00
|
|
|
if (_reply_counter == _reply_length) {
|
|
|
|
parse_reply();
|
|
|
|
|
|
|
|
switch (_reply_type) {
|
|
|
|
case ReplyType_ACK:
|
|
|
|
_reply_type = ReplyType_DATA;
|
|
|
|
_reply_length = get_reply_size(_reply_type);
|
|
|
|
_reply_counter = 0;
|
|
|
|
break;
|
|
|
|
case ReplyType_DATA:
|
|
|
|
_reply_type = ReplyType_UNKNOWN;
|
|
|
|
_reply_length = get_reply_size(_reply_type);
|
|
|
|
_reply_counter = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_reply_length = get_reply_size(_reply_type);
|
|
|
|
_reply_counter = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Mount_SToRM32_serial::parse_reply() {
|
|
|
|
uint16_t crc;
|
|
|
|
bool crc_ok;
|
|
|
|
|
|
|
|
switch (_reply_type) {
|
|
|
|
case ReplyType_DATA:
|
2016-06-06 10:50:53 -03:00
|
|
|
crc = crc_calculate(&_buffer[0], sizeof(_buffer.data) - 3);
|
2015-05-26 08:43:13 -03:00
|
|
|
crc_ok = crc == _buffer.data.crc;
|
|
|
|
if (!crc_ok) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-08 13:09:49 -04:00
|
|
|
// Parse angles (Note: reversed pitch and yaw) to match ardupilot coordinate system
|
2015-05-26 08:43:13 -03:00
|
|
|
_current_angle.x = _buffer.data.imu1_roll;
|
2022-12-08 13:09:49 -04:00
|
|
|
_current_angle.y = -_buffer.data.imu1_pitch;
|
|
|
|
_current_angle.z = -_buffer.data.imu1_yaw;
|
2015-05-26 08:43:13 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 01:56:34 -03:00
|
|
|
#endif // HAL_MOUNT_STORM32SERIAL_ENABLED
|