Commit 65b24af4 authored by Clément Foucher's avatar Clément Foucher
Browse files

Introduce object-based user-level API for the data acquisition module.

Integrate data conversion into the data acquisition module.
parent 3ae8c917
......@@ -13,4 +13,3 @@ lib_deps=
peripherals = https://gitlab.laas.fr/owntech/power-api/opalib-peripherals.git
control_pid = https://gitlab.laas.fr/owntech/power-api/opalib-control-pid.git
power_conversion = https://gitlab.laas.fr/owntech/power-api/opalib-power-conversion.git
data_conversion = https://gitlab.laas.fr/owntech/power-api/opalib-data-conversion.git
if(CONFIG_OWNTECH_DATA_ACQUISITION)
zephyr_include_directories(./public_include)
zephyr_include_directories(./public_api)
zephyr_library()
zephyr_library_sources(
......@@ -10,6 +10,7 @@ if(CONFIG_OWNTECH_DATA_ACQUISITION)
./adc/adc_helper.c
./dma/dma.c
./data_dispatch/data_dispatch.c
./data_acquisition.c
./data_conversion/data_conversion.c
./public_api/DataAcquisition.cpp
)
endif()
......@@ -60,7 +60,7 @@ void adc_configure_trigger_source(uint8_t adc_number, uint32_t trigger_source)
adc_trigger_sources[adc_number-1] = trigger_source;
}
int8_t adc_configure_adc_channels(uint8_t adc_number, char* channel_list[], uint8_t channel_count)
int8_t adc_configure_adc_channels(uint8_t adc_number, const char* channel_list[], uint8_t channel_count)
{
return adc_channnels_configure_adc_channels(adc_number, channel_list, channel_count);
}
......@@ -113,7 +113,7 @@ void adc_start()
}
}
char* adc_get_channel_name(uint8_t adc_number, uint8_t channel_rank)
const char* adc_get_channel_name(uint8_t adc_number, uint8_t channel_rank)
{
return adc_channels_get_channel_name(adc_number, channel_rank);
}
......@@ -91,7 +91,7 @@ void adc_configure_trigger_source(uint8_t adc_number, uint32_t trigger_source);
* is not available in the given ADC. Available channels are the
* ones defined in the device tree.
*/
int8_t adc_configure_adc_channels(uint8_t adc_number, char* channel_list[], uint8_t channel_count);
int8_t adc_configure_adc_channels(uint8_t adc_number, const char* channel_list[], uint8_t channel_count);
/**
* @brief Starts all configured ADCs.
......@@ -111,7 +111,7 @@ void adc_start();
* NULL if channel configuration has not been made or
* channel_rank is over (number of enabled channels)-1.
*/
char* adc_get_channel_name(uint8_t adc_number, uint8_t channel_rank);
const char* adc_get_channel_name(uint8_t adc_number, uint8_t channel_rank);
#ifdef __cplusplus
}
......
......@@ -37,8 +37,8 @@
#include "adc_channels.h"
// OwnTech API
#include "data_acquisition.h"
#include "adc_helper.h"
#include "data_acquisition_error_codes.h"
/////
......@@ -290,7 +290,7 @@ static void _adc_channels_set_enabled_channels(uint8_t adc_num, channel_prop_t**
}
}
static channel_prop_t* _adc_channels_get_available_channel_by_name(uint8_t adc_num, char* channel_name)
static channel_prop_t* _adc_channels_get_available_channel_by_name(uint8_t adc_num, const char* channel_name)
{
channel_prop_t** current_adc_available_channels = _adc_channels_get_available_channels_list(adc_num);
......@@ -384,7 +384,7 @@ void adc_channels_configure(uint8_t adc_num)
LL_ADC_REG_SetSequencerLength(adc, (uint32_t)enabled_channels_in_this_adc-1);
}
int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, char* channel_list[], uint8_t channel_count)
int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, const char* channel_list[], uint8_t channel_count)
{
uint8_t result = 0;
......@@ -422,7 +422,7 @@ int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, char* channel_list[
return result;
}
char* adc_channels_get_channel_name(uint8_t adc_num, uint8_t channel_rank)
const char* adc_channels_get_channel_name(uint8_t adc_num, uint8_t channel_rank)
{
channel_prop_t** current_adc_enabled_channels_list = _adc_channels_get_enabled_channels_list(adc_num);
......
......@@ -72,7 +72,7 @@ void adc_channels_configure(uint8_t adc_num);
* is not available in the given ADC. Available channels are the
* ones defined in the device tree.
*/
int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, char* channel_list[], uint8_t channel_count);
int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, const char* channel_list[], uint8_t channel_count);
/**
* This function returns the name of an enabled channel.
......@@ -87,7 +87,7 @@ int8_t adc_channnels_configure_adc_channels(uint8_t adc_num, char* channel_list[
* NULL if channel configuration has not been made or
* channel_rank is over (number of enabled channels)-1.
*/
char* adc_channels_get_channel_name(uint8_t adc_num, uint8_t channel_rank);
const char* adc_channels_get_channel_name(uint8_t adc_num, uint8_t channel_rank);
/**
* Get the number of enabled channels for an ADC.
......
/*
* Copyright (c) 2021 LAAS-CNRS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGLPV2.1
*/
/**
* @author Clément Foucher <clement.foucher@laas.fr>
*/
// Stdlib
#include <string.h>
#include <stdint.h>
// OwnTech Power API
#include "dma/dma.h"
#include "adc/adc.h"
#include "data_dispatch/data_dispatch.h"
#include "data_acquisition.h"
/////
// Local types
typedef struct
{
uint8_t adc_number;
uint8_t channel_rank;
} channel_assignment_t;
/////
// Local variables
static uint8_t data_acquisition_initialized = 0;
static uint8_t data_acquisition_started = 0;
static channel_assignment_t v1_low_assignement = {0};
static channel_assignment_t v2_low_assignement = {0};
static channel_assignment_t v_high_assignement = {0};
static channel_assignment_t i1_low_assignement = {0};
static channel_assignment_t i2_low_assignement = {0};
static channel_assignment_t i_high_assignement = {0};
static channel_assignment_t temp_sensor_assignement = {0};
/////
// Public API
int8_t data_acquisition_init()
{
if (data_acquisition_initialized == 0)
{
adc_init();
data_acquisition_initialized = 1;
return 0;
}
else
{
return EALREADYINIT;
}
}
int8_t data_acquisition_set_adc12_dual_mode(uint8_t dual_mode)
{
if ( (data_acquisition_initialized == 1) && (data_acquisition_started == 0) )
{
adc_set_dual_mode(dual_mode);
return 0;
}
else if (data_acquisition_initialized == 0)
{
return EUNINITIALIZED;
}
else
{
return EALREADYSTARTED;
}
}
int8_t data_acquisition_configure_adc_channels(uint8_t adc_number, char* channel_list[], uint8_t channel_count)
{
if ( (data_acquisition_initialized == 1) && (data_acquisition_started == 0) )
{
int8_t result = adc_configure_adc_channels(adc_number, channel_list, channel_count);
if (result == 0)
{
for (int i = 0 ; i < channel_count ; i++)
{
if (strcmp(channel_list[i], "V1_LOW") == 0)
{
v1_low_assignement.adc_number = adc_number;
v1_low_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "V2_LOW") == 0)
{
v2_low_assignement.adc_number = adc_number;
v2_low_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "V_HIGH") == 0)
{
v_high_assignement.adc_number = adc_number;
v_high_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "I1_LOW") == 0)
{
i1_low_assignement.adc_number = adc_number;
i1_low_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "I2_LOW") == 0)
{
i2_low_assignement.adc_number = adc_number;
i2_low_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "I_HIGH") == 0)
{
i_high_assignement.adc_number = adc_number;
i_high_assignement.channel_rank = i;
}
else if (strcmp(channel_list[i], "TEMP_SENSOR") == 0)
{
temp_sensor_assignement.adc_number = adc_number;
temp_sensor_assignement.channel_rank = i;
}
}
}
return result;
}
else if (data_acquisition_initialized == 0)
{
return EUNINITIALIZED;
}
else
{
return EALREADYSTARTED;
}
}
int8_t data_acquisition_configure_adc_trigger_source(uint8_t adc_number, uint32_t trigger_source)
{
if ( (data_acquisition_initialized == 1) && (data_acquisition_started == 0) )
{
adc_configure_trigger_source(adc_number, trigger_source);
return 0;
}
else if (data_acquisition_initialized == 0)
{
return EUNINITIALIZED;
}
else
{
return EALREADYSTARTED;
}
}
int8_t data_acquisition_start()
{
if ( (data_acquisition_initialized == 1) && (data_acquisition_started == 0) )
{
// DMAs
dma_configure_and_start(2);
// Initialize data dispatch
data_dispatch_init(2);
// Launch ADC conversion
adc_start();
data_acquisition_started = 1;
return 0;
}
else if (data_acquisition_initialized == 0)
{
return EUNINITIALIZED;
}
else
{
return EALREADYSTARTED;
}
}
char* data_acquisition_get_channel_name(uint8_t adc_number, uint8_t channel_rank)
{
return adc_get_channel_name(adc_number, channel_rank);
}
uint16_t* data_acquisition_get_v1_low_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(v1_low_assignement.adc_number, v1_low_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_v2_low_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(v2_low_assignement.adc_number, v2_low_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_v_high_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(v_high_assignement.adc_number, v_high_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_i1_low_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(i1_low_assignement.adc_number, i1_low_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_i2_low_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(i2_low_assignement.adc_number, i2_low_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_i_high_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(i_high_assignement.adc_number, i_high_assignement.channel_rank, number_of_values_acquired);
}
uint16_t* data_acquisition_get_temp_sensor_values(uint32_t* number_of_values_acquired)
{
return data_dispatch_get_acquired_values(temp_sensor_assignement.adc_number, temp_sensor_assignement.channel_rank, number_of_values_acquired);
}
/*
* Copyright (c) 2021-2022 LAAS-CNRS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGLPV2.1
*/
/**
* @date 2022
* @author Antoine Boche <antoine.boche@laas.fr>
* @author Clément Foucher <clement.foucher@laas.fr>
* @author Luiz Villa <luiz.villa@laas.fr>
*/
/////
// Include
// Zephyr
#include <zephyr.h>
// Current file header
#include "data_conversion.h"
/////
// Local variables
// ADC convertions variable
// Currents
static float32_t gain_currents[3] = {0.0125, 0.0143, 0.0125}; //i1_low, i2_low and i_high
static float32_t offset_currents[3] = {25.9, 29.28, 0}; //i1_low, i2_low offsets - ihigh needs to be callibrated still
// Voltages
static float32_t gain_voltages[3] = {0.0462, 0.0462, 0.0640}; //v1_low, v2_low and v_high
static float32_t offset_voltages[3] = {94.04, 94.04, 4.408}; //v1_low, v2_low and v_high
// Temperature
static float32_t gain_temperature = 0.0;
static float32_t offset_temperature = 0.0;
/////
// Public Functions
/**
* These functions convert raw values into their appropriate physical unit
*/
float32_t data_conversion_convert_v1_low(uint16_t raw_value)
{
return (raw_value*gain_voltages[0])-offset_voltages[0];
}
float32_t data_conversion_convert_i1_low(uint16_t raw_value)
{
return (raw_value*gain_currents[0])+offset_currents[0];
}
float32_t data_conversion_convert_v2_low(uint16_t raw_value)
{
return (raw_value*gain_voltages[1])-offset_voltages[1];
}
float32_t data_conversion_convert_i2_low(uint16_t raw_value)
{
return (raw_value*gain_currents[1])+offset_currents[1];
}
float32_t data_conversion_convert_v_high(uint16_t raw_value)
{
return (raw_value*gain_voltages[2])+offset_voltages[2];
}
float32_t data_conversion_convert_i_high(uint16_t raw_value)
{
return (raw_value*gain_currents[2])+offset_currents[2];
}
float32_t data_conversion_convert_temp(uint16_t raw_value)
{
return (raw_value*gain_temperature)+offset_temperature;
}
/**
* These functions update the parameters of the variables
*/
void data_conversion_set_v1_low_parameters(float32_t gain, float32_t offset)
{
gain_voltages[0] = gain;
offset_voltages[0] = offset;
}
void data_conversion_set_v2_low_parameters(float32_t gain, float32_t offset)
{
gain_voltages[0] = gain;
offset_voltages[0] = offset;
}
void data_conversion_set_v_high_parameters(float32_t gain, float32_t offset)
{
gain_voltages[2] = gain;
offset_voltages[2] = offset;
}
void data_conversion_set_i1_low_parameters(float32_t gain, float32_t offset)
{
gain_currents[1] = gain;
offset_currents[1] = offset;
}
void data_conversion_set_i2_low_parameters(float32_t gain, float32_t offset)
{
gain_currents[1] = gain;
offset_currents[1] = offset;
}
void data_conversion_set_i_high_parameters(float32_t gain, float32_t offset)
{
gain_currents[2] = gain;
offset_currents[2] = offset;
}
void data_conversion_set_temp_parameters(float32_t gain, float32_t offset)
{
gain_temperature = gain;
offset_temperature = offset;
}
/*
* Copyright (c) 2021-2022 LAAS-CNRS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGLPV2.1
*/
/**
* @date 2022
* @author Antoine Boche <antoine.boche@laas.fr>
* @author Clément Foucher <clement.foucher@laas.fr>
* @author Luiz Villa <luiz.villa@laas.fr>
*/
#ifndef DATA_CONVERSION_H_
#define DATA_CONVERSION_H_
#include <arm_math.h> // adds all the CMSIS library
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for v1 low voltage
*/
float32_t data_conversion_convert_v1_low(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for v2 low voltage
*/
float32_t data_conversion_convert_v2_low(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for vhigh voltage
*/
float32_t data_conversion_convert_v_high(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for i1 low voltage
*/
float32_t data_conversion_convert_i1_low(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for i2 low voltage
*/
float32_t data_conversion_convert_i2_low(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for ihigh low voltage
*/
float32_t data_conversion_convert_i_high(uint16_t raw_value);
/**
* @brief Converts the values of the given raw_value into a physical unit
*
* @param[in] raw_value
*
* @return a foat32_t value for the temperature probe
*/
float32_t data_conversion_convert_temp(uint16_t raw_value);