2024-11-08 19:29:06 -04:00
|
|
|
#include "AP_Mount_config.h"
|
2015-01-15 18:46:15 -04:00
|
|
|
|
2022-06-14 01:54:57 -03:00
|
|
|
#if HAL_MOUNT_ALEXMOS_ENABLED
|
2024-11-08 19:29:06 -04:00
|
|
|
|
|
|
|
#include "AP_Mount_Alexmos.h"
|
|
|
|
|
2022-06-14 01:54:57 -03:00
|
|
|
#include <AP_SerialManager/AP_SerialManager.h>
|
2015-01-15 18:46:15 -04:00
|
|
|
|
2024-06-14 17:10:43 -03:00
|
|
|
//definition of the commands id for the Alexmos Serial Protocol
|
|
|
|
#define CMD_READ_PARAMS 'R'
|
|
|
|
#define CMD_WRITE_PARAMS 'W'
|
|
|
|
#define CMD_REALTIME_DATA 'D'
|
|
|
|
#define CMD_BOARD_INFO 'V'
|
|
|
|
#define CMD_CALIB_ACC 'A'
|
|
|
|
#define CMD_CALIB_GYRO 'g'
|
|
|
|
#define CMD_CALIB_EXT_GAIN 'G'
|
|
|
|
#define CMD_USE_DEFAULTS 'F'
|
|
|
|
#define CMD_CALIB_POLES 'P'
|
|
|
|
#define CMD_RESET 'r'
|
|
|
|
#define CMD_HELPER_DATA 'H'
|
|
|
|
#define CMD_CALIB_OFFSET 'O'
|
|
|
|
#define CMD_CALIB_BAT 'B'
|
|
|
|
#define CMD_MOTORS_ON 'M'
|
|
|
|
#define CMD_MOTORS_OFF 'm'
|
|
|
|
#define CMD_CONTROL 'C'
|
|
|
|
#define CMD_TRIGGER_PIN 'T'
|
|
|
|
#define CMD_EXECUTE_MENU 'E'
|
|
|
|
#define CMD_GET_ANGLES 'I'
|
|
|
|
#define CMD_CONFIRM 'C'
|
|
|
|
// Board v3.x only
|
|
|
|
#define CMD_BOARD_INFO_3 20
|
|
|
|
#define CMD_READ_PARAMS_3 21
|
|
|
|
#define CMD_WRITE_PARAMS_3 22
|
|
|
|
#define CMD_REALTIME_DATA_3 23
|
|
|
|
#define CMD_SELECT_IMU_3 24
|
|
|
|
#define CMD_READ_PROFILE_NAMES 28
|
|
|
|
#define CMD_WRITE_PROFILE_NAMES 29
|
|
|
|
#define CMD_QUEUE_PARAMS_INFO_3 30
|
|
|
|
#define CMD_SET_PARAMS_3 31
|
|
|
|
#define CMD_SAVE_PARAMS_3 32
|
|
|
|
#define CMD_READ_PARAMS_EXT 33
|
|
|
|
#define CMD_WRITE_PARAMS_EXT 34
|
|
|
|
#define CMD_AUTO_PID 35
|
|
|
|
#define CMD_SERVO_OUT 36
|
|
|
|
#define CMD_ERROR 255
|
|
|
|
|
|
|
|
#define AP_MOUNT_ALEXMOS_MODE_NO_CONTROL 0
|
|
|
|
#define AP_MOUNT_ALEXMOS_MODE_SPEED 1
|
|
|
|
#define AP_MOUNT_ALEXMOS_MODE_ANGLE 2
|
|
|
|
#define AP_MOUNT_ALEXMOS_MODE_SPEED_ANGLE 3
|
|
|
|
#define AP_MOUNT_ALEXMOS_MODE_RC 4
|
|
|
|
|
|
|
|
#define AP_MOUNT_ALEXMOS_SPEED 30 // deg/s
|
|
|
|
|
|
|
|
#define INT14_DEGREES (360.0f / float(0x3FFF)) // 1 full rotation in degrees over 14 bit range
|
|
|
|
#define VALUE_TO_DEGREE(d) (float(d)*INT14_DEGREES)
|
|
|
|
#define DEGREE_TO_VALUE(d) ((int16_t)((float)(d)*(1.0f/INT14_DEGREES)))
|
|
|
|
#define DEGREE_PER_SEC_TO_VALUE(d) ((int16_t)((float)(d)*(1.0f/0.1220740379f)))
|
|
|
|
|
2019-08-27 03:23:30 -03:00
|
|
|
void AP_Mount_Alexmos::init()
|
2015-01-16 06:53:54 -04:00
|
|
|
{
|
2019-08-27 03:23:30 -03:00
|
|
|
const AP_SerialManager& serial_manager = AP::serialmanager();
|
|
|
|
|
2015-01-19 09:38:49 -04:00
|
|
|
// check for alexmos protcol
|
2015-03-27 22:46:58 -03:00
|
|
|
if ((_port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_AlexMos, 0))) {
|
2015-01-19 09:38:49 -04:00
|
|
|
_initialised = true;
|
|
|
|
get_boardinfo();
|
|
|
|
read_params(0); //we request parameters for profile 0 and therfore get global and profile parameters
|
|
|
|
}
|
2023-04-20 05:22:59 -03:00
|
|
|
AP_Mount_Backend::init();
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update mount position - should be called periodically
|
|
|
|
void AP_Mount_Alexmos::update()
|
|
|
|
{
|
2015-01-16 06:53:54 -04:00
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
2015-01-16 06:53:54 -04:00
|
|
|
read_incoming(); // read the incoming messages from the gimbal
|
2015-01-15 18:46:15 -04:00
|
|
|
|
|
|
|
// update based on mount mode
|
2022-06-23 00:38:12 -03:00
|
|
|
switch (get_mode()) {
|
2015-01-15 18:46:15 -04:00
|
|
|
// move mount to a "retracted" position. we do not implement a separate servo based retract mechanism
|
2022-06-23 00:38:12 -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.target_type = MountTargetType::ANGLE;
|
|
|
|
mnt_target.angle_rad.set(target*DEG_TO_RAD, false);
|
2015-01-15 18:46:15 -04:00
|
|
|
break;
|
2022-06-23 00:38:12 -03:00
|
|
|
}
|
2015-01-15 18:46:15 -04:00
|
|
|
|
|
|
|
// move mount to a neutral position, typically pointing forward
|
2022-06-23 00:38:12 -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.target_type = MountTargetType::ANGLE;
|
|
|
|
mnt_target.angle_rad.set(target*DEG_TO_RAD, false);
|
2015-01-15 18:46:15 -04:00
|
|
|
break;
|
2022-06-23 00:38:12 -03:00
|
|
|
}
|
2015-01-15 18:46:15 -04:00
|
|
|
|
|
|
|
// point to the angles given by a mavlink message
|
|
|
|
case MAV_MOUNT_MODE_MAVLINK_TARGETING:
|
2023-07-21 03:35:42 -03:00
|
|
|
// mavlink targets are stored while handling the incoming message
|
2015-01-15 18:46:15 -04: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-01-15 18:46:15 -04:00
|
|
|
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-01-15 18:46:15 -04:00
|
|
|
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
|
|
|
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
|
|
|
break;
|
|
|
|
|
2015-01-15 18:46:15 -04:00
|
|
|
default:
|
|
|
|
// we do not know this mode so do nothing
|
|
|
|
break;
|
|
|
|
}
|
2022-06-23 00:38:12 -03:00
|
|
|
|
2023-07-21 03:35:42 -03:00
|
|
|
// send target angles or rates depending on the target type
|
|
|
|
switch (mnt_target.target_type) {
|
|
|
|
case MountTargetType::RATE:
|
|
|
|
update_angle_target_from_rate(mnt_target.rate_rads, mnt_target.angle_rad);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case MountTargetType::ANGLE:
|
|
|
|
// send latest angle targets to gimbal
|
|
|
|
control_axis(mnt_target.angle_rad);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
2022-09-29 02:11:31 -03:00
|
|
|
// has_pan_control - returns true if this mount can control its pan (required for multicopters)
|
2015-01-15 18:46:15 -04:00
|
|
|
bool AP_Mount_Alexmos::has_pan_control() const
|
|
|
|
{
|
2022-08-26 00:42:17 -03:00
|
|
|
return _gimbal_3axis && yaw_range_valid();
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-11 05:07:22 -03:00
|
|
|
// get attitude as a quaternion. returns true on success
|
|
|
|
bool AP_Mount_Alexmos::get_attitude_quaternion(Quaternion& att_quat)
|
2015-01-15 18:46:15 -04:00
|
|
|
{
|
2015-06-05 00:26:13 -03:00
|
|
|
if (!_initialised) {
|
2022-07-11 05:07:22 -03:00
|
|
|
return false;
|
2015-06-05 00:26:13 -03:00
|
|
|
}
|
|
|
|
|
2022-07-11 05:07:22 -03:00
|
|
|
// request attitude from gimbal
|
2015-01-16 04:19:24 -04:00
|
|
|
get_angles();
|
2022-07-11 05:07:22 -03:00
|
|
|
|
|
|
|
// construct quaternion
|
|
|
|
att_quat.from_euler(radians(_current_angle.x), radians(_current_angle.y), radians(_current_angle.z));
|
|
|
|
return true;
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-16 06:53:54 -04:00
|
|
|
* get_angles
|
2015-01-15 18:46:15 -04:00
|
|
|
*/
|
2015-01-16 06:53:54 -04:00
|
|
|
void AP_Mount_Alexmos::get_angles()
|
|
|
|
{
|
2015-01-15 18:46:15 -04:00
|
|
|
uint8_t data[1] = {(uint8_t)1};
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_GET_ANGLES, data, 1);
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
2015-06-05 00:09:12 -03:00
|
|
|
|
2015-01-15 18:46:15 -04:00
|
|
|
/*
|
2015-06-05 00:09:12 -03:00
|
|
|
* set_motor will activate motors if true, and disable them if false.
|
2015-01-15 18:46:15 -04:00
|
|
|
*/
|
2015-06-05 00:09:12 -03:00
|
|
|
void AP_Mount_Alexmos::set_motor(bool on)
|
|
|
|
{
|
2015-01-16 06:53:54 -04:00
|
|
|
if (on) {
|
|
|
|
uint8_t data[1] = {(uint8_t)1};
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_MOTORS_ON, data, 1);
|
2015-01-15 18:46:15 -04:00
|
|
|
} else {
|
2015-01-16 06:53:54 -04:00
|
|
|
uint8_t data[1] = {(uint8_t)1};
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_MOTORS_OFF, data, 1);
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get board version and firmware version
|
|
|
|
*/
|
2015-01-16 06:53:54 -04:00
|
|
|
void AP_Mount_Alexmos::get_boardinfo()
|
|
|
|
{
|
|
|
|
if (_board_version != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-15 18:46:15 -04:00
|
|
|
uint8_t data[1] = {(uint8_t)1};
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_BOARD_INFO, data, 1);
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
2015-01-16 04:19:24 -04:00
|
|
|
/*
|
2022-06-23 00:38:12 -03:00
|
|
|
control_axis : send new angle target to the gimbal at a fixed speed of 30 deg/s
|
2015-01-16 04:19:24 -04:00
|
|
|
*/
|
2022-06-23 00:38:12 -03:00
|
|
|
void AP_Mount_Alexmos::control_axis(const MountTarget& angle_target_rad)
|
2015-01-16 06:53:54 -04:00
|
|
|
{
|
2015-01-16 18:13:17 -04:00
|
|
|
alexmos_parameters outgoing_buffer;
|
|
|
|
outgoing_buffer.angle_speed.mode = AP_MOUNT_ALEXMOS_MODE_ANGLE;
|
|
|
|
outgoing_buffer.angle_speed.speed_roll = DEGREE_PER_SEC_TO_VALUE(AP_MOUNT_ALEXMOS_SPEED);
|
2022-06-23 00:38:12 -03:00
|
|
|
outgoing_buffer.angle_speed.angle_roll = DEGREE_TO_VALUE(degrees(angle_target_rad.roll));
|
2015-01-16 18:13:17 -04:00
|
|
|
outgoing_buffer.angle_speed.speed_pitch = DEGREE_PER_SEC_TO_VALUE(AP_MOUNT_ALEXMOS_SPEED);
|
2022-06-23 00:38:12 -03:00
|
|
|
outgoing_buffer.angle_speed.angle_pitch = DEGREE_TO_VALUE(degrees(angle_target_rad.pitch));
|
2015-01-16 18:13:17 -04:00
|
|
|
outgoing_buffer.angle_speed.speed_yaw = DEGREE_PER_SEC_TO_VALUE(AP_MOUNT_ALEXMOS_SPEED);
|
2023-03-25 00:50:56 -03:00
|
|
|
outgoing_buffer.angle_speed.angle_yaw = DEGREE_TO_VALUE(angle_target_rad.get_bf_yaw());
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_CONTROL, (uint8_t *)&outgoing_buffer.angle_speed, sizeof(alexmos_angles_speed));
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read current profile profile_id and global parameters from the gimbal settings
|
|
|
|
*/
|
2015-06-05 00:09:12 -03:00
|
|
|
void AP_Mount_Alexmos::read_params(uint8_t profile_id)
|
|
|
|
{
|
2015-01-15 18:46:15 -04:00
|
|
|
uint8_t data[1] = {(uint8_t) profile_id};
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_READ_PARAMS, data, 1);
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write new parameters to the gimbal settings
|
|
|
|
*/
|
2015-01-16 06:53:54 -04:00
|
|
|
void AP_Mount_Alexmos::write_params()
|
|
|
|
{
|
|
|
|
if (!_param_read_once) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-05 00:09:12 -03:00
|
|
|
send_command(CMD_WRITE_PARAMS, (uint8_t *)&_current_parameters.params, sizeof(alexmos_params));
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-16 06:53:54 -04:00
|
|
|
send a command to the Alemox Serial API
|
2015-01-15 18:46:15 -04:00
|
|
|
*/
|
2015-01-16 06:53:54 -04:00
|
|
|
void AP_Mount_Alexmos::send_command(uint8_t cmd, uint8_t* data, uint8_t size)
|
|
|
|
{
|
2016-08-02 10:42:50 -03:00
|
|
|
if (_port->txspace() < (size + 5U)) {
|
2015-01-16 06:53:54 -04:00
|
|
|
return;
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
_port->write( '>' );
|
|
|
|
_port->write( cmd ); // write command id
|
|
|
|
_port->write( size ); // write body size
|
|
|
|
_port->write( cmd+size ); // write header checkum
|
|
|
|
|
2015-01-16 06:53:54 -04:00
|
|
|
for (uint8_t i = 0; i != size ; i++) {
|
|
|
|
checksum += data[i];
|
2015-06-05 00:09:12 -03:00
|
|
|
_port->write( data[i] );
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
2015-06-05 00:09:12 -03:00
|
|
|
_port->write(checksum);
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the body of the message received from the Alexmos gimbal
|
2015-01-16 06:53:54 -04:00
|
|
|
*/
|
|
|
|
void AP_Mount_Alexmos::parse_body()
|
|
|
|
{
|
|
|
|
switch (_command_id ) {
|
|
|
|
case CMD_BOARD_INFO:
|
|
|
|
_board_version = _buffer.version._board_version/ 10;
|
2022-03-11 13:36:59 -04:00
|
|
|
_current_firmware_version = _buffer.version._firmware_version * 0.001f ;
|
2015-01-16 06:53:54 -04:00
|
|
|
_firmware_beta_version = _buffer.version._firmware_version % 10 ;
|
2015-06-05 00:09:12 -03:00
|
|
|
_gimbal_3axis = (_buffer.version._board_features & 0x1);
|
|
|
|
_gimbal_bat_monitoring = (_buffer.version._board_features & 0x2);
|
2015-01-16 06:53:54 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_GET_ANGLES:
|
|
|
|
_current_angle.x = VALUE_TO_DEGREE(_buffer.angles.angle_roll);
|
|
|
|
_current_angle.y = VALUE_TO_DEGREE(_buffer.angles.angle_pitch);
|
|
|
|
_current_angle.z = VALUE_TO_DEGREE(_buffer.angles.angle_yaw);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_READ_PARAMS:
|
|
|
|
_param_read_once = true;
|
|
|
|
_current_parameters.params = _buffer.params;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_WRITE_PARAMS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
_last_command_confirmed = true;
|
|
|
|
break;
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* detect and read the header of the incoming message from the gimbal
|
|
|
|
*/
|
2015-01-16 06:53:54 -04:00
|
|
|
void AP_Mount_Alexmos::read_incoming()
|
|
|
|
{
|
|
|
|
uint8_t data;
|
2015-01-17 04:59:58 -04:00
|
|
|
int16_t numc;
|
|
|
|
|
|
|
|
numc = _port->available();
|
2015-01-19 05:12:41 -04:00
|
|
|
|
2023-11-04 06:03:14 -03:00
|
|
|
if (numc < 0 ) {
|
2015-01-19 05:12:41 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-05 00:09:12 -03:00
|
|
|
|
2015-01-17 04:59:58 -04:00
|
|
|
for (int16_t i = 0; i < numc; i++) { // Process bytes received
|
2015-01-16 06:53:54 -04:00
|
|
|
data = _port->read();
|
|
|
|
switch (_step) {
|
|
|
|
case 0:
|
|
|
|
if ( '>' == data) {
|
|
|
|
_step = 1;
|
|
|
|
_checksum = 0; //reset checksum accumulator
|
|
|
|
_last_command_confirmed = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // command ID
|
|
|
|
_checksum = data;
|
|
|
|
_command_id = data;
|
|
|
|
_step++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // Size of the body of the message
|
|
|
|
_checksum += data;
|
|
|
|
_payload_length = data;
|
|
|
|
_step++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // checksum of the header
|
|
|
|
if (_checksum != data ) {
|
|
|
|
_step = 0;
|
|
|
|
_checksum = 0;
|
|
|
|
// checksum error
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_step++;
|
|
|
|
_checksum = 0;
|
|
|
|
_payload_counter = 0; // prepare to receive payload
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // parsing body
|
|
|
|
_checksum += data;
|
|
|
|
if (_payload_counter < sizeof(_buffer)) {
|
2016-06-06 10:50:53 -03:00
|
|
|
_buffer[_payload_counter] = data;
|
2015-01-16 06:53:54 -04:00
|
|
|
}
|
|
|
|
if (++_payload_counter == _payload_length)
|
|
|
|
_step++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:// body checksum
|
|
|
|
_step = 0;
|
|
|
|
if (_checksum != data) {
|
|
|
|
break;
|
|
|
|
}
|
2015-06-05 00:09:12 -03:00
|
|
|
parse_body();
|
2015-01-16 06:53:54 -04:00
|
|
|
}
|
2015-01-15 18:46:15 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 01:54:57 -03:00
|
|
|
#endif // HAL_MOUNT_ALEXMOS_ENABLED
|