ADC: replace ioctl with uorb message (#14087)

This commit is contained in:
SalimTerryLi
2020-03-20 18:23:32 +08:00
committed by GitHub
parent 40af5b0fbe
commit dc8e775d8f
30 changed files with 345 additions and 486 deletions

View File

@@ -144,10 +144,10 @@ private:
DataValidator _airspeed_validator; /**< data validator to monitor airspeed */
#ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL
int _adc_fd {-1}; /**< ADC driver handle */
hrt_abstime _last_adc{0}; /**< last time we took input from the ADC */
uORB::Subscription _adc_report_sub{ORB_ID(adc_report)}; /**< adc_report sub */
differential_pressure_s _diff_pres {};
uORB::PublicationMulti<differential_pressure_s> _diff_pres_pub{ORB_ID(differential_pressure)}; /**< differential_pressure */
#endif /* ADC_AIRSPEED_VOLTAGE_CHANNEL */
@@ -183,11 +183,6 @@ private:
*/
void parameter_update_poll(bool forced = false);
/**
* Do adc-related initialisation.
*/
int adc_init();
/**
* Poll the ADC and update readings to suit.
*
@@ -254,23 +249,6 @@ int Sensors::parameters_update()
return PX4_OK;
}
int Sensors::adc_init()
{
if (!_hil_enabled) {
#ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL
_adc_fd = px4_open(ADC0_DEVICE_PATH, O_RDONLY);
if (_adc_fd == -1) {
PX4_ERR("no ADC found: %s", ADC0_DEVICE_PATH);
return PX4_ERROR;
}
#endif // ADC_AIRSPEED_VOLTAGE_CHANNEL
}
return OK;
}
void Sensors::diff_pres_poll()
{
differential_pressure_s diff_pres{};
@@ -365,37 +343,40 @@ Sensors::parameter_update_poll(bool forced)
void Sensors::adc_poll()
{
#ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL
/* only read if not in HIL mode */
if (_hil_enabled) {
return;
}
#ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL
if (_parameters.diff_pres_analog_scale > 0.0f) {
hrt_abstime t = hrt_absolute_time();
/* rate limit to 100 Hz */
if (t - _last_adc >= 10000) {
/* make space for a maximum of twelve channels (to ensure reading all channels at once) */
px4_adc_msg_t buf_adc[PX4_MAX_ADC_CHANNELS];
/* read all channels available */
int ret = px4_read(_adc_fd, &buf_adc, sizeof(buf_adc));
if (ret >= (int)sizeof(buf_adc[0])) {
adc_report_s adc;
if (_adc_report_sub.update(&adc)) {
/* Read add channels we got */
for (unsigned i = 0; i < ret / sizeof(buf_adc[0]); i++) {
if (ADC_AIRSPEED_VOLTAGE_CHANNEL == buf_adc[i].am_channel) {
for (unsigned i = 0; i < PX4_MAX_ADC_CHANNELS; i++) {
if (adc.channel_id[i] == -1) {
continue; // skip non-exist channels
}
if (ADC_AIRSPEED_VOLTAGE_CHANNEL == adc.channel_id[i]) {
/* calculate airspeed, raw is the difference from */
const float voltage = (float)(buf_adc[i].am_data) * 3.3f / 4096.0f * 2.0f; // V_ref/4096 * (voltage divider factor)
const float voltage = (float)(adc.raw_data[i]) * adc.v_ref / adc.resolution * ADC_DP_V_DIV;
/**
* The voltage divider pulls the signal down, only act on
* a valid voltage from a connected sensor. Also assume a non-
* zero offset from the sensor if its connected.
*
* Notice: This won't work on devices which have PGA controlled
* vref. Those devices require no divider at all.
*/
if (voltage > 0.4f) {
const float diff_pres_pa_raw = voltage * _parameters.diff_pres_analog_scale - _parameters.diff_pres_offset_pa;
@@ -410,9 +391,9 @@ void Sensors::adc_poll()
}
}
}
_last_adc = t;
}
_last_adc = t;
}
}
@@ -465,7 +446,6 @@ void Sensors::Run()
// run once
if (_last_config_update == 0) {
adc_init();
_voted_sensors_update.init(_sensor_combined);
parameter_update_poll(true);
}