Skip to content

Commit

Permalink
templated MCP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dopp-IO committed Feb 6, 2024
1 parent 33c0b3e commit 19be0e5
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 104 deletions.
2 changes: 1 addition & 1 deletion include/MCU_rev15_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const int ADC1_CS = 34;
const int ADC2_CS = 33;
const int ADC3_CS = 29;
const HardwareSerial* STEERING_SERIAL = &Serial5;
HardwareSerial* STEERING_SERIAL = &Serial5;
const int SOFTWARE_OK = 28;
const int WATCHDOG_INPUT = 32;

Expand Down
6 changes: 3 additions & 3 deletions lib/interfaces/include/AnalogSensorsInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ template <int N>
class AnalogMultiSensor
{
private:
protected:
AnalogChannel channels_[N];
public:
// Data
AnalogChannel channels[N];
AnalogConversionPacket_s<N> data;
// Functions
/// @brief Called by the main loop. Allows AnalogMultiSensor devices not owned by a single system to self-actualize sampling & conversion.
Expand All @@ -90,7 +90,7 @@ class AnalogMultiSensor
{
for (int i = 0; i < N; i++)
{
data.conversions[i] = channels[i].convert();
data.conversions[i] = channels_[i].convert();
}
}

Expand Down
31 changes: 0 additions & 31 deletions lib/interfaces/include/MCP3208.h

This file was deleted.

33 changes: 33 additions & 0 deletions lib/interfaces/include/MCP_ADC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __MCP_ADC_H__
#define __MCP_ADC_H__

#include "AnalogSensorsInterface.h"

// Definitions
const int MCP_ADC_DEFAULT_SPI_SDI = 12;
const int MCP_ADC_DEFAULT_SPI_SDO = 11;
const int MCP_ADC_DEFAULT_SPI_CLK = 13;
const int MCP_ADC_DEFAULT_SPI_SPEED = 2000000;

template <int MCP_ADC_NUM_CHANNELS>
class MCP_ADC : public AnalogMultiSensor<MCP_ADC_NUM_CHANNELS>
{
private:
const int spiPinCS_;
const int spiPinSDI_;
const int spiPinSDO_;
const int spiPinCLK_;
const int spiSpeed_;
public:
// Constructors
MCP_ADC(int spiPinCS, const int spiPinSDI, const int spiPinSDO, const int spiPinCLK, const int spiSpeed);
MCP_ADC(int spiPinCS);

// Functions
void tick(const SysTick_s &tick);
void sample();
};

#include "MCP_ADC.tpp"

#endif /* __MCP_ADC_H__ */
4 changes: 2 additions & 2 deletions lib/interfaces/include/TelemetryInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class TelemetryInterface
void tick(
const SysTick_s &tick,
const AnalogConversionPacket_s<8> &adc1,
const AnalogConversionPacket_s<8> &adc2,
const AnalogConversionPacket_s<8> &adc3,
const AnalogConversionPacket_s<4> &adc2,
const AnalogConversionPacket_s<4> &adc3,
const SteeringEncoderConversion_s &encoder
);

Expand Down
56 changes: 0 additions & 56 deletions lib/interfaces/src/MCP3208.cpp

This file was deleted.

60 changes: 60 additions & 0 deletions lib/interfaces/src/MCP_ADC.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "MCP_ADC.h"
#include <SPI.h>

template <int MCP_ADC_NUM_CHANNELS>
MCP_ADC<MCP_ADC_NUM_CHANNELS>::MCP_ADC(const int spiPinCS, const int spiPinSDI, const int spiPinSDO, const int spiPinCLK, const int spiSpeed)
: spiPinCS_(spiPinCS)
, spiPinSDI_(spiPinSDI)
, spiPinSDO_(spiPinSDO)
, spiPinCLK_(spiPinCLK)
, spiSpeed_(spiSpeed)
{
for (int i = 0; i < MCP_ADC_NUM_CHANNELS; i++)
{
MCP_ADC<MCP_ADC_NUM_CHANNELS>::channels_[i] = AnalogChannel();
}

pinMode(spiPinCS_, OUTPUT);
pinMode(spiPinSDI_, INPUT);
pinMode(spiPinSDO_, OUTPUT);
pinMode(spiPinCLK_, OUTPUT);

digitalWrite(spiPinCS_, HIGH);
}

template <int MCP_ADC_NUM_CHANNELS>
MCP_ADC<MCP_ADC_NUM_CHANNELS>::MCP_ADC(const int spiPinCS__)
: MCP_ADC<MCP_ADC_NUM_CHANNELS>(spiPinCS__, MCP_ADC_DEFAULT_SPI_SDI, MCP_ADC_DEFAULT_SPI_SDO, MCP_ADC_DEFAULT_SPI_CLK, MCP_ADC_DEFAULT_SPI_SPEED) {}

template <int MCP_ADC_NUM_CHANNELS>
void MCP_ADC<MCP_ADC_NUM_CHANNELS>::tick(const SysTick_s &tick)
{
// Sample at 100hz
if (tick.triggers.trigger100)
{
MCP_ADC<MCP_ADC_NUM_CHANNELS>::sample();
MCP_ADC<MCP_ADC_NUM_CHANNELS>::convert();
}
}

template <int MCP_ADC_NUM_CHANNELS>
void MCP_ADC<MCP_ADC_NUM_CHANNELS>::sample()
{
uint16_t command = (
(0b1 << 15) | // start bit
(0b1 << 14) // single ended mode
);

SPI.beginTransaction(SPISettings(spiSpeed_, MSBFIRST, SPI_MODE0));

for (int channelIndex = 0; channelIndex < MCP_ADC_NUM_CHANNELS; channelIndex++)
{
digitalWrite(spiPinCS_, LOW);
uint16_t value = SPI.transfer16(command | channelIndex << 11);
MCP_ADC<MCP_ADC_NUM_CHANNELS>::channels_[channelIndex].lastSample = (value & 0x0FFF);
digitalWrite(spiPinCS_, HIGH);
delayMicroseconds(1); // MCP_ADC Tcsh = 500ns
}

SPI.endTransaction();
}
4 changes: 2 additions & 2 deletions lib/interfaces/src/TelemetryInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ void TelemetryInterface::update_analog_readings_CAN_msg(const SteeringEncoderCon
/* Tick SysClock */
void TelemetryInterface::tick(const SysTick_s &tick,
const AnalogConversionPacket_s<8> &adc1,
const AnalogConversionPacket_s<8> &adc2,
const AnalogConversionPacket_s<8> &adc3,
const AnalogConversionPacket_s<4> &adc2,
const AnalogConversionPacket_s<4> &adc3,
const SteeringEncoderConversion_s &encoder) {

if (tick.triggers.trigger50) {
Expand Down
18 changes: 9 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/* Interfaces */
#include "HytechCANInterface.h"
#include "MCP3208.h"
#include "MCP_ADC.h"
#include "ORBIS_BR10.h"
#include "MCUInterface.h"
#include "AMSInterface.h"
Expand Down Expand Up @@ -40,9 +40,9 @@ using CircularBufferType = Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_mes


/* Sensors */
MCP3208 ADC1(ADC1_CS);
MCP3208 ADC2(ADC2_CS);
MCP3208 ADC3(ADC3_CS);
MCP_ADC<8> ADC1(ADC1_CS);
MCP_ADC<4> ADC2(ADC2_CS);
MCP_ADC<4> ADC3(ADC3_CS);
OrbisBR10 steering1(STEERING_SERIAL);


Expand Down Expand Up @@ -193,16 +193,16 @@ void sample_all_external_readings() {
ADC2.tick(curr_tick);
ADC3.tick(curr_tick);
// Tick steering system
steering_system.tick(curr_tick, ADC1.channels[MCU15_STEERING_CHANNEL].convert());
steering_system.tick(curr_tick, ADC1.get().conversions[MCU15_STEERING_CHANNEL]);
// Read shutdown circuits
main_ecu.read_mcu_status();
}

void process_all_value_readings() {
pedals.tick(curr_tick,
ADC1.channels[MCU15_ACCEL1_CHANNEL].convert(),
ADC1.channels[MCU15_ACCEL2_CHANNEL].convert(),
ADC1.channels[MCU15_BRAKE1_CHANNEL].convert(),
ADC1.channels[MCU15_BRAKE2_CHANNEL].convert());
ADC1.get().conversions[MCU15_ACCEL1_CHANNEL],
ADC1.get().conversions[MCU15_ACCEL2_CHANNEL],
ADC1.get().conversions[MCU15_BRAKE1_CHANNEL],
ADC1.get().conversions[MCU15_BRAKE2_CHANNEL]);
}

0 comments on commit 19be0e5

Please sign in to comment.