Skip to content

Commit

Permalink
Implemented findings from BlueAndi
Browse files Browse the repository at this point in the history
  • Loading branch information
jkerpe committed Nov 12, 2023
1 parent 4082977 commit af74107
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 139 deletions.
18 changes: 9 additions & 9 deletions doc/architecture/SENSORFUSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ On simulation the physical communication uses a socket connection.
### Tx channel "SENSOR_DATA"
This channel is used to send raw sensor data used for Sensor Fusion on the ZumoComSystem.

* The dataypes int16_t is used.
* The datatypes can be found in SerialMuxChannel.h.
* Order:
* Acceleration in X
* Acceleration in Y
* turnRateZ around Z
* Magnetometer value in X
* Magnetometer value in Y
* Angle calculated by Odometry
* Position in X calculated by Odometry
* Position in Y calculated by Odometry
* Acceleration in X (raw sensor value)
* Acceleration in Y (raw sensor value)
* turnRateZ around Z (raw sensor value)
* Magnetometer value in X (raw sensor value)
* Magnetometer value in Y (raw sensor value)
* Angle calculated by Odometry (in mrad)
* Position in X calculated by Odometry (in mm)
* Position in Y calculated by Odometry (in mm)
* Endianess: Big endian

# SW Architecture
Expand Down
2 changes: 1 addition & 1 deletion lib/HALInterfaces/IBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class IBoard
virtual ILed& getGreenLed() = 0;

/**
* Get IMU driver.
* Get IMU (=Inertial Measurement Unit) driver.
*
* @return IMU driver
*/
Expand Down
46 changes: 24 additions & 22 deletions lib/HALInterfaces/IIMU.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* MIT License
*
* Copyright (c) 2023 Andreas Merkle <[email protected]>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
Expand All @@ -25,9 +25,9 @@
DESCRIPTION
*******************************************************************************/
/**
* @brief Abstract IMU interface, depicts Zumo32U4IMU library
* @brief Abstract IMU (Inertial Measurement Unit) interface, depicts Zumo32U4IMU library
* @author Juliane Kerpe <[email protected]>
*
*
* @addtogroup HALInterfaces
*
* @{
Expand Down Expand Up @@ -55,12 +55,14 @@
/** Struct of the IMU data in x, y and z direction. */
typedef struct _IMUData
{
int16_t valueX;
int16_t valueY;
int16_t valueZ;
int16_t valueX; /* Raw sensor value in x direction (unitless) */
int16_t valueY; /* Raw sensor value in y direction (unitless) */
int16_t valueZ; /* Raw sensor value in z direction (unitless) */
} __attribute__((packed)) IMUData;

/** The abstract IMU interface. */
/** The abstract IMU interface.
* IMU stands for Inertial Measurement Unit.
*/
class IIMU
{
public:
Expand All @@ -77,49 +79,49 @@ class IIMU
* @return True if the sensor type was detected succesfully; false otherwise.
*/
virtual bool init() = 0;

/**
* Enables all of the inertial sensors with a default configuration.
*/
virtual void enableDefault() = 0;

/**
* Configures the sensors with settings optimized for turn sensing.
*/
virtual void configureForTurnSensing() = 0;

/**
* Takes a reading from the accelerometer and makes the measurements available in a.
*/
*/
virtual void readAccelerometer() = 0;

/**
* Takes a reading from the gyro and makes the measurements available in g.
*/
virtual void readGyro() = 0;

/**
* Takes a reading from the magnetometer and makes the measurements available in m.
*/
virtual void readMagnetometer() = 0;

/**
* Indicates whether the accelerometer has new measurement data ready.
*
* @return True if there is new accelerometer data available; false otherwise.
*
* @return True if there is new accelerometer data available; false otherwise.
*/
virtual bool accelerometerDataReady() = 0;

/**
* Indicates whether the gyro has new measurement data ready.
*
*
* @return True if there is new gyro data available; false otherwise.
*/
virtual bool gyroDataReady() = 0;

/**
* Indicates whether the magnetometer has new measurement data ready.
*
*
* @return True if there is new magnetometer data available; false otherwise.
*/
virtual bool magnetometerDataReady() = 0;
Expand All @@ -129,21 +131,21 @@ class IIMU
*
* @param[in] accelerationValues Pointer to IMUData struct.
*/
virtual void getAccelerationValues(IMUData* accelerationValues) = 0;
virtual const void getAccelerationValues(IMUData* accelerationValues) = 0;

/**
* Get last raw Gyroscope values as a IMUData struct containing values in x, y and z.
*
* @param[in] turnRates Pointer to IMUData struct.
*/
virtual void getTurnRates(IMUData* turnRates) = 0;
virtual const void getTurnRates(IMUData* turnRates) = 0;

/**
* Get last raw Magnetometer values as a IMUData struct containing values in x, y and z.
*
* @param[in] magnetometerValues Pointer to IMUData struct.
*/
virtual void getMagnetometerValues(IMUData* magnetometerValues) = 0;
virtual const void getMagnetometerValues(IMUData* magnetometerValues) = 0;

/**
* Calibrate the IMU.
Expand Down
2 changes: 1 addition & 1 deletion lib/HALSim/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void Board::init()
m_keyboard.init();
m_lineSensors.init();
m_motors.init();
// To Do: TD084 React if IMU initialization fails
/* TODO: TD084 React if IMU initialization fails */
(void)m_imu.init();
m_imu.configureForTurnSensing();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/HALSim/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ class Board : public IBoard
}

/**
* Get IMU.
*
* Get IMU (=Inertial Measurement Unit) driver.
*
* @return IMU driver
*/
IIMU& getIMU() final
Expand Down
38 changes: 18 additions & 20 deletions lib/HALSim/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ void IMU::readAccelerometer()
const double* accelerationValues = m_accelerometer->getValues();
if (nullptr != accelerationValues)
{
m_accelerationValues.valueX = convertFromDoubleToInt16(accelerationValues[0]); /** Index 0 is the x-axis. */
m_accelerationValues.valueY = convertFromDoubleToInt16(accelerationValues[1]); /** Index 1 is the y-axis. */
m_accelerationValues.valueZ = convertFromDoubleToInt16(accelerationValues[2]); /** Index 2 is the z-axis. */
m_accelerationValues.valueX = convertFromDoubleToInt16(accelerationValues[0]); /* Index 0 is the x-axis. */
m_accelerationValues.valueY = convertFromDoubleToInt16(accelerationValues[1]); /* Index 1 is the y-axis. */
m_accelerationValues.valueZ = convertFromDoubleToInt16(accelerationValues[2]); /* Index 2 is the z-axis. */
}
}
}
Expand All @@ -93,9 +93,9 @@ void IMU::readGyro()

if (nullptr != gyroValues)
{
m_gyroValues.valueX = convertFromDoubleToInt16(gyroValues[0]); /** Index 0 is the x-axis. */
m_gyroValues.valueY = convertFromDoubleToInt16(gyroValues[1]); /** Index 1 is the y-axis. */
m_gyroValues.valueZ = convertFromDoubleToInt16(gyroValues[2]); /** Index 2 is the z-axis. */
m_gyroValues.valueX = convertFromDoubleToInt16(gyroValues[0]); /* Index 0 is the x-axis. */
m_gyroValues.valueY = convertFromDoubleToInt16(gyroValues[1]); /* Index 1 is the y-axis. */
m_gyroValues.valueZ = convertFromDoubleToInt16(gyroValues[2]); /* Index 2 is the z-axis. */
}
}
}
Expand All @@ -108,14 +108,14 @@ void IMU::readMagnetometer()

if (nullptr != magnetometerValues)
{
m_magnetometerValues.valueX = convertFromDoubleToInt16(magnetometerValues[0]); /** Index 0 is the x-axis. */
m_magnetometerValues.valueY = convertFromDoubleToInt16(magnetometerValues[1]); /** Index 1 is the y-axis. */
m_magnetometerValues.valueZ = convertFromDoubleToInt16(magnetometerValues[2]); /** Index 2 is the z-axis. */
m_magnetometerValues.valueX = convertFromDoubleToInt16(magnetometerValues[0]); /* Index 0 is the x-axis. */
m_magnetometerValues.valueY = convertFromDoubleToInt16(magnetometerValues[1]); /* Index 1 is the y-axis. */
m_magnetometerValues.valueZ = convertFromDoubleToInt16(magnetometerValues[2]); /* Index 2 is the z-axis. */
}
}
}

void IMU::getAccelerationValues(IMUData* accelerationValues)
const void IMU::getAccelerationValues(IMUData* accelerationValues)
{
if (nullptr != accelerationValues)
{
Expand All @@ -125,7 +125,7 @@ void IMU::getAccelerationValues(IMUData* accelerationValues)
}
}

void IMU::getTurnRates(IMUData* turnRates)
const void IMU::getTurnRates(IMUData* turnRates)
{
if (nullptr != turnRates)
{
Expand All @@ -135,7 +135,7 @@ void IMU::getTurnRates(IMUData* turnRates)
}
}

void IMU::getMagnetometerValues(IMUData* magnetometerValues)
const void IMU::getMagnetometerValues(IMUData* magnetometerValues)
{
if (nullptr != magnetometerValues)
{
Expand All @@ -147,22 +147,20 @@ void IMU::getMagnetometerValues(IMUData* magnetometerValues)

void IMU::calibrate()
{
// TODO: implement TD067
/* TODO: implement TD067 */
}

int16_t IMU::convertFromDoubleToInt16(double originalValue)
{
int16_t convertedValue;
const int16_t minimumValue = std::numeric_limits<int16_t>::min();
const int16_t maximumValue = std::numeric_limits<int16_t>::max();
int16_t convertedValue;

if (originalValue <= minimumValue)
if (originalValue <= INT16_MIN)
{
convertedValue = minimumValue;
convertedValue = INT16_MIN;
}
else if (originalValue >= maximumValue)
else if (originalValue >= INT16_MAX)
{
convertedValue = maximumValue;
convertedValue = INT16_MAX;
}
else
{
Expand Down
25 changes: 13 additions & 12 deletions lib/HALSim/IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class IMU : public IIMU
*/
IMU(const SimTime& simTime, webots::Accelerometer* accelerometer, webots::Gyro* gyro, webots::Compass* compass) :
IIMU(),
m_accelerationValues{0, 0, 0},
m_gyroValues{0, 0, 0},
m_magnetometerValues{0, 0, 0},
m_simTime(simTime),
m_accelerometer(accelerometer),
m_gyro(gyro),
Expand Down Expand Up @@ -157,21 +160,21 @@ class IMU : public IIMU
*
* @param[in] accelerationValues Pointer to IMUData struct.
*/
void getAccelerationValues(IMUData* accelerationValues);
void const getAccelerationValues(IMUData* accelerationValues);

/**
* Get last raw Gyroscope values as a IMUData struct containing values in x, y and z.
*
* @param[in] turnRates Pointer to IMUData struct.
*/
void getTurnRates(IMUData* turnRates);
void const getTurnRates(IMUData* turnRates);

/**
* Get last raw Magnetometer values as a IMUData struct containing values in x, y and z.
*
* @param[in] magnetometerValues Pointer to IMUData struct.
*/
void getMagnetometerValues(IMUData* magnetometerValues);
void const getMagnetometerValues(IMUData* magnetometerValues);

/**
* Calibrate the IMU.
Expand All @@ -188,16 +191,14 @@ class IMU : public IIMU
*/
int16_t convertFromDoubleToInt16(double originalValue);

private:
IMUData m_accelerationValues = {0, 0, 0};
IMUData m_gyroValues = {0, 0, 0};
IMUData m_magnetometerValues = {0, 0, 0};
IMUData m_accelerationValues; /* Struct to store the current raw accelerometer data. */
IMUData m_gyroValues; /* Struct to store the current gyro data. */
IMUData m_magnetometerValues; /* Struct to store the current magnotometer data. */

private:
const SimTime& m_simTime; /** Simulation time. */
webots::Accelerometer* m_accelerometer; /** The accelerometer of Webots. */
webots::Gyro* m_gyro; /** The gyro of Webots. */
webots::Compass* m_magnetometer; /** The magnetometer of Webots. */
const SimTime& m_simTime; /* Simulation time. */
webots::Accelerometer* m_accelerometer; /* The accelerometer of Webots. */
webots::Gyro* m_gyro; /* The gyro of Webots. */
webots::Compass* m_magnetometer; /* The magnetometer of Webots. */
};

/******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion lib/HALTarget/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Board : public IBoard
}

/**
* Get IMU driver.
* Get IMU (=Inertial Measurement Unit) driver.
*
* @return IMU driver
*/
Expand Down
Loading

0 comments on commit af74107

Please sign in to comment.