Skip to content

Commit 8e04c1f

Browse files
committed
SRV_Channel:add ap periph servo power output currect sampling and dronecan telemary support
1 parent 5e0efd3 commit 8e04c1f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

libraries/SRV_Channel/SRV_Channel.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,50 @@ const AP_Param::GroupInfo SRV_Channel::var_info[] = {
171171
// @RebootRequired: True
172172
AP_GROUPINFO("FUNCTION", 5, SRV_Channel, function, 0),
173173

174+
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
175+
// @Param: TELEM_RATE
176+
// @DisplayName: Actuator Telemetry rate
177+
// @Description: Actuator Telemetry update rate in Hz for this servo channel.
178+
// @Units: Hz
179+
// @Range: 1 100
180+
// @User: Standard
181+
AP_GROUPINFO("TELEM_RATE", 6, SRV_Channel, _telem_rate, AP_PERIPH_ACTUATOR_TELEM_RATE_DEFAULT),
182+
183+
// @Param: CURR_PIN
184+
// @DisplayName: Current sensing pin
185+
// @Description: Analog input pin number for current sensing on this servo channel. Set to -1 to disable current sensing.
186+
// @Values: -1:Disabled
187+
// @Range: -1 15
188+
// @User: Standard
189+
// @RebootRequired: True
190+
AP_GROUPINFO("CURR_PIN", 7, SRV_Channel, _curr_pin, -1),
191+
192+
// @Param: AMP_OFFSET
193+
// @DisplayName: Current sensor offset
194+
// @Description: Voltage offset at zero current on the current sensor for this servo channel. Used to calibrate analog current sensors.
195+
// @Units: V
196+
// @Range: -10 10
197+
// @User: Standard
198+
AP_GROUPINFO("AMP_OFFSET", 8, SRV_Channel, _curr_amp_offset, 0),
199+
200+
// @Param: AMP_PERVLT
201+
// @DisplayName: Amps per volt
202+
// @Description: Number of amps that a 1V reading on the current sensor corresponds to for this servo channel. This is the current sensor scale factor.
203+
// @Units: A/V
204+
// @Range: 0 100
205+
// @User: Standard
206+
AP_GROUPINFO("AMP_PERVLT", 9, SRV_Channel, _curr_amp_per_volt, 0),
207+
208+
209+
// @Param: CURR_MAX
210+
// @DisplayName: Maximum current
211+
// @Description: Maximum expected current draw for this servo channel in amps. Used for scaling and safety limits.
212+
// @Units: A
213+
// @Range: 0 100
214+
// @User: Standard
215+
AP_GROUPINFO("CURR_MAX", 10, SRV_Channel, _curr_max, 0),
216+
#endif
217+
174218
AP_GROUPEND
175219
};
176220

@@ -399,3 +443,33 @@ int8_t SRV_Channel::get_motor_num(void) const
399443
}
400444
return -1;
401445
}
446+
447+
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
448+
// return the current in amps for this channel
449+
float SRV_Channel::get_current_ampere(void) const
450+
{
451+
// Check if ADC channel is configured
452+
if (_curr_pin > 0 && _curr_pin_analog_source == nullptr) {
453+
_curr_pin_analog_source = hal.analogin->channel(_curr_pin);
454+
if (_curr_pin_analog_source == nullptr) {
455+
return 0.0f;
456+
}
457+
if (!_curr_pin_analog_source->set_pin(_curr_pin)) {
458+
return 0.0f;
459+
}
460+
} else if (_curr_pin <= 0 || _curr_pin_analog_source == nullptr) {
461+
return 0.0f;
462+
}
463+
464+
float adc_voltage = _curr_pin_analog_source->voltage_average();
465+
float current = (adc_voltage - _curr_amp_offset) * _curr_amp_per_volt;
466+
467+
return current;
468+
}
469+
470+
//return preset maximum current in amps for this channel
471+
float SRV_Channel::get_current_max_ampere(void) const
472+
{
473+
return _curr_max;
474+
}
475+
#endif

libraries/SRV_Channel/SRV_Channel.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ class SRV_Channel {
313313
// used by DO_SET_SERVO commands
314314
void ignore_small_rcin_changes() { ign_small_rcin_changes = true; }
315315

316+
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
317+
// get current in amperes by reading ADC
318+
float get_current_ampere(void) const;
319+
// get maximum expected current in amperes
320+
float get_current_max_ampere(void) const;
321+
// get telemetry update rate in Hz
322+
int16_t get_telem_rate(void) const { return _telem_rate; }
323+
#endif
324+
316325
private:
317326
AP_Int16 servo_min;
318327
AP_Int16 servo_max;
@@ -321,6 +330,15 @@ class SRV_Channel {
321330
AP_Int8 reversed;
322331
AP_Enum16<Function> function;
323332

333+
#if AP_PERIPH_ACTUATOR_TELEM_ENABLED
334+
AP_Int16 _telem_rate; // telemetry update rate in Hz
335+
AP_Int8 _curr_pin; // current sensor ADC channel
336+
AP_Float _curr_amp_offset; // current sensor offset
337+
AP_Float _curr_amp_per_volt; // current sensor scale in amps per volt
338+
AP_Float _curr_max; // maximum expected current in amps
339+
mutable AP_HAL::AnalogSource *_curr_pin_analog_source; // analog source for current (mutable for lazy init)
340+
#endif
341+
324342
// a pending output value as PWM
325343
uint16_t output_pwm;
326344

libraries/SRV_Channel/SRV_Channel_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@
1919
#define NUM_SERVO_CHANNELS 16
2020
#endif
2121
#endif
22+
23+
#ifndef AP_PERIPH_ACTUATOR_TELEM_ENABLED
24+
#define AP_PERIPH_ACTUATOR_TELEM_ENABLED 0
25+
#endif
26+
27+
#ifndef AP_PERIPH_ACTUATOR_TELEM_RATE_DEFAULT
28+
#define AP_PERIPH_ACTUATOR_TELEM_RATE_DEFAULT 25
29+
#endif

0 commit comments

Comments
 (0)