2015-06-01 02:04:25 -03:00
|
|
|
#include "Tracker.h"
|
|
|
|
|
2015-04-17 21:45:26 -03:00
|
|
|
/*
|
2016-07-25 15:52:50 -03:00
|
|
|
* Code to move pitch and yaw servos to attain a target heading or pitch
|
2015-04-17 21:45:26 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
// init_servos - initialises the servos
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::init_servos()
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
2018-09-05 05:47:35 -03:00
|
|
|
// update assigned functions and enable auxiliary servos
|
|
|
|
SRV_Channels::enable_aux_servos();
|
|
|
|
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_default_function(CH_YAW, SRV_Channel::k_tracker_yaw);
|
|
|
|
SRV_Channels::set_default_function(CH_PITCH, SRV_Channel::k_tracker_pitch);
|
2015-04-17 21:45:26 -03:00
|
|
|
|
2017-01-06 22:51:56 -04:00
|
|
|
// yaw range is +/- (YAW_RANGE parameter/2) converted to centi-degrees
|
2019-06-13 14:16:29 -03:00
|
|
|
SRV_Channels::set_angle(SRV_Channel::k_tracker_yaw, g.yaw_range * 100/2);
|
2015-04-17 21:45:26 -03:00
|
|
|
|
2017-01-06 22:51:56 -04:00
|
|
|
// pitch range is +/- (PITCH_MIN/MAX parameters/2) converted to centi-degrees
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_angle(SRV_Channel::k_tracker_pitch, (-g.pitch_min+g.pitch_max) * 100/2);
|
2016-07-27 23:19:54 -03:00
|
|
|
|
2017-01-06 22:51:56 -04:00
|
|
|
SRV_Channels::calc_pwm();
|
|
|
|
SRV_Channels::output_ch_all();
|
2018-09-05 05:48:10 -03:00
|
|
|
|
2016-07-27 23:19:54 -03:00
|
|
|
yaw_servo_out_filt.set_cutoff_frequency(SERVO_OUT_FILT_HZ);
|
|
|
|
pitch_servo_out_filt.set_cutoff_frequency(SERVO_OUT_FILT_HZ);
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
update the pitch (elevation) servo. The aim is to drive the boards ahrs pitch to the
|
|
|
|
requested pitch, so the board (and therefore the antenna) will be pointing at the target
|
|
|
|
*/
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::update_pitch_servo(float pitch)
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
2016-05-23 04:54:47 -03:00
|
|
|
switch ((enum ServoType)g.servo_pitch_type.get()) {
|
2015-04-17 21:45:26 -03:00
|
|
|
case SERVO_TYPE_ONOFF:
|
|
|
|
update_pitch_onoff_servo(pitch);
|
|
|
|
break;
|
|
|
|
|
2015-06-03 11:05:58 -03:00
|
|
|
case SERVO_TYPE_CR:
|
|
|
|
update_pitch_cr_servo(pitch);
|
|
|
|
break;
|
|
|
|
|
2015-04-17 21:45:26 -03:00
|
|
|
case SERVO_TYPE_POSITION:
|
|
|
|
default:
|
2016-07-13 23:40:49 -03:00
|
|
|
update_pitch_position_servo();
|
2015-04-17 21:45:26 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
update the pitch (elevation) servo. The aim is to drive the boards ahrs pitch to the
|
|
|
|
requested pitch, so the board (and therefore the antenna) will be pointing at the target
|
|
|
|
*/
|
2016-07-13 23:40:49 -03:00
|
|
|
void Tracker::update_pitch_position_servo()
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
2016-07-01 00:40:01 -03:00
|
|
|
int32_t pitch_min_cd = g.pitch_min*100;
|
|
|
|
int32_t pitch_max_cd = g.pitch_max*100;
|
2015-04-17 21:45:26 -03:00
|
|
|
// Need to configure your servo so that increasing servo_out causes increase in pitch/elevation (ie pointing higher into the sky,
|
|
|
|
// above the horizon. On my antenna tracker this requires the pitch/elevation servo to be reversed
|
|
|
|
// param set RC2_REV -1
|
|
|
|
//
|
|
|
|
// The pitch servo (RC channel 2) is configured for servo_out of -9000-0-9000 servo_out,
|
|
|
|
// which will drive the servo from RC2_MIN to RC2_MAX usec pulse width.
|
|
|
|
// Therefore, you must set RC2_MIN and RC2_MAX so that your servo drives the antenna altitude between -90 to 90 exactly
|
|
|
|
// To drive my HS-645MG servos through their full 180 degrees of rotational range, I have to set:
|
|
|
|
// param set RC2_MAX 2540
|
|
|
|
// param set RC2_MIN 640
|
|
|
|
//
|
|
|
|
// You will also need to tune the pitch PID to suit your antenna and servos. I use:
|
|
|
|
// PITCH2SRV_P 0.100000
|
|
|
|
// PITCH2SRV_I 0.020000
|
|
|
|
// PITCH2SRV_D 0.000000
|
|
|
|
// PITCH2SRV_IMAX 4000.000000
|
|
|
|
|
|
|
|
// calculate new servo position
|
2021-09-18 14:54:05 -03:00
|
|
|
float new_servo_out = SRV_Channels::get_output_scaled(SRV_Channel::k_tracker_pitch) + g.pidPitch2Srv.update_error(nav_status.angle_error_pitch);
|
2015-04-17 21:45:26 -03:00
|
|
|
|
|
|
|
// position limit pitch servo
|
2016-07-27 23:19:54 -03:00
|
|
|
if (new_servo_out <= pitch_min_cd) {
|
|
|
|
new_servo_out = pitch_min_cd;
|
2016-06-16 00:07:07 -03:00
|
|
|
g.pidPitch2Srv.reset_I();
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
2016-07-27 23:19:54 -03:00
|
|
|
if (new_servo_out >= pitch_max_cd) {
|
|
|
|
new_servo_out = pitch_max_cd;
|
2016-06-16 00:07:07 -03:00
|
|
|
g.pidPitch2Srv.reset_I();
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
2016-07-27 23:19:54 -03:00
|
|
|
// rate limit pitch servo
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_pitch, new_servo_out);
|
2016-07-27 23:19:54 -03:00
|
|
|
|
|
|
|
if (pitch_servo_out_filt_init) {
|
|
|
|
pitch_servo_out_filt.apply(new_servo_out, G_Dt);
|
|
|
|
} else {
|
|
|
|
pitch_servo_out_filt.reset(new_servo_out);
|
|
|
|
pitch_servo_out_filt_init = true;
|
|
|
|
}
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
update the pitch (elevation) servo. The aim is to drive the boards ahrs pitch to the
|
|
|
|
requested pitch, so the board (and therefore the antenna) will be pointing at the target
|
|
|
|
*/
|
2021-02-01 12:26:21 -04:00
|
|
|
void Tracker::update_pitch_onoff_servo(float pitch) const
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
2016-07-01 00:40:01 -03:00
|
|
|
int32_t pitch_min_cd = g.pitch_min*100;
|
|
|
|
int32_t pitch_max_cd = g.pitch_max*100;
|
2015-04-17 21:45:26 -03:00
|
|
|
|
|
|
|
float acceptable_error = g.onoff_pitch_rate * g.onoff_pitch_mintime;
|
2016-07-13 23:40:49 -03:00
|
|
|
if (fabsf(nav_status.angle_error_pitch) < acceptable_error) {
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_pitch, 0);
|
2016-07-13 23:40:49 -03:00
|
|
|
} else if ((nav_status.angle_error_pitch > 0) && (pitch*100>pitch_min_cd)) {
|
|
|
|
// positive error means we are pointing too low, so push the
|
|
|
|
// servo up
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_pitch, -9000);
|
2016-07-27 23:19:54 -03:00
|
|
|
} else if (pitch*100<pitch_max_cd) {
|
2016-07-13 23:40:49 -03:00
|
|
|
// negative error means we are pointing too high, so push the
|
|
|
|
// servo down
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_pitch, 9000);
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 11:05:58 -03:00
|
|
|
/**
|
|
|
|
update the pitch for continuous rotation servo
|
|
|
|
*/
|
|
|
|
void Tracker::update_pitch_cr_servo(float pitch)
|
|
|
|
{
|
2019-06-27 06:36:31 -03:00
|
|
|
const float pitch_out = constrain_float(g.pidPitch2Srv.update_error(nav_status.angle_error_pitch), -(-g.pitch_min+g.pitch_max) * 100/2, (-g.pitch_min+g.pitch_max) * 100/2);
|
2019-03-10 07:49:28 -03:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_pitch, pitch_out);
|
2015-06-03 11:05:58 -03:00
|
|
|
}
|
|
|
|
|
2015-04-17 21:45:26 -03:00
|
|
|
/**
|
|
|
|
update the yaw (azimuth) servo.
|
|
|
|
*/
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::update_yaw_servo(float yaw)
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
2016-05-23 04:54:47 -03:00
|
|
|
switch ((enum ServoType)g.servo_yaw_type.get()) {
|
2015-04-17 21:45:26 -03:00
|
|
|
case SERVO_TYPE_ONOFF:
|
|
|
|
update_yaw_onoff_servo(yaw);
|
|
|
|
break;
|
|
|
|
|
2015-06-03 11:05:58 -03:00
|
|
|
case SERVO_TYPE_CR:
|
|
|
|
update_yaw_cr_servo(yaw);
|
|
|
|
break;
|
|
|
|
|
2015-04-17 21:45:26 -03:00
|
|
|
case SERVO_TYPE_POSITION:
|
|
|
|
default:
|
2016-07-13 23:40:49 -03:00
|
|
|
update_yaw_position_servo();
|
2015-04-17 21:45:26 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
update the yaw (azimuth) servo. The aim is to drive the boards ahrs
|
|
|
|
yaw to the requested yaw, so the board (and therefore the antenna)
|
|
|
|
will be pointing at the target
|
|
|
|
*/
|
2016-07-13 23:40:49 -03:00
|
|
|
void Tracker::update_yaw_position_servo()
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
|
|
|
int32_t yaw_limit_cd = g.yaw_range*100/2;
|
|
|
|
|
|
|
|
// Antenna as Ballerina. Use with antenna that do not have continuously rotating servos, ie at some point in rotation
|
|
|
|
// the servo limits are reached and the servo has to slew 360 degrees to the 'other side' to keep tracking.
|
|
|
|
//
|
|
|
|
// This algorithm accounts for the fact that the antenna mount may not be aligned with North
|
2016-05-12 14:06:58 -03:00
|
|
|
// (in fact, any alignment is permissible), and that the alignment may change (possibly rapidly) over time
|
2015-04-17 21:45:26 -03:00
|
|
|
// (as when the antenna is mounted on a moving, turning vehicle)
|
|
|
|
//
|
2021-10-22 06:01:25 -03:00
|
|
|
// With my antenna mount, large pwm output drives the antenna anticlockwise, so need:
|
2015-04-17 21:45:26 -03:00
|
|
|
// param set RC1_REV -1
|
|
|
|
// to reverse the servo. Yours may be different
|
|
|
|
//
|
|
|
|
// You MUST set RC1_MIN and RC1_MAX so that your servo drives the antenna azimuth from -180 to 180 relative
|
|
|
|
// to the mount.
|
|
|
|
// To drive my HS-645MG servos through their full 180 degrees of rotational range and therefore the
|
|
|
|
// antenna through a full 360 degrees, I have to set:
|
|
|
|
// param set RC1_MAX 2380
|
|
|
|
// param set RC1_MIN 680
|
|
|
|
// According to the specs at https://www.servocity.com/html/hs-645mg_ultra_torque.html,
|
|
|
|
// that should be 600 through 2400, but the azimuth gearing in my antenna pointer is not exactly 2:1
|
|
|
|
|
|
|
|
/*
|
2016-07-13 23:40:49 -03:00
|
|
|
a positive error means that we need to rotate clockwise
|
|
|
|
a negative error means that we need to rotate counter-clockwise
|
2015-04-17 21:45:26 -03:00
|
|
|
|
|
|
|
Use our current yawspeed to determine if we are moving in the
|
|
|
|
right direction
|
|
|
|
*/
|
|
|
|
|
2019-06-27 06:36:31 -03:00
|
|
|
float servo_change = g.pidYaw2Srv.update_error(nav_status.angle_error_yaw);
|
2016-07-27 23:19:54 -03:00
|
|
|
servo_change = constrain_float(servo_change, -18000, 18000);
|
2017-01-08 18:30:37 -04:00
|
|
|
float new_servo_out = constrain_float(SRV_Channels::get_output_scaled(SRV_Channel::k_tracker_yaw) + servo_change, -18000, 18000);
|
2015-04-17 21:45:26 -03:00
|
|
|
|
2016-07-27 23:19:54 -03:00
|
|
|
// position limit yaw servo
|
|
|
|
if (new_servo_out <= -yaw_limit_cd) {
|
|
|
|
new_servo_out = -yaw_limit_cd;
|
|
|
|
g.pidYaw2Srv.reset_I();
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
2016-07-27 23:19:54 -03:00
|
|
|
if (new_servo_out >= yaw_limit_cd) {
|
|
|
|
new_servo_out = yaw_limit_cd;
|
2015-04-17 21:45:26 -03:00
|
|
|
g.pidYaw2Srv.reset_I();
|
|
|
|
}
|
|
|
|
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_yaw, new_servo_out);
|
2015-04-17 21:45:26 -03:00
|
|
|
|
2016-12-13 22:06:30 -04:00
|
|
|
if (yaw_servo_out_filt_init) {
|
2016-07-27 23:19:54 -03:00
|
|
|
yaw_servo_out_filt.apply(new_servo_out, G_Dt);
|
|
|
|
} else {
|
|
|
|
yaw_servo_out_filt.reset(new_servo_out);
|
|
|
|
yaw_servo_out_filt_init = true;
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
update the yaw (azimuth) servo. The aim is to drive the boards ahrs
|
|
|
|
yaw to the requested yaw, so the board (and therefore the antenna)
|
|
|
|
will be pointing at the target
|
|
|
|
*/
|
2021-02-01 12:26:21 -04:00
|
|
|
void Tracker::update_yaw_onoff_servo(float yaw) const
|
2015-04-17 21:45:26 -03:00
|
|
|
{
|
|
|
|
float acceptable_error = g.onoff_yaw_rate * g.onoff_yaw_mintime;
|
2016-07-13 23:40:49 -03:00
|
|
|
if (fabsf(nav_status.angle_error_yaw * 0.01f) < acceptable_error) {
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_yaw, 0);
|
2016-07-13 23:40:49 -03:00
|
|
|
} else if (nav_status.angle_error_yaw * 0.01f > 0) {
|
|
|
|
// positive error means we are counter-clockwise of the target, so
|
2015-04-17 21:45:26 -03:00
|
|
|
// move clockwise
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_yaw, 18000);
|
2016-07-13 23:40:49 -03:00
|
|
|
} else {
|
|
|
|
// negative error means we are clockwise of the target, so
|
|
|
|
// move counter-clockwise
|
2017-01-08 18:30:37 -04:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_yaw, -18000);
|
2015-04-17 21:45:26 -03:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 11:05:58 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
update the yaw continuous rotation servo
|
|
|
|
*/
|
|
|
|
void Tracker::update_yaw_cr_servo(float yaw)
|
|
|
|
{
|
2019-06-27 06:36:31 -03:00
|
|
|
const float yaw_out = constrain_float(-g.pidYaw2Srv.update_error(nav_status.angle_error_yaw), -g.yaw_range * 100/2, g.yaw_range * 100/2);
|
2019-03-10 07:49:28 -03:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_tracker_yaw, yaw_out);
|
2015-06-03 11:05:58 -03:00
|
|
|
}
|