Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Josephs/wired module big refactor #128

Open
wants to merge 3 commits into
base: josephs/wired-module
Choose a base branch
from
Open
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
21 changes: 7 additions & 14 deletions wired_module/wired_module_arduino/include/BarometerSensor.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#include "I2cSensorBase.h"
#pragma once

#ifndef BAROMETER_SENSOR
#define BAROMETER_SENSOR
#include "I2cSensorBase.h"

class BarometerSensor : public I2cSensorBase {
public:
// Attributes
const int scaleFactor = 524288;
int16_t c0;
int16_t c1;
BarometerSensor(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID);

// Use parent constructor
using I2cSensorBase::I2cSensorBase;

// Methods
void configure() override;
void read() override;
};

#endif
private:
int16_t c0;
int16_t c1;
};
13 changes: 3 additions & 10 deletions wired_module/wired_module_arduino/include/I2cMaster.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#include "driver/i2c.h"
#pragma once

#ifndef I2C_MASTER
#define I2C_MASTER
#include <driver/i2c.h>

class I2cMaster {
public:
// Attributes
i2c_port_t portNum;

// Constructor
I2cMaster(i2c_port_t portNum, int sda, int scl, int clockFrequency);

// Destructor
~I2cMaster();
};

#endif
};
23 changes: 11 additions & 12 deletions wired_module/wired_module_arduino/include/I2cSensorBase.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#pragma once

#include "SensorBase.h"
#include "driver/i2c.h"
#include "constants.h"

#ifndef I2C_BASE
#define I2C_BASE
#include <driver/i2c.h>

class I2cSensorBase : public SensorBase {
public:
// Attributes
uint8_t sensorAddress;
i2c_port_t masterPortNum;
uint8_t readBuffer[8];

// Constructor
explicit I2cSensorBase(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID);

// Methods
void read_sensor_register(uint8_t registerAddress, size_t readLength, int timeout);
void write_sensor_register(uint8_t registerAddress, uint8_t data, int timeout);
};

#endif
protected:
uint8_t readBuffer[I2CConfig::READ_BUFFER_SIZE];

private:
uint8_t sensorAddress;
i2c_port_t masterPortNum;
};
14 changes: 4 additions & 10 deletions wired_module/wired_module_arduino/include/MpuSensor.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include "I2cSensorBase.h"
#pragma once

#ifndef MPU_SENSOR
#define MPU_SENSOR
#include "I2cSensorBase.h"

class MpuSensor : public I2cSensorBase {
public:
// Use parent constructor
using I2cSensorBase::I2cSensorBase;
explicit MpuSensor(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID);

// Methods
void configure() override;
void read() override;
};

#endif
};
29 changes: 11 additions & 18 deletions wired_module/wired_module_arduino/include/SensorBase.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
#include <CAN.h>
#pragma once

#include "constants.h"

#ifndef SENSOR_BASE
#define SENSOR_BASE
#include <CAN.h>

// Abstract Class
class SensorBase {
public:
// Attributes
uint8_t canBuffer[4];
uint8_t sensorID;
explicit SensorBase(uint8_t sensorID);

// Abstract methods
virtual void configure() = 0;
virtual void read() = 0;
void send();

// Methods
void send() {
CAN.beginPacket(this->sensorID);
this->read(); // Read data from sensor and store in buffer
CAN.write(this->canBuffer, sizeof(this->canBuffer)); // Send packet
CAN.endPacket();
}
};
protected:
uint8_t canBuffer[CANConfig::PACKET_SIZE];

#endif
private:
uint8_t sensorID;
};
29 changes: 29 additions & 0 deletions wired_module/wired_module_arduino/include/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <driver/i2c.h>

namespace CANConfig {
const int RX_PIN = 16;
const int TX_PIN = 17;
const double BAUD_RATE = 500E3;
const int PACKET_SIZE = 4;
}

namespace I2CConfig {
const i2c_port_t NUM = I2C_NUM_0;
const int SCL = GPIO_NUM_22;
const int SDA = GPIO_NUM_21;
const int FREQ_HZ = 400000;
const int READ_BUFFER_SIZE = 8;
}

namespace MPUConfig {
const int ID = 0x13;
const int ADDR = 0x68;
}

namespace BarometerConfig {
const int ID = 0x11;
const int ADDR = 0x76;
const int SCALE_FACTOR = 524288;
}
3 changes: 3 additions & 0 deletions wired_module/wired_module_arduino/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void onReceive(int packetSize);
24 changes: 0 additions & 24 deletions wired_module/wired_module_arduino/src/MpuSensor.cpp

This file was deleted.

61 changes: 15 additions & 46 deletions wired_module/wired_module_arduino/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,42 @@
#include <BarometerSensor.h>
#include "BarometerSensor.h"
#include "I2cMaster.h"
#include "MpuSensor.h"
#include "utils.h"

#include <CAN.h>
#include <I2cMaster.h>
#include <MpuSensor.h>
#include <driver/i2c.h>

#include <vector>

#include "driver/i2c.h"

#define I2C_NUM I2C_NUM_0
#define I2C_SCL GPIO_NUM_22
#define I2C_SDA GPIO_NUM_21

// Function prototypes
static esp_err_t i2c_master_init(void);
void onReceive(int packetSize);
void setup();
void loop();

// ==================================================================
// SETUP SENSORS HERE
// ==================================================================
I2cMaster i2cMaster(I2C_NUM, I2C_SDA, I2C_SCL, 400000);
MpuSensor mpuSensor(i2cMaster.portNum, 0x68, 0x13);
BarometerSensor barometerSensor(i2cMaster.portNum, 0x76, 0x11);
std::vector<SensorBase*> sensors = {&mpuSensor, &barometerSensor};
// ==================================================================

void onReceive(int packetSize) {
// Get all bytes from packet
uint8_t buffer[4];
int i = 0;
while (CAN.available()) {
buffer[i] = CAN.read();
i++;
};
I2cMaster i2cMaster(I2CConfig::NUM, I2CConfig::SDA, I2CConfig::SCL, I2CConfig::FREQ_HZ);

// Convert bytes to float
float data;
memcpy(&data, buffer, sizeof(float));
MpuSensor mpuSensor(i2cMaster.portNum, MPUConfig::ADDR, MPUConfig::ID);
BarometerSensor barometerSensor(i2cMaster.portNum, BarometerConfig::ADDR, BarometerConfig::ID);

Serial.printf("Msg received | ID = 0x%lx | Data = %f\n", CAN.packetId(), data);
}
std::vector<SensorBase *> sensors = {&mpuSensor, &barometerSensor};
// ==================================================================

void setup() {
// Setup CAN bus
CAN.setPins(16, 17);
CAN.setPins(CANConfig::RX_PIN, CANConfig::TX_PIN);
Serial.begin(115200);
while (!Serial);

// Start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
// Start CAN bus
if (!CAN.begin(CANConfig::BAUD_RATE)) {
Serial.println("\n\nStarting CAN failed!\n\n");
while (1);
}

// Configure can bus to loopback mode for self test
// DELETE WHEN CONNECTED TO RASP PI (used for self testing)
CAN.loopback();

// Set up receive callback, for self testing
CAN.onReceive(onReceive);

// Configure all sensors
for (auto sensor : sensors) {
sensor->configure();
}
}

void loop() {
// Send CAN bus from all sensors
for (auto sensor : sensors) {
sensor->send();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "BarometerSensor.h"
#include "constants.h"

#include <cstring>

void BarometerSensor::configure() {
// TODO: Add comments and remove magic numbers

BarometerSensor::BarometerSensor(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID)
: I2cSensorBase(masterPortNum, sensorAddress, sensorID) {
read_sensor_register(0x10, 3, 1000);

this->c0 = this->readBuffer[0] << 4 | this->readBuffer[1] >> 4;
if (this->c0 & (1 << 11))
this->c0 = this->c0 | 0XF000;
Expand All @@ -14,20 +19,16 @@ void BarometerSensor::configure() {
this->c1 = this->c1 | 0XF000;
}

void BarometerSensor ::read() {
// Get raw data from sensor
void BarometerSensor::read() {
read_sensor_register(0x03, 3, 1000);

int32_t raw_temp = (this->readBuffer[0] << 8) | this->readBuffer[1];
raw_temp = (raw_temp << 8) | this->readBuffer[2];

if (raw_temp & (1 << 23)) {
raw_temp = raw_temp | 0XFF000000;
}
if (raw_temp & (1 << 23)) {raw_temp = raw_temp | 0XFF000000;}

float temp = (float)raw_temp / (float)this->scaleFactor;
float temp = (float)raw_temp / (float)BarometerConfig::SCALE_FACTOR;
temp = this->c0 * 0.5 + this->c1 * temp;

// Write to CAN buffer
memcpy(this->canBuffer, &temp, sizeof(temp));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "I2cMaster.h"

I2cMaster::I2cMaster(i2c_port_t portNum, int sda, int scl, int clockFrequency) {
this->portNum = portNum;
I2cMaster::I2cMaster(i2c_port_t portNum, int sda, int scl, int clockFrequency)
: portNum(portNum) {
i2c_config_t masterConfig = {
.mode = I2C_MODE_MASTER,
.sda_io_num = sda,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include "I2cSensorBase.h"

I2cSensorBase::I2cSensorBase(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID) {
this->masterPortNum = masterPortNum;
this->sensorAddress = sensorAddress;
this->sensorID = sensorID;
}
I2cSensorBase::I2cSensorBase(i2c_port_t masterPortNum, uint8_t sensorAddress, uint8_t sensorID)
: SensorBase(sensorID), sensorAddress(sensorAddress), masterPortNum(masterPortNum) {}

void I2cSensorBase::read_sensor_register(uint8_t registerAddress, size_t readLength, int timeout) {
i2c_master_write_read_device(this->masterPortNum, this->sensorAddress, &registerAddress,
1, this->readBuffer, readLength, timeout);
}

void I2cSensorBase::write_sensor_register(uint8_t registerAddress, uint8_t data, int timeout) {
uint8_t writeBuf[2]; // writeBuf[len+1];
uint8_t writeBuf[2];
writeBuf[0] = registerAddress;
writeBuf[1] = data;
i2c_master_write_to_device(this->masterPortNum, this->sensorAddress, writeBuf, 2, timeout);
Expand Down
Loading