Skip to content

Commit ca112b4

Browse files
committed
AP_Periph: move actator telem to use servo telem lib
1 parent 5b17863 commit ca112b4

File tree

4 files changed

+38
-62
lines changed

4 files changed

+38
-62
lines changed

Tools/AP_Periph/AP_Periph.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,6 @@ void AP_Periph_FW::update()
513513
rcin_update();
514514
#endif
515515

516-
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
517-
actuator_telem.update();
518-
#endif
519-
520516
#if AP_PERIPH_BATTERY_BALANCE_ENABLED
521517
batt_balance_update();
522518
#endif

Tools/AP_Periph/Servo_telem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ void AP_Periph_FW::servo_telem_update()
1212

1313
// Run update function at 50hz mirroring vehicle update rate
1414
if (now_ms - servo_telem.last_update_ms > 20) {
15+
// Update periph only actuator telem
16+
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
17+
actuator_telem.update();
18+
#endif
19+
20+
// Update main servo telem lib
1521
servo_telem.lib.update();
1622
servo_telem.last_update_ms = now_ms;
1723
}

Tools/AP_Periph/actuator_telem.cpp

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ extern const AP_HAL::HAL &hal;
4545
#define AP_PERIPH_ACTUATOR_TELEM_CURR_MAX_DEFAULT 2.5f
4646
#endif
4747

48+
#if AP_SERVO_TELEM_ENABLED == 0
49+
#error "AP_PERIPH_ACTUATOR_TELEM_ENABLED requires AP_SERVO_TELEM_ENABLED"
50+
#endif
51+
4852
const AP_Param::GroupInfo ActuatorTelem::var_info[] = {
49-
// @Param: _TELEM_RATE
50-
// @DisplayName: Actuator Telemetry rate
51-
// @Description: Actuator Telemetry update rate in Hz. Set to 0 to disable.
52-
// @Units: Hz
53-
// @Range: 0 100
54-
// @User: Standard
55-
AP_GROUPINFO("_TELEM_RATE", 1, ActuatorTelem, rate, AP_PERIPH_ACTUATOR_TELEM_RATE_DEFAULT),
5653

5754
// @Param: _NUM_CHANS
5855
// @DisplayName: Number of actuator channels
@@ -129,71 +126,51 @@ void ActuatorTelem::send_telemetry(uint8_t channel_index, uint8_t actuator_id)
129126
return;
130127
}
131128
// Read current from analog input
132-
float current_amp = 0;
133-
float adc_voltage = source->voltage_average();
134-
current_amp = (adc_voltage - curr_amp_offset) * curr_amp_per_volt;
135-
136-
uavcan_equipment_actuator_Status pkt {};
137-
138-
pkt.actuator_id = actuator_id;
139-
pkt.position = nanf(""); // Not available
140-
pkt.force = nanf(""); // Not available
141-
pkt.speed = nanf(""); // Not available
129+
const float adc_voltage = source->voltage_average();
130+
const float current_amp = (adc_voltage - curr_amp_offset) * curr_amp_per_volt;
142131

143132
// Calculate power rating percentage from current
144133
const float max_current = curr_max.get();
145-
if (max_current > 0 && current_amp >= 0) {
146-
pkt.power_rating_pct = constrain_int16(current_amp / max_current * 100.0f, 0, 100);
147-
} else {
148-
pkt.power_rating_pct = UAVCAN_EQUIPMENT_ACTUATOR_STATUS_POWER_RATING_PCT_UNKNOWN;
134+
uint8_t duty_cycle = 0;
135+
const bool have_duty_cycle = (max_current > 0) && (current_amp >= 0);
136+
if (have_duty_cycle) {
137+
duty_cycle = constrain_float(current_amp / max_current * 100.0f, 0, 100);
149138
}
150139

151-
// encode and broadcast
152-
uint8_t buffer[UAVCAN_EQUIPMENT_ACTUATOR_STATUS_MAX_SIZE];
153-
uint16_t total_size = uavcan_equipment_actuator_Status_encode(&pkt, buffer, !periph.canfdout());
140+
const AP_Servo_Telem::TelemetryData telem_data {
141+
.current = current_amp,
142+
.duty_cycle = duty_cycle,
143+
.present_types = AP_Servo_Telem::TelemetryData::Types::CURRENT |
144+
have_duty_cycle ? AP_Servo_Telem::TelemetryData::Types::DUTY_CYCLE : 0
145+
};
146+
147+
periph.servo_telem.lib.update_telem_data(channel_index, telem_data);
154148

155-
periph.canard_broadcast(UAVCAN_EQUIPMENT_ACTUATOR_STATUS_SIGNATURE,
156-
UAVCAN_EQUIPMENT_ACTUATOR_STATUS_ID,
157-
CANARD_TRANSFER_PRIORITY_LOW,
158-
&buffer[0],
159-
total_size);
160149
}
161150

162151
void ActuatorTelem::update()
163152
{
164-
// Check global telemetry rate
165-
const float telem_rate = rate.get();
166-
if (telem_rate <= 0) {
167-
return; // telemetry disabled
168-
}
169-
170-
const uint32_t now = AP_HAL::millis();
171-
if (now - last_telem_update_ms < 1000.0f / telem_rate) {
153+
const int8_t num_chans_val = num_chans.get();
154+
if (num_chans_val <= 0) {
172155
return;
173156
}
174-
last_telem_update_ms = now;
175157

176-
#if HAL_PWM_COUNT > 0
177-
const int8_t num_chans_val = num_chans.get();
178-
if (num_chans_val > 0) {
179-
for (uint8_t i = 0; i < MIN(HAL_PWM_COUNT, num_chans_val); i++) {
180-
const auto *srv_channel = periph.servo_channels.srv_channel(i);
181-
if (srv_channel == nullptr) {
182-
continue;
183-
}
158+
for (uint8_t i = 0; i < MIN(HAL_PWM_COUNT, num_chans_val); i++) {
159+
const auto *srv_channel = periph.servo_channels.srv_channel(i);
160+
if (srv_channel == nullptr) {
161+
continue;
162+
}
184163

185-
const SRV_Channel::Function function = srv_channel->get_function();
186-
// Only send for configured actuator functions
187-
if (function < SRV_Channel::k_rcin1 || function > SRV_Channel::k_rcin16) {
188-
continue;
189-
}
164+
const SRV_Channel::Function function = srv_channel->get_function();
165+
// Only send for configured actuator functions
166+
if (function < SRV_Channel::k_rcin1 || function > SRV_Channel::k_rcin16) {
167+
continue;
168+
}
190169

191-
const uint8_t actuator_id = function - SRV_Channel::k_rcin1 + 1;
170+
const uint8_t actuator_id = function - SRV_Channel::k_rcin1 + 1;
192171

193-
send_telemetry(i, actuator_id);
194-
}
172+
send_telemetry(i, actuator_id);
195173
}
196-
#endif
197174
}
198175

199176
#endif // AP_PERIPH_ACTUATOR_TELEM_ENABLED

Tools/AP_Periph/actuator_telem.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// Manager for all actuator channels
1010
class ActuatorTelem {
1111
public:
12-
friend class AP_Periph_FW;
1312
ActuatorTelem(void);
1413

1514
void init(void);
@@ -19,13 +18,11 @@ class ActuatorTelem {
1918
static const struct AP_Param::GroupInfo var_info[];
2019

2120
private:
22-
AP_Int16 rate;
2321
AP_Int8 num_chans;
2422
AP_Int8 curr_pin1;
2523
AP_Float curr_amp_offset;
2624
AP_Float curr_amp_per_volt;
2725
AP_Float curr_max;
28-
uint32_t last_telem_update_ms;
2926
AP_HAL::AnalogSource *analog_sources[HAL_ACTUATOR_TELEM_CURR_MAX_CHANNELS];
3027
};
3128

0 commit comments

Comments
 (0)