123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "AP_Compass.h"
- #include <GCS_MAVLink/GCS.h>
- #include <SRV_Channel/SRV_Channel.h>
- extern const AP_HAL::HAL &hal;
- const AP_Param::GroupInfo Compass_PerMotor::var_info[] = {
-
-
-
-
-
- AP_GROUPINFO_FLAGS("_EN", 1, Compass_PerMotor, enable, 0, AP_PARAM_FLAG_ENABLE),
-
-
-
-
-
-
- AP_GROUPINFO("_EXP", 2, Compass_PerMotor, expo, 0.65),
-
-
-
-
-
-
-
-
-
-
-
-
- AP_GROUPINFO("1", 3, Compass_PerMotor, compensation[0], 0),
-
-
-
-
-
-
-
-
-
-
-
-
- AP_GROUPINFO("2", 4, Compass_PerMotor, compensation[1], 0),
-
-
-
-
-
-
-
-
-
-
-
-
- AP_GROUPINFO("3", 5, Compass_PerMotor, compensation[2], 0),
-
-
-
-
-
-
-
-
-
-
-
-
- AP_GROUPINFO("4", 6, Compass_PerMotor, compensation[3], 0),
-
- AP_GROUPEND
- };
- Compass_PerMotor::Compass_PerMotor(Compass &_compass) :
- compass(_compass)
- {
- AP_Param::setup_object_defaults(this, var_info);
- }
- float Compass_PerMotor::scaled_output(uint8_t motor)
- {
- if (!have_motor_map) {
- if (SRV_Channels::find_channel(SRV_Channel::k_motor1, motor_map[0]) &&
- SRV_Channels::find_channel(SRV_Channel::k_motor2, motor_map[1]) &&
- SRV_Channels::find_channel(SRV_Channel::k_motor3, motor_map[2]) &&
- SRV_Channels::find_channel(SRV_Channel::k_motor4, motor_map[3])) {
- have_motor_map = true;
- }
- }
- if (!have_motor_map) {
- return 0;
- }
-
-
- uint16_t pwm = hal.rcout->read_last_sent(motor_map[motor]);
-
- float output = (hal.rcout->scale_esc_to_unity(pwm)+1) * 0.5f;
- if (output <= 0) {
- return 0;
- }
-
-
- output *= voltage;
-
- output = powf(output, expo);
- return output;
- }
- void Compass_PerMotor::calibration_start(void)
- {
- for (uint8_t i=0; i<4; i++) {
- field_sum[i].zero();
- output_sum[i] = 0;
- count[i] = 0;
- start_ms[i] = 0;
- }
-
-
-
- for (uint8_t i=0; i<4; i++) {
- compass.read();
- hal.scheduler->delay(50);
- }
-
- base_field = compass.get_field(0);
- running = true;
- }
- void Compass_PerMotor::calibration_update(void)
- {
- uint32_t now = AP_HAL::millis();
-
-
- for (uint8_t i=0; i<4; i++) {
- float output = scaled_output(i);
- if (output <= 0) {
-
- start_ms[i] = 0;
- continue;
- }
- if (start_ms[i] == 0) {
- start_ms[i] = now;
- }
- if (now - start_ms[i] < 500) {
-
- continue;
- }
-
- field_sum[i] += compass.get_field(0);
- output_sum[i] += output;
- count[i]++;
- }
- }
- void Compass_PerMotor::calibration_end(void)
- {
- for (uint8_t i=0; i<4; i++) {
- if (count[i] == 0) {
- continue;
- }
-
- float output = output_sum[i] / count[i];
-
- Vector3f field_change = base_field - (field_sum[i] / count[i]);
- if (output <= 0) {
- continue;
- }
-
- Vector3f c = field_change / output;
- compensation[i].set_and_save(c);
- }
-
- enable.set_and_save(1);
-
- running = false;
- }
- void Compass_PerMotor::compensate(Vector3f &offset)
- {
- offset.zero();
- if (running) {
-
- return;
- }
- for (uint8_t i=0; i<4; i++) {
- float output = scaled_output(i);
- const Vector3f &c = compensation[i].get();
- offset += c * output;
- }
- }
|