void Adc::update(const ros::TimerEvent& e)
{
int value = 0;
double voltage = 0.0;
if (get_adc_data(ADC_2, &value) >= 0)
{
voltage = ((double)value) * TEMP_CONST;
std_msgs::Float64 msg;
msg.data = voltage;
voltage_pub_.publish(msg);
}
else
{
int max_count = output_hz_ * 5;
open_count_++;
if (open_count_ >= max_count)
{
open_count_ = 0;
}
}
updateDiagnostics();
}
void Adc::updateDiagnostics()
{
int max_count = output_hz_;
diag_count_++;
if (diag_count_ >= max_count)
{
diag_count_ = 0;
diagnostic_updater_.update();
}
}
int Adc::get_adc_data(int adc_channel, int *value)
{
if (value == NULL) {
printf("value pointer error\n");
return ADC_ERROR;
}
char adc_path[128];
(void)memset(adc_path, 0, sizeof(adc_path));
if (adc_channel == ADC_1) {
(void)sprintf(adc_path, "%s", ADC_CHANNEL_1);
} else if (adc_channel == ADC_2) {
(void)sprintf(adc_path, "%s", ADC_CHANNEL_2);
} else {
printf("no such a adc_channel\n");
return ADC_ERROR;
}
FILE *fp = NULL;
char buffer[sizeof(int)];
fp = fopen(adc_path, "r");
if (!fp) {
perror("fopen");
return ADC_ERROR;
}
(void)fread(buffer, sizeof(buffer), 1, fp);
(void)fclose(fp);
fp = NULL;
*value = atoi(buffer);
return 0;
}
更多回帖