2021-09-10 17:08:38 +02:00
|
|
|
/*
|
2021-09-24 14:19:27 +02:00
|
|
|
* Title: ArduinoDeviceHandler.cpp
|
|
|
|
* Author: Marco Modè
|
|
|
|
* Last version: 24/09/2021
|
|
|
|
* Project: ESBO-DS
|
|
|
|
*-------------------------------------------------------------------------------------------------------------------
|
2021-09-10 17:08:38 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mission/DeviceHandler/ArduinoDeviceHandler.h>
|
|
|
|
|
|
|
|
ArduinoDH::ArduinoDH(object_id_t objectId, object_id_t comIF, CookieIF *cookie) :
|
2021-09-24 14:19:27 +02:00
|
|
|
DeviceHandlerBase(objectId, comIF, cookie),
|
|
|
|
TempValueVec(datapool::Temperature_value, &ArduinoDataSet, PoolVariableIF::VAR_WRITE),
|
|
|
|
TempTimeVec(datapool::Temperature_Timestamp, &ArduinoDataSet, PoolVariableIF::VAR_WRITE) {
|
|
|
|
mode = _MODE_START_UP;
|
|
|
|
}
|
|
|
|
ArduinoDH::~ArduinoDH() {
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArduinoDH::doStartUp() {
|
2021-09-24 14:19:27 +02:00
|
|
|
std::cout << "Arduino device -> Switching-ON" << std::endl;
|
2021-09-10 17:08:38 +02:00
|
|
|
setMode(_MODE_TO_ON);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArduinoDH::doShutDown() {
|
2021-09-24 14:19:27 +02:00
|
|
|
std::cout << "Arduino device -> Switching-OFF" << std::endl;
|
2021-09-10 17:08:38 +02:00
|
|
|
setMode(_MODE_SHUT_DOWN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ReturnValue_t ArduinoDH::buildNormalDeviceCommand(DeviceCommandId_t *id) {
|
|
|
|
return NOTHING_TO_SEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArduinoDH::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
|
|
|
|
return NOTHING_TO_SEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArduinoDH::doTransition(Mode_t modeFrom, Submode_t submodeFrom) {
|
|
|
|
if (mode == _MODE_TO_NORMAL) {
|
2021-09-23 21:48:32 +02:00
|
|
|
setMode(_MODE_TO_NORMAL);
|
2021-09-24 14:19:27 +02:00
|
|
|
std::cout << "Arduino device is in Normal mode" << std::endl;
|
2021-09-10 17:08:38 +02:00
|
|
|
} else {
|
2021-09-14 13:16:26 +02:00
|
|
|
setMode(_MODE_TO_NORMAL);
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArduinoDH::buildCommandFromCommand(
|
|
|
|
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
|
|
|
|
size_t commandDataLen) {
|
|
|
|
return HasActionsIF::EXECUTION_FINISHED;
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:19:27 +02:00
|
|
|
void ArduinoDH::fillCommandAndReplyMap() {
|
|
|
|
}
|
2021-09-10 17:08:38 +02:00
|
|
|
|
|
|
|
ReturnValue_t ArduinoDH::scanForReply(const uint8_t *start, size_t len,
|
|
|
|
DeviceCommandId_t *foundId, size_t *foundLen) {
|
|
|
|
|
2021-09-24 14:19:27 +02:00
|
|
|
/* In this case the data are sent from the Arduino board without any command.
|
|
|
|
* No replies of data received are required.
|
|
|
|
* This function checks the validity of the data packet received.
|
|
|
|
*/
|
2021-09-14 17:52:32 +02:00
|
|
|
|
2021-09-23 21:48:32 +02:00
|
|
|
*foundLen = len;
|
2021-09-24 14:19:27 +02:00
|
|
|
// The ID is defined as a macro. It includes info about the array size.
|
|
|
|
std::map<DeviceCommandId_t, size_t> id { { *foundId, len } };
|
|
|
|
|
|
|
|
/* Check validity of the packet in terms of length (the data have
|
|
|
|
* been already checked in ArduinoComIF.cpp in order to provide the
|
|
|
|
* start of the data buffer.
|
|
|
|
*/
|
|
|
|
if (*foundLen == 7740) {
|
|
|
|
return APERIODIC_REPLY;
|
2021-09-15 19:17:34 +02:00
|
|
|
} else {
|
2021-09-24 14:19:27 +02:00
|
|
|
return DeviceHandlerIF::LENGTH_MISSMATCH;
|
2021-09-23 21:48:32 +02:00
|
|
|
}
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArduinoDH::interpretDeviceReply(DeviceCommandId_t id,
|
|
|
|
const uint8_t *packet) {
|
2021-09-23 21:48:32 +02:00
|
|
|
|
2021-09-24 14:19:27 +02:00
|
|
|
/* The data stored in the read buffer are here copied in the variables with the SPC format.
|
|
|
|
* (The structures are defined in the header)
|
|
|
|
* The data of temperature, environment and orientation are then stored in three separated vectors.
|
|
|
|
* These vectors will be then saved in the framework datapool in order to be used by the controller.
|
|
|
|
*/
|
2021-09-24 18:11:10 +02:00
|
|
|
/*printf(
|
2021-09-23 21:48:32 +02:00
|
|
|
"\n***********************************************************************************************\n");
|
2021-09-24 18:11:10 +02:00
|
|
|
printf("TEMPERATURE parameters are: ");*/
|
2021-09-10 17:08:38 +02:00
|
|
|
for (int i = 0; i < 36; i++) {
|
|
|
|
memcpy(&Temp_ch.start_string, &packet[27 * i + 0], 8);
|
|
|
|
memcpy(&Temp_ch.Typ, &packet[27 * i + 8], 1);
|
|
|
|
memcpy(&Temp_ch.SPCChNumber, &packet[27 * i + 9], 1);
|
|
|
|
memcpy(&Temp_ch.Value_Cnt, &packet[27 * i + 10], 1);
|
|
|
|
memcpy(&Temp_ch.temperature, &packet[27 * i + 11], 4);
|
|
|
|
memcpy(&Temp_ch.Timestamp, &packet[27 * i + 15], 4);
|
|
|
|
memcpy(&Temp_ch.end_string, &packet[27 * i + 19], 8);
|
2021-09-24 14:19:27 +02:00
|
|
|
/* The data (temperature, timestamp) are here saved in the datapool.
|
|
|
|
* They will be exploited by the thermal controller.
|
|
|
|
*/
|
|
|
|
memcpy(&TempValueVec[i], &Temp_ch.temperature, 4);
|
|
|
|
memcpy(&TempTimeVec[i], &Temp_ch.Timestamp, 4);
|
2021-09-23 21:48:32 +02:00
|
|
|
// The data are here printed (useful for tests).
|
2021-09-24 21:18:38 +02:00
|
|
|
/*printf("\n\nStart: %7s", Temp_ch.start_string);
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\nTyp: %u", Temp_ch.Typ);
|
|
|
|
printf("\nSPCChNumber: %u", Temp_ch.SPCChNumber);
|
|
|
|
printf("\nValue_Cnt: %u", Temp_ch.Value_Cnt);
|
|
|
|
printf("\nTemperature: %f", Temp_ch.temperature);
|
|
|
|
printf("\nTimestamp: %u", Temp_ch.Timestamp);
|
|
|
|
printf("\nEnd: %7s", Temp_ch.end_string);
|
2021-09-24 21:18:38 +02:00
|
|
|
vecTemp.emplace_back(Temp_ch);*/
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
2021-09-24 14:19:27 +02:00
|
|
|
ArduinoDataSet.commit(PoolVariableIF::VALID);
|
|
|
|
/* The environment and orientation data reading and printing
|
|
|
|
* are currently commented out because they are not needed.
|
|
|
|
* Anyway they are available and they can be used if necessary.
|
|
|
|
*/
|
2021-09-24 21:18:38 +02:00
|
|
|
/*printf(
|
2021-09-23 21:48:32 +02:00
|
|
|
"\n\n***********************************************************************************************\n");
|
|
|
|
printf("ENVIRONMENTAL parameters are: ");
|
2021-09-24 14:19:27 +02:00
|
|
|
for (int j = 0; j < 9; j++) {
|
2021-09-10 17:08:38 +02:00
|
|
|
memcpy(&Env_ch.start_string, &packet[27 * (36 + j) + 0], 8);
|
|
|
|
memcpy(&Env_ch.Typ, &packet[27 * (36 + j) + 8], 1);
|
|
|
|
memcpy(&Env_ch.SPCChNumber, &packet[27 * (36 + j) + 9], 1);
|
|
|
|
memcpy(&Env_ch.Value_Cnt, &packet[27 * (36 + j) + 10], 1);
|
|
|
|
memcpy(&Env_ch.Value, &packet[27 * (36 + j) + 11], 4);
|
|
|
|
memcpy(&Env_ch.Timestamp, &packet[27 * (36 + j) + 15], 4);
|
|
|
|
memcpy(&Env_ch.end_string, &packet[27 * (36 + j) + 19], 8);
|
2021-09-23 21:48:32 +02:00
|
|
|
// The data are here printed (useful for tests).
|
2021-09-24 14:19:27 +02:00
|
|
|
if (j == 0 || j == 3 || j == 6) {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nHUMIDITY: ");
|
2021-09-24 14:19:27 +02:00
|
|
|
} else if (j == 1 || j == 4 || j == 7) {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nPRESSURE: ");
|
2021-09-24 14:19:27 +02:00
|
|
|
} else {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nTEMPERATURE: ");
|
|
|
|
}
|
|
|
|
printf("\n\nStart: %7s", Env_ch.start_string);
|
|
|
|
printf("\nTyp: %u", Env_ch.Typ);
|
|
|
|
printf("\nSPCChNumber: %u", Env_ch.SPCChNumber);
|
|
|
|
printf("\nValue_Cnt: %u", Env_ch.Value_Cnt);
|
|
|
|
printf("\nTemperature: %f", Env_ch.Value);
|
|
|
|
printf("\nTimestamp: %u", Env_ch.Timestamp);
|
|
|
|
printf("\nEnd: %7s", Env_ch.end_string);
|
2021-09-14 13:16:26 +02:00
|
|
|
vecEnv.emplace_back(Env_ch);
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
|
|
|
printf(
|
|
|
|
"\n\n***********************************************************************************************\n");
|
|
|
|
printf("ACCELEROMETER parameters are: ");
|
2021-09-23 21:48:32 +02:00
|
|
|
for (int k = 0; k < 15; k++) {
|
|
|
|
memcpy(&Ornt_ch.start_string, &packet[27 * (36 + 9) + 91 * k + 0], 8);
|
|
|
|
memcpy(&Ornt_ch.Typ, &packet[27 * (36 + 9) + 91 * k + 8], 1);
|
|
|
|
memcpy(&Ornt_ch.SPCChNumber, &packet[27 * (36 + 9) + 91 * k + 9], 1);
|
|
|
|
memcpy(&Ornt_ch.Value_Cnt, &packet[27 * (36 + 9) + 91 * k + 10], 1);
|
|
|
|
memcpy(&Ornt_ch.Value, &packet[27 * (36 + 9) + 91 * k + 11], 36);
|
|
|
|
memcpy(&Ornt_ch.Timestamp, &packet[27 * (36 + 9) + 91 * k + 47], 36);
|
|
|
|
memcpy(&Ornt_ch.end_string, &packet[27 * (36 + 9) + 91 * k + 83], 8);
|
|
|
|
// The data are here printed (useful for tests).
|
2021-09-10 17:08:38 +02:00
|
|
|
switch (k) {
|
|
|
|
case 0:
|
|
|
|
printf("\n\nACCELERATION: ");
|
|
|
|
break;
|
2021-09-23 21:48:32 +02:00
|
|
|
case 3:
|
2021-09-10 17:08:38 +02:00
|
|
|
printf("\n\nGYROSCOPE: ");
|
|
|
|
break;
|
2021-09-23 21:48:32 +02:00
|
|
|
case 6:
|
2021-09-10 17:08:38 +02:00
|
|
|
printf("\n\nMAGNETOMETER: ");
|
|
|
|
break;
|
2021-09-23 21:48:32 +02:00
|
|
|
case 9:
|
2021-09-10 17:08:38 +02:00
|
|
|
printf("\n\nLINEAR ACCELERATION: ");
|
|
|
|
break;
|
2021-09-23 21:48:32 +02:00
|
|
|
case 12:
|
2021-09-10 17:08:38 +02:00
|
|
|
printf("\n\nEULER ANGLES: ");
|
|
|
|
break;
|
|
|
|
}
|
2021-09-24 14:19:27 +02:00
|
|
|
if (k == 0 || k == 3 || k == 6 || k == 9 || k == 12) {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nX ==> ");
|
2021-09-24 14:19:27 +02:00
|
|
|
} else if (k == 1 || k == 4 || k == 7 || k == 10 || k == 13) {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nY ==> ");
|
2021-09-24 14:19:27 +02:00
|
|
|
} else {
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\n\nZ ==> ");
|
|
|
|
}
|
|
|
|
printf("\nStart: %7s", Ornt_ch.start_string);
|
|
|
|
printf("\nTyp: %u", Ornt_ch.Typ);
|
|
|
|
printf("\nSPCChNumber: %u", Ornt_ch.SPCChNumber);
|
|
|
|
printf("\nValue_Cnt: %u", Ornt_ch.Value_Cnt);
|
2021-09-10 17:08:38 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
printf("\nMeasurement number: %d", i);
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\nValue: %f", Ornt_ch.Value[i]);
|
|
|
|
printf("\nTimestamp: %u", Ornt_ch.Timestamp[i]);
|
2021-09-10 17:08:38 +02:00
|
|
|
}
|
2021-09-23 21:48:32 +02:00
|
|
|
printf("\nEnd: %7s", Ornt_ch.end_string);
|
|
|
|
vecOrnt.emplace_back(Ornt_ch);
|
|
|
|
}//*/
|
2021-09-10 17:08:38 +02:00
|
|
|
|
2021-09-15 17:31:20 +02:00
|
|
|
std::cout << "\n\nEnd reading data.\n" << std::endl;
|
2021-09-10 17:08:38 +02:00
|
|
|
|
|
|
|
return RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:19:27 +02:00
|
|
|
void ArduinoDH::setNormalDatapoolEntriesInvalid() {
|
|
|
|
}
|