Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/AntennaTracker/mode.cpp
Views: 1798
#include "mode.h"12#include "Tracker.h"34void Mode::update_auto(void)5{6struct Tracker::NavStatus &nav_status = tracker.nav_status;78Parameters &g = tracker.g;910float yaw = wrap_180_cd((nav_status.bearing+g.yaw_trim)*100); // target yaw in centidegrees11float pitch = constrain_float(nav_status.pitch+g.pitch_trim, g.pitch_min, g.pitch_max) * 100; // target pitch in centidegrees1213bool direction_reversed = get_ef_yaw_direction();1415calc_angle_error(pitch, yaw, direction_reversed);1617float bf_pitch;18float bf_yaw;19convert_ef_to_bf(pitch, yaw, bf_pitch, bf_yaw);2021// only move servos if target is at least distance_min away if we have a target22if ((g.distance_min <= 0) || (nav_status.distance >= g.distance_min) || !tracker.vehicle.location_valid) {23tracker.update_pitch_servo(bf_pitch);24tracker.update_yaw_servo(bf_yaw);25}26}2728void Mode::update_scan(void)29{30struct Tracker::NavStatus &nav_status = tracker.nav_status;3132Parameters &g = tracker.g;3334if (!nav_status.manual_control_yaw) {35float yaw_delta = g.scan_speed_yaw * 0.02f;36nav_status.bearing += yaw_delta * (nav_status.scan_reverse_yaw?-1:1);37if (nav_status.bearing < 0 && nav_status.scan_reverse_yaw) {38nav_status.scan_reverse_yaw = false;39}40if (nav_status.bearing > 360 && !nav_status.scan_reverse_yaw) {41nav_status.scan_reverse_yaw = true;42}43nav_status.bearing = constrain_float(nav_status.bearing, 0, 360);44}4546if (!nav_status.manual_control_pitch) {47const float pitch_delta = g.scan_speed_pitch * 0.02f;48if (nav_status.scan_reverse_pitch) {49nav_status.pitch -= pitch_delta;50if (nav_status.pitch < g.pitch_min) {51nav_status.scan_reverse_pitch = false;52}53} else {54nav_status.pitch += pitch_delta;55if (nav_status.pitch > g.pitch_max) {56nav_status.scan_reverse_pitch = true;57}58}59nav_status.pitch = constrain_float(nav_status.pitch, g.pitch_min, g.pitch_max);60}6162update_auto();63}6465void Mode::calc_angle_error(float pitch, float yaw, bool direction_reversed)66{67// Pitch angle error in centidegrees68// Positive error means the target is above current pitch69// Negative error means the target is below current pitch70const AP_AHRS &ahrs = AP::ahrs();71float ahrs_pitch = ahrs.pitch_sensor;72int32_t ef_pitch_angle_error = pitch - ahrs_pitch;7374// Yaw angle error in centidegrees75// Positive error means the target is right of current yaw76// Negative error means the target is left of current yaw77int32_t ahrs_yaw_cd = wrap_180_cd(ahrs.yaw_sensor);78int32_t ef_yaw_angle_error = wrap_180_cd(yaw - ahrs_yaw_cd);79if (direction_reversed) {80if (ef_yaw_angle_error > 0) {81ef_yaw_angle_error = (yaw - ahrs_yaw_cd) - 36000;82} else {83ef_yaw_angle_error = 36000 + (yaw - ahrs_yaw_cd);84}85}8687// earth frame to body frame angle error conversion88float bf_pitch_err;89float bf_yaw_err;90convert_ef_to_bf(ef_pitch_angle_error, ef_yaw_angle_error, bf_pitch_err, bf_yaw_err);91struct Tracker::NavStatus &nav_status = tracker.nav_status;92nav_status.angle_error_pitch = bf_pitch_err;93nav_status.angle_error_yaw = bf_yaw_err;9495// set actual and desired for logging, note we are using angles not rates96Parameters &g = tracker.g;97g.pidPitch2Srv.set_target_rate(pitch * 0.01);98g.pidPitch2Srv.set_actual_rate(ahrs_pitch * 0.01);99g.pidYaw2Srv.set_target_rate(yaw * 0.01);100g.pidYaw2Srv.set_actual_rate(ahrs_yaw_cd * 0.01);101}102103void Mode::convert_ef_to_bf(float pitch, float yaw, float& bf_pitch, float& bf_yaw)104{105// earth frame to body frame pitch and yaw conversion106const AP_AHRS &ahrs = AP::ahrs();107bf_pitch = ahrs.cos_roll() * pitch + ahrs.sin_roll() * ahrs.cos_pitch() * yaw;108bf_yaw = -ahrs.sin_roll() * pitch + ahrs.cos_pitch() * ahrs.cos_roll() * yaw;109}110111bool Mode::convert_bf_to_ef(float pitch, float yaw, float& ef_pitch, float& ef_yaw)112{113const AP_AHRS &ahrs = AP::ahrs();114// avoid divide by zero115if (is_zero(ahrs.cos_pitch())) {116return false;117}118// convert earth frame angle or rates to body frame119ef_pitch = ahrs.cos_roll() * pitch - ahrs.sin_roll() * yaw;120ef_yaw = (ahrs.sin_roll() / ahrs.cos_pitch()) * pitch + (ahrs.cos_roll() / ahrs.cos_pitch()) * yaw;121return true;122}123124// return value is true if taking the long road to the target, false if normal, shortest direction should be used125bool Mode::get_ef_yaw_direction()126{127// calculating distances from current pitch/yaw to lower and upper limits in centi-degrees128Parameters &g = tracker.g;129float yaw_angle_limit_lower = (-g.yaw_range * 100.0f / 2.0f) - tracker.yaw_servo_out_filt.get();130float yaw_angle_limit_upper = (g.yaw_range * 100.0f / 2.0f) - tracker.yaw_servo_out_filt.get();131float pitch_angle_limit_lower = (g.pitch_min * 100.0f) - tracker.pitch_servo_out_filt.get();132float pitch_angle_limit_upper = (g.pitch_max * 100.0f) - tracker.pitch_servo_out_filt.get();133134// distances to earthframe angle limits in centi-degrees135float ef_yaw_limit_lower = yaw_angle_limit_lower;136float ef_yaw_limit_upper = yaw_angle_limit_upper;137float ef_pitch_limit_lower = pitch_angle_limit_lower;138float ef_pitch_limit_upper = pitch_angle_limit_upper;139convert_bf_to_ef(pitch_angle_limit_lower, yaw_angle_limit_lower, ef_pitch_limit_lower, ef_yaw_limit_lower);140convert_bf_to_ef(pitch_angle_limit_upper, yaw_angle_limit_upper, ef_pitch_limit_upper, ef_yaw_limit_upper);141142const AP_AHRS &ahrs = AP::ahrs();143float ef_yaw_current = wrap_180_cd(ahrs.yaw_sensor);144struct Tracker::NavStatus &nav_status = tracker.nav_status;145float ef_yaw_target = wrap_180_cd((nav_status.bearing+g.yaw_trim)*100);146float ef_yaw_angle_error = wrap_180_cd(ef_yaw_target - ef_yaw_current);147148// calculate angle error to target in both directions (i.e. moving up/right or lower/left)149float yaw_angle_error_upper;150float yaw_angle_error_lower;151if (ef_yaw_angle_error >= 0) {152yaw_angle_error_upper = ef_yaw_angle_error;153yaw_angle_error_lower = ef_yaw_angle_error - 36000;154} else {155yaw_angle_error_upper = 36000 + ef_yaw_angle_error;156yaw_angle_error_lower = ef_yaw_angle_error;157}158159// checks that the vehicle is outside the tracker's range160if ((yaw_angle_error_lower < ef_yaw_limit_lower) && (yaw_angle_error_upper > ef_yaw_limit_upper)) {161// if the tracker is trying to move clockwise to reach the vehicle,162// but the tracker could get closer to the vehicle by moving counter-clockwise then set direction_reversed to true163if (ef_yaw_angle_error>0 && ((ef_yaw_limit_lower - yaw_angle_error_lower) < (yaw_angle_error_upper - ef_yaw_limit_upper))) {164return true;165}166// if the tracker is trying to move counter-clockwise to reach the vehicle,167// but the tracker could get closer to the vehicle by moving then set direction_reversed to true168if (ef_yaw_angle_error<0 && ((ef_yaw_limit_lower - yaw_angle_error_lower) > (yaw_angle_error_upper - ef_yaw_limit_upper))) {169return true;170}171}172173// if we get this far we can use the regular, shortest path to the target174return false;175}176177178