Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions Arduino_BHY2Host/src/Arduino_BHY2Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,121 @@
#endif



/**
* @brief Enumeration for defining wiring configuration over ESLOV, Shield or BLE.
*
* For NICLA_AS_SHIELD configuration, see https://docs.arduino.cc/tutorials/nicla-sense-me/use-as-mkr-shield
*
*/
enum NiclaWiring {
NICLA_VIA_ESLOV = 0,
NICLA_AS_SHIELD,
NICLA_VIA_BLE
};


/**
* @brief Main class for initialising communication with sensors
*/
class Arduino_BHY2Host {
public:
Arduino_BHY2Host();
virtual ~Arduino_BHY2Host();

// Necessary API. Update function should be continuously polled if PASSTHORUGH is ENABLED
/**
* @brief
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brief description missing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief
* @brief Establishes a connection with the client (Nicla Sense ME) using the given type of communication (ESLOV / BLE / As shield).

*
* @param passthrough Enable Serial port at 115200 bps
* @param niclaConnection Configuration for set @ref NiclaWiring state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param niclaConnection Configuration for set @ref NiclaWiring state
* @param niclaConnection Configuration for setting @ref NiclaWiring state. This is useful to configure the board to communicate either via ESLOV, BLE or both. It's also possible to mount the Nicla Sense ME as a shield. In that case you need to pass `NICLA_AS_SHIELD` to this parameter.

* @return true Connection successful to the Nicla board
* @return false Connection unsuccessful to the Nicla board
Comment on lines +50 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to our style guide we should use a single @return statement.

*/
bool begin(bool passthrough = false, NiclaWiring niclaConnection = NICLA_VIA_ESLOV);
/**
* @brief Pings sensor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Pings sensor
* @brief Requests new sensor data from the client (Nicla Sense ME) and saves it locally so it can be retrieved via `Sensor` objects.

*
* @note Sensor must be continuously polled to prevent sleep
*/
void update();
/**
* @brief Pings sensor and then sleep
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Pings sensor and then sleep
* @brief Requests new sensor data from the client (Nicla Sense ME) and saves it locally so it can be retrieved via `Sensor` objects. The Nicla Sense ME's sensors are then put to sleep for the given amount of milliseconds.

*
* @note Delay does not stall microcontroller to sleep. See https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay
*/
void update(unsigned long ms); // Update and then sleep

// Functions for controlling the BHY when PASSTHROUGH is DISABLED
/**
* @brief Poll Bosch method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Poll Bosch method
* @brief Configures a sensor to be read using a given sample rate and latency. The configuration is packaged up in a `SensorConfigurationPacket` object.
* @see SensorConfigurationPacket

*
* @param config Sensor configuration
*/
void configureSensor(SensorConfigurationPacket& config);
/**
* @brief Setup virtual sensors on the BHI260
*
* @param sensorId Sensor ID for sensor
* @param sampleRate Polling rate for sensor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aliphys Please double check the unit of this.

* @param latency Latency in milliseconds
*
* @note For list of SensorID, see src/SensorID.h
*/
void configureSensor(uint8_t sensorId, float sampleRate, uint32_t latency);
/**
* @brief Recieve acknowledgement from Nicla board over ESLOV
*
* @return uint8_t Data read from I2C bus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return uint8_t Data read from I2C bus
* @return uint8_t One byte of data read from the I2C bus.

*/
uint8_t requestAck();
/**
* @brief Return available sensor data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Return available sensor data
* @brief Returns amount of available sensor data in bytes.

*
* @return uint8_t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return uint8_t
* @return uint8_t Amount of bytes.

*/
uint8_t availableSensorData();
/**
* @brief Return available long sensor data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Return available long sensor data
* @brief Returns the amount of 16 bit chunks of the available sensor data.

*
* @return uint8_t
*/
uint8_t availableSensorLongData();
/**
* @brief Read sensor data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and saves it into a SensorDataPacket that is passed by reference.
Where does it read the data from?

*
* @param data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to construct a data packet?

* @return true
* @return false
Comment on lines +106 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the success / error case refer to? Success in parsing the data?

*/
bool readSensorData(SensorDataPacket &data);
/**
* @brief Read long sensor data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as for readSensorData()

*
* @param data
* @return true
* @return false
*/
bool readSensorLongData(SensorLongDataPacket &data);

/**
* @brief Parse XYZ Cartesian data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it takes the SensorDataPacket data, parses it and then saves it to the DataXYZ vector?

*
* @param data Data packet including SensorID
* @param vector vector with XYZ
*/
void parse(SensorDataPacket& data, DataXYZ& vector);
/**
* @brief Parse orientation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. How does it construct the vector data? Does it use data as input, parses it and then write the values to vector?

*
* @param data Data packet including SensorID
* @param vector Vector with heading, pitch and roll
*/
void parse(SensorDataPacket& data, DataOrientation& vector);
/**
* @brief Parse orientation with scale factor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do I need a scale factor?

*
* @param data Data packet including SensorID
* @param vector Vector with heading, pitch and roll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I access them? vector.heading vector.pitch vector.roll ?

* @param scaleFactor scale factor for vector
*/
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);

NiclaWiring getNiclaConnection();
Expand Down
29 changes: 28 additions & 1 deletion Arduino_BHY2Host/src/BLEHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,45 @@
#include "DFUTypes.h"
#include "ArduinoBLE.h"

/**
* @brief Class for handling BLE communication for both sensor data transfer and DFU
*
*/

class BLEHandler {
public:
BLEHandler();
virtual ~BLEHandler();

/**
* @brief Start BLE connection to the Nicla board
*
* @return true Connection successful to the Nicla board
* @return false Connection unsuccessful to the Nicla board
*/
bool begin();
/**
* @brief Poll data over Bluetooth
*
*/
void update();
/**
* @brief Disconnect BLE connection between host and Nicla board
*
*/
void end();

/**
* @brief Specify which sensor to poll data from on the Nicla Sense ME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more like it sends out sensor configuration data to the client, doesn't it?

*
* @param config Configuration for the Sensor including sensor ID, sample rate and latency
*/
void writeConfigPacket(SensorConfigurationPacket& config);

private:
/**
* @brief The Arduino_BHY2Host class can accces both private and public methods of BLEHandler
*
*/
friend class Arduino_BHY2Host;
Comment on lines +44 to 48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictuly necessary as this is a regular C++ feature.


static void receivedSensorData(BLEDevice central, BLECharacteristic characteristic);
Expand Down
87 changes: 83 additions & 4 deletions Arduino_BHY2Host/src/EslovHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@

#define I2C_INT_PIN (0)

/**
* @brief Enumerator for ESLOV operational state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are they used for?

*
*/
enum EslovOpcode {
ESLOV_DFU_INTERNAL_OPCODE,
ESLOV_DFU_EXTERNAL_OPCODE,
ESLOV_SENSOR_CONFIG_OPCODE,
ESLOV_SENSOR_STATE_OPCODE
ESLOV_DFU_INTERNAL_OPCODE, /*!< ESLOV DFU ANNA-B112 */
ESLOV_DFU_EXTERNAL_OPCODE, /*!< ESLOV DFU BHY260 */
ESLOV_SENSOR_CONFIG_OPCODE, /*!< ESLOV Sensor Configuration */
ESLOV_SENSOR_STATE_OPCODE /*!< ESLOV Sensor State */
};


/**
* @brief Enumeration for Host-Nicla configuration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between these and the normal Opcodes?

*
*/
enum HostOpcode {
HOST_DFU_INTERNAL_OPCODE = ESLOV_DFU_INTERNAL_OPCODE,
HOST_DFU_EXTERNAL_OPCODE = ESLOV_DFU_EXTERNAL_OPCODE,
Expand All @@ -28,6 +37,11 @@ enum HostOpcode {
HOST_READ_LONG_SENSOR_OPCODE
};


/**
* @brief Enumeration for various states over ESLOV
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to regulate what exactly?

*
*/
enum EslovState {
ESLOV_AVAILABLE_SENSOR_STATE = 0x00,
ESLOV_READ_SENSOR_STATE = 0x01,
Expand All @@ -43,19 +57,80 @@ class EslovHandler {
EslovHandler();
virtual ~EslovHandler();

/**
* @brief Start I2C communication with Nicla Sense ME Board
*
* @param passthrough Enable Serial port at 115200 bps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What data is passed through to where and which serial port is opened the client one or host one?

*
* @return true I2C communication initialised successfully.
* @return false Failure in starting communication over ESLOV protocol.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESLOV is not a protocol

*/
bool begin(bool passthrough);
/**
* @brief When Serial port is avaliable, then based on the defined opcode write data
*
*/
void update();

/**
* @brief Send DFU data over ESLOV. On the last packet, the built-in LED is pulled down.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the LED pulled down (turned off)?

*
* @param data data to be send for DFU
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param data data to be send for DFU
* @param data data to be send for DFU as a byte array.

* @param length total length of data to be sent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length in bytes I assume?

*/
void writeDfuPacket(uint8_t *data, uint8_t length);
/**
* @brief Configure the Nicla device to run in a specific state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these states used for?

*
* @param state Configuration passed to Nicla based on @ref EslovState enumerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param state Configuration passed to Nicla based on @ref EslovState enumerator
* @param state State value sent to Nicla Sense ME based on @ref EslovState enumerator.

*/
void writeStateChange(EslovState state);
/**
* @brief Specify which sensor to poll data from on the Nicla Sense ME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it only to enable / disable the sensors? Also to configure latency and sample rate, no?

*
* @param config Configuration for the Sensor including sensor ID, sample rate and latency
*/
void writeConfigPacket(SensorConfigurationPacket& config);
/**
* @brief Recieve acknowledgement from Nicla board over ESLOV
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Recieve acknowledgement from Nicla board over ESLOV
* @brief Request acknowledgement byte from the Nicla Sense ME over ESLOV to confirm the previously sent packet was received.

*
* @return uint8_t Data read from I2C bus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the returned number stand for?

*/
uint8_t requestPacketAck();
/**
* @brief Obtain data sent over ESLOV to the host board
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requests only the available bytes, no?

*
* @return uint8_t Data read from I2C bus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it return the data or the number of bytes available?

*/
uint8_t requestAvailableData();
/**
* @brief Obtain long data sent over ESLOV to the host board
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as for requestAvailableData()

*
* @return uint8_t Data read from I2C bus
*/
uint8_t requestAvailableLongData();
/**
* @brief Request data from the Nicla board
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for a specific sensor, right? So do I need to pass the sensor ID or how does this work?

*
* @param sData
* @return true Request successful
* @return false Request unsuccessful
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transmission error only or also configuration error?

*/
bool requestSensorData(SensorDataPacket &sData);
/**
* @brief Request long data from the Nicla board
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as for requestSensorData()

*
* @param sData
* @return true Request successful
* @return false Request unsuccessful
*/
bool requestSensorLongData(SensorLongDataPacket &sData);

protected:
/**
* @brief Nicla Sense ME is mounted as a Shield
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the function do? Does it set some pins, change some configuration, ...?

*
*/
void niclaAsShield();

private:
Expand All @@ -68,6 +143,10 @@ class EslovHandler {
bool _dfuLedOn;

private:
/**
* @brief The Arduino_BHY2Host class can accces both private and public methods of EslovHandler
*
*/
friend class Arduino_BHY2Host;

void flushWire();
Expand Down