testArduino/mission/DeviceHandler/ArduinoDeviceHandler.h

210 lines
8.2 KiB
C++

/*
* Title: ArduinoDeviceHandler.h
* Author: Marco Modè
* Last version: 24/09/2021
* Project: ESBO-DS
*
* @ brief: Basic device handler to manage the communication with the Arduino sensor board.
* The data are sent to the serial port of the computer from the Arduino board.
* The device handler read and manages these data exploiting the ComIF and the
* DeviceHandlerBase functions.
* @ details: The ArduinoDH object is instantiated in the generic factory.
* @ author: Marco Modè
* @ ingroup: mission/DeviceHandler
*-------------------------------------------------------------------------------------------------------------------
*
*/
/*
* The structure of data sent is in SPC format, required by the
* On-Board Computer.
* The structure format is defined below in the class.
*
* Hereafter, the sensor boards connected to the Arduino Micro are described
* with board name, associated pin on the Arduino board and related channels.
* This is done to give a clearer understanding of the reading process.
*
* TEMPERATURE DATA - STRUCTURE
* TMB 1, SS = pin 4
* Temperature Temp_ch1; Temperature Temp_ch2; Temperature Temp_ch3;
* Temperature Temp_ch4; Temperature Temp_ch5; Temperature Temp_ch6;
* Temperature Temp_ch7; Temperature Temp_ch8; Temperature Temp_ch9;
* TMB 2, SS = pin 5
* Temperature Temp_ch11; Temperature Temp_ch12; Temperature Temp_ch13;
* Temperature Temp_ch14; Temperature Temp_ch15; Temperature Temp_ch16;
* Temperature Temp_ch17; Temperature Temp_ch18; Temperature Temp_ch19;
* TMB 3, SS = pin 6
* Temperature Temp_ch21; Temperature Temp_ch22; Temperature Temp_ch23;
* Temperature Temp_ch24; Temperature Temp_ch25; Temperature Temp_ch26;
* Temperature Temp_ch27; Temperature Temp_ch28; Temperature Temp_ch29;
* TMB 4, SS = pin 7
* Temperature Temp_ch31; Temperature Temp_ch32; Temperature Temp_ch33;
* Temperature Temp_ch34; Temperature Temp_ch35; Temperature Temp_ch36;
* Temperature Temp_ch37; Temperature Temp_ch38; Temperature Temp_ch39;
*
* ENVIRONMENTAL DATA - STRUCTURE
* BME280_1, SS = pin 8 (pressure, humidity, temperature)
* Environmental Env_ch41; Environmental Env_ch42; Environmental Env_ch43;
* BME280_2, SS = pin 9 (pressure, humidity, temperature)
* Environmental Env_ch51; Environmental Env_ch52; Environmental Env_ch53;
* BME280_3, SS = pin 10 (pressure, humidity, temperature)
* Environmental Env_ch61; Environmental Env_ch62; Environmental Env_ch63;
*
* ORIENTATION DATA - STRUCTURE
* BNO055_1
* ACCELERATION
* Orientation Ornt_ch81; Orientation Ornt_ch82; Orientation Ornt_ch83;
* GYROSCOPE
* Orientation Ornt_ch91; Orientation Ornt_ch92; Orientation Ornt_ch93;
* MAGNETOMETER
* Orientation Ornt_ch101; Orientation Ornt_ch102; Orientation Ornt_ch103;
* LINEAR ACCELERATION
* Orientation Ornt_ch111; Orientation Ornt_ch112; Orientation Ornt_ch113;
* EULER ANGLES
* Orientation Ornt_ch121; Orientation Ornt_ch122; Orientation Ornt_ch123;
*
*/
#ifndef MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_
#define MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_
#include <fsfw/devicehandlers/DeviceHandlerBase.h>
#include <fsfw/globalfunctions/PeriodicOperationDivider.h>
#include <fsfw/timemanager/Countdown.h>
#include <fsfw/datapool/DataSet.h>
#include <fsfw/datapool/PoolVector.h>
#include <bsp_linux/fsfwconfig/datapool/dataPoolInit.h>
#include <OBSWConfig.h>
#include <vector>
#include <cstdlib>
#include <map>
class ArduinoDH: public DeviceHandlerBase {
public:
/**
* Build the test device in the factory.
* @param objectId This ID will be assigned to the test device handler.
* @param comIF The ID of the Communication IF used by test device handler.
* @param cookie Cookie object used by the test device handler. This is
* also used and passed to the comIF object.
*/
ArduinoDH(object_id_t objectId, object_id_t comIF, CookieIF *cookie);
virtual ~ ArduinoDH();
/* This header code contains the definition of the structures of data
* (Temperature, Environemntal, Orientation) in which the sensors data are stored.
* These structures are defined in the Arduino IDE code developed for the sensor board.
* The Arduino is the responsible of the management of sensors devices and data.
* Furthermore, the vectors and arrays necessary for the code execution are here
* defined.
*/
/* Definiton of data structure for SPC communication. Three different structures are defined for measurements of:
* - Temperature data,
* - Environmental data,
* - Orientation data.
*/
struct Temperature {
char start_string[8];
uint8_t Typ;
uint8_t SPCChNumber;
uint8_t Value_Cnt;
float temperature;
unsigned int Timestamp;
char end_string[8];
Temperature() = default;
Temperature(const char *_start_string, uint8_t _Typ,
uint8_t _SPCChNumber, uint8_t _Value_Cnt, float _temperature,
unsigned int _Timestamp, const char *_end_string) :
Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt), temperature(
_temperature), Timestamp(_Timestamp) {
strncpy(start_string, _start_string, sizeof(start_string) - 1);
strncpy(end_string, _end_string, sizeof(end_string) - 1);
}
};
struct Environmental {
char start_string[8];
uint8_t Typ;
uint8_t SPCChNumber;
uint8_t Value_Cnt;
float Value;
unsigned int Timestamp;
char end_string[8];
Environmental() = default;
Environmental(const char *_start_string, uint8_t _Typ,
uint8_t _SPCChNumber, uint8_t _Value_Cnt, float _Value,
unsigned int _Timestamp, const char *_end_string) :
Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt), Value(
_Value), Timestamp(_Timestamp) {
strncpy(start_string, _start_string, sizeof(start_string) - 1);
strncpy(end_string, _end_string, sizeof(end_string) - 1);
}
};
struct Orientation {
char start_string[8];
uint8_t Typ;
uint8_t SPCChNumber;
uint8_t Value_Cnt;
float Value[9]; //max buffer
unsigned int Timestamp[9]; //max buffer
char end_string[8];
Orientation() = default;
Orientation(const char *_start_string, uint8_t _Typ,
uint8_t _SPCChNumber, uint8_t _Value_Cnt, const float *_Value,
const unsigned int *_Timestamp, const char *_end_string) :
Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt) {
strncpy(start_string, _start_string, sizeof(start_string) - 1);
memcpy(&Value, _Value, sizeof(Value) - 1);
memcpy(&Timestamp, _Timestamp, sizeof(Timestamp) - 1);
strncpy(end_string, _end_string, sizeof(end_string) - 1);
}
};
/* Three vectors are defined to store the three type of classes sequentially
* during the phase of reading copying data from the buffers
*/
std::vector<Temperature> vecTemp;
std::vector<Environmental> vecEnv;
std::vector<Orientation> vecOrnt;
/* Three dummy child structures are defined. They are used to store the three
* different types of data during the measurement loop and then the data are
* copied in the vectors above.
* Then, they are overwritten by the data of next iteration and the process is
* repeated ,until all the data from the buffer are copied to the three vectors
* using the three different structures.
*/
Temperature Temp_ch;
Environmental Env_ch;
Orientation Ornt_ch;
protected:
// DeviceHandlerBase inherited functions.
virtual void doStartUp() override;
virtual void doShutDown() override;
virtual ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id)
override;
virtual ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id)
override;
virtual void doTransition(Mode_t modeFrom, Submode_t subModeFrom) override;
virtual ReturnValue_t buildCommandFromCommand(
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
size_t commandDataLen) override;
virtual void fillCommandAndReplyMap() override;
virtual ReturnValue_t scanForReply(const uint8_t *start, size_t len,
DeviceCommandId_t *foundId, size_t *foundLen) override;
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) override;
virtual void setNormalDatapoolEntriesInvalid() override;
// Dataset initialized. It is used for copying the data to the datapool.
DataSet ArduinoDataSet;
// Here the datapool variables are defined. The needed data will be stored inside them.
PoolVector <float, 36> TempValueVec;
PoolVector <unsigned int, 36> TempTimeVec;
};
#endif /* MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_ */