diff --git a/include/MCU_rev15_defs.h b/include/MCU_rev15_defs.h index eb6308766..6f0f21c52 100644 --- a/include/MCU_rev15_defs.h +++ b/include/MCU_rev15_defs.h @@ -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; diff --git a/lib/interfaces/include/AnalogSensorsInterface.h b/lib/interfaces/include/AnalogSensorsInterface.h index 7ad700a46..e7c2f61b4 100644 --- a/lib/interfaces/include/AnalogSensorsInterface.h +++ b/lib/interfaces/include/AnalogSensorsInterface.h @@ -69,9 +69,9 @@ template class AnalogMultiSensor { private: +protected: + AnalogChannel channels_[N]; public: -// Data - AnalogChannel channels[N]; AnalogConversionPacket_s data; // Functions /// @brief Called by the main loop. Allows AnalogMultiSensor devices not owned by a single system to self-actualize sampling & conversion. @@ -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(); } } diff --git a/lib/interfaces/include/MCP3208.h b/lib/interfaces/include/MCP3208.h deleted file mode 100644 index e90622891..000000000 --- a/lib/interfaces/include/MCP3208.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __MCP3208_H__ -#define __MCP3208_H__ - -#include "AnalogSensorsInterface.h" - -// Definitions -const int MCP3208_DEFAULT_SPI_SDI = 12; -const int MCP3208_DEFAULT_SPI_SDO = 11; -const int MCP3208_DEFAULT_SPI_CLK = 13; -const int MCP3208_DEFAULT_SPI_SPEED = 2000000; -const int MCP3208_NUM_CHANNELS = 8; - -class MCP3208 : public AnalogMultiSensor -{ -private: - const int spiPinCS_; - const int spiPinSDI_; - const int spiPinSDO_; - const int spiPinCLK_; - const int spiSpeed_; -public: -// Constructors - MCP3208(const int spiPinCS, const int spiPinSDI, const int spiPinSDO, const int spiPinCLK, const int spiSpeed); - MCP3208(const int spiPinCS); - -// Functions - void tick(const SysTick_s &tick); - void sample(); -}; - -#endif /* __MCP3208_H__ */ \ No newline at end of file diff --git a/lib/interfaces/include/MCP_ADC.h b/lib/interfaces/include/MCP_ADC.h new file mode 100644 index 000000000..8a9f8f90a --- /dev/null +++ b/lib/interfaces/include/MCP_ADC.h @@ -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 +class MCP_ADC : public AnalogMultiSensor +{ +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__ */ \ No newline at end of file diff --git a/lib/interfaces/include/TelemetryInterface.h b/lib/interfaces/include/TelemetryInterface.h index 73508f046..f64a15810 100644 --- a/lib/interfaces/include/TelemetryInterface.h +++ b/lib/interfaces/include/TelemetryInterface.h @@ -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 ); diff --git a/lib/interfaces/src/MCP3208.cpp b/lib/interfaces/src/MCP3208.cpp deleted file mode 100644 index 3fdf32dd1..000000000 --- a/lib/interfaces/src/MCP3208.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "MCP3208.h" -#include - -MCP3208::MCP3208(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 < MCP3208_NUM_CHANNELS; i++) - { - channels[i] = AnalogChannel(); - } - - pinMode(spiPinCS_, OUTPUT); - pinMode(spiPinSDI_, INPUT); - pinMode(spiPinSDO_, OUTPUT); - pinMode(spiPinCLK_, OUTPUT); - - digitalWrite(spiPinCS_, HIGH); -} - -MCP3208::MCP3208(const int spiPinCS__) -: MCP3208(spiPinCS__, MCP3208_DEFAULT_SPI_SDI, MCP3208_DEFAULT_SPI_SDO, MCP3208_DEFAULT_SPI_CLK, MCP3208_DEFAULT_SPI_SPEED) {} - -void MCP3208::tick(const SysTick_s &tick) -{ - // Sample at 100hz - if (tick.triggers.trigger100) - { - sample(); - convert(); - } -} - -void MCP3208::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 < MCP3208_NUM_CHANNELS; channelIndex++) - { - digitalWrite(spiPinCS_, LOW); - uint16_t value = SPI.transfer16(command | channelIndex << 11); - channels[channelIndex].lastSample = (value & 0x0FFF); - digitalWrite(spiPinCS_, HIGH); - delayMicroseconds(1); // MCP3208 Tcsh = 500ns - } - - SPI.endTransaction(); -} diff --git a/lib/interfaces/src/MCP_ADC.tpp b/lib/interfaces/src/MCP_ADC.tpp new file mode 100644 index 000000000..fde0df7be --- /dev/null +++ b/lib/interfaces/src/MCP_ADC.tpp @@ -0,0 +1,60 @@ +#include "MCP_ADC.h" +#include + +template +MCP_ADC::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::channels_[i] = AnalogChannel(); + } + + pinMode(spiPinCS_, OUTPUT); + pinMode(spiPinSDI_, INPUT); + pinMode(spiPinSDO_, OUTPUT); + pinMode(spiPinCLK_, OUTPUT); + + digitalWrite(spiPinCS_, HIGH); +} + +template +MCP_ADC::MCP_ADC(const int spiPinCS__) +: MCP_ADC(spiPinCS__, MCP_ADC_DEFAULT_SPI_SDI, MCP_ADC_DEFAULT_SPI_SDO, MCP_ADC_DEFAULT_SPI_CLK, MCP_ADC_DEFAULT_SPI_SPEED) {} + +template +void MCP_ADC::tick(const SysTick_s &tick) +{ + // Sample at 100hz + if (tick.triggers.trigger100) + { + MCP_ADC::sample(); + MCP_ADC::convert(); + } +} + +template +void MCP_ADC::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::channels_[channelIndex].lastSample = (value & 0x0FFF); + digitalWrite(spiPinCS_, HIGH); + delayMicroseconds(1); // MCP_ADC Tcsh = 500ns + } + + SPI.endTransaction(); +} diff --git a/lib/interfaces/src/TelemetryInterface.cpp b/lib/interfaces/src/TelemetryInterface.cpp index ad6197080..cdce69f9f 100644 --- a/lib/interfaces/src/TelemetryInterface.cpp +++ b/lib/interfaces/src/TelemetryInterface.cpp @@ -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) { diff --git a/src/main.cpp b/src/main.cpp index c5da9f596..84b669f0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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" @@ -40,9 +40,9 @@ using CircularBufferType = Circular_Buffer ADC1(ADC1_CS); +MCP_ADC<4> ADC2(ADC2_CS); +MCP_ADC<4> ADC3(ADC3_CS); OrbisBR10 steering1(STEERING_SERIAL); @@ -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]); }