diff --git a/src/boards/HeliTest/ScreenManager.cpp b/src/boards/HeliTest/ScreenManager.cpp index bb8c29e94e747e8f884c19d9f5877c11f097bd41..641a6b48b3808f2818f719d6273a657d87f5d088 100644 --- a/src/boards/HeliTest/ScreenManager.cpp +++ b/src/boards/HeliTest/ScreenManager.cpp @@ -89,7 +89,7 @@ void ScreenManager::clearScreen() void ScreenManager::drawScreen() { // Disegna titolo - writeLine(0, 0, "ROGALLINA CONTROLLER", green, black); + writeLine(0, 0, "HELI DROP TEST CONTROLLER", green, black); sprintf(buffer, "T: %-10.3f", miosix::getTick() / 1000.0f); @@ -315,6 +315,12 @@ void ScreenManager::drawSensorSamples() writeLine(sl + 12, x1, buffer); sprintf(buffer, "TEMP: %-8.2f", sensorData.bme280_data.temperature); writeLine(sl + 12, (x2 + x3) / 2, buffer); + + // Current sensor + sprintf(buffer, "CSENSE: %-8.3f Max: %-8.3f", + sensorData.current_data.current_value, + sensorData.current_data.current_max_value); + writeLine(sl + 13, 0, buffer); } void ScreenManager::drawSystemStats() diff --git a/src/boards/HeliTest/config.h b/src/boards/HeliTest/config.h index fcac44c9ded7e12e92c462da09315e2c8e558734..cd4f5afbb1f2710e904dde0056f6d38cd72e5564 100644 --- a/src/boards/HeliTest/config.h +++ b/src/boards/HeliTest/config.h @@ -44,7 +44,7 @@ static const PWM::Timer CUTTER_TIM{ using CUTTER_ENA = interfaces::actuators::hbridgel::inh; using CUTTER_IN = interfaces::actuators::servor::ctrl; -using CUTTER_CSENSE = interfaces::actuators::hbridgel::csens; //TODO: This does not work +using CUTTER_CSENSE = miosix::Gpio<GPIOC_BASE, 4>; /* * State machines diff --git a/src/entrypoints/heli-entry.cpp b/src/entrypoints/heli-entry.cpp index 17a02fef44a1c7533b789c97762920c58f27e87d..64cf36c173e3996f3f8b391fec82a3b5d6ce0c1c 100644 --- a/src/entrypoints/heli-entry.cpp +++ b/src/entrypoints/heli-entry.cpp @@ -73,7 +73,7 @@ void initPins() CUTTER_ENA::mode(Mode::OUTPUT); CUTTER_IN::mode(Mode::ALTERNATE); CUTTER_IN::alternateFunction(2); - // CUTTER_CSENSE::mode(Mode::INPUT_ANALOG); + CUTTER_CSENSE::mode(Mode::INPUT_ANALOG); } interfaces::buttons::user::mode(Mode::INPUT); diff --git a/src/shared/SensorManager/SensorManager.cpp b/src/shared/SensorManager/SensorManager.cpp index 577ce7ca8ae9cadd536cf4683688eb7c3ba9e51c..0b7db84882b10722a1b8317f45c008534c30713e 100644 --- a/src/shared/SensorManager/SensorManager.cpp +++ b/src/shared/SensorManager/SensorManager.cpp @@ -64,6 +64,8 @@ SensorManager::~SensorManager() delete gps; if (mpu9255 != nullptr) delete mpu9255; + if (current_sensor != nullptr) + delete current_sensor; } bool SensorManager::init() @@ -101,6 +103,13 @@ bool SensorManager::initSensors() success = false; } + current_sensor = new CurrentSensor(); + if (!current_sensor->init()) + { + TRACE("Init error: CURRENT SENSE\n"); + success = false; + } + return success; } @@ -109,7 +118,7 @@ bool SensorManager::initSamplers() sampler_press_simple.AddSensor(bme280); sampler_imu_simple.AddSensor(mpu9255); sampler_gps_simple.AddSensor(gps); - + sampler_current_simple.AddSensor(current_sensor); return true; } @@ -159,6 +168,17 @@ void SensorManager::startSampling() scheduler->add(simple_press_sampler, 1000 / PRESS_SAMPLE_RATE, TASK_PRESSURE); + // Simple Current Sampler callback and scheduler function + std::function<void()> simple_current_callback = + std::bind(&SensorManager::onSimpleCurrentCallback, this); + + std::function<void()> simple_current_sampler = + std::bind(&SimpleSensorSampler::UpdateAndCallback, + &sampler_current_simple, simple_current_callback); + + scheduler->add(simple_current_sampler, 1000 / ADC_SAMPLE_RATE, + TASK_ADC); + // Simple IMU Sampler callback and scheduler function std::function<void()> simple_imu_callback = std::bind(&SensorManager::onSimpleIMUCallback, this); @@ -208,6 +228,15 @@ void SensorManager::onSimpleIMUCallback() logger.log(*mpu9255->getIMUDataPtr()); } +void SensorManager::onSimpleCurrentCallback() +{ + // Log data & update sensor data struct + data.current_data = *(current_sensor->getCurrentDataPtr()); + + if (logSensors) + logger.log(data.current_data); +} + void SensorManager::onSimpleGPSCallback() { // Log data & update sensor data struct diff --git a/src/shared/SensorManager/SensorManager.h b/src/shared/SensorManager/SensorManager.h index ed5a3ae1d2931b0be7e121ca10eb9ca8baceace3..f195053bed31e9f6952d10cc9dfceab5f7628840 100644 --- a/src/shared/SensorManager/SensorManager.h +++ b/src/shared/SensorManager/SensorManager.h @@ -31,6 +31,7 @@ #include "Sensors/gps.h" #include "hwmapping.h" #include "sensors/SensorSampling.h" +#include "Sensors/CurrentSensor.h" // Forward declarations typedef BME280<RogallinaBoard::interfaces::sensors::bme280::spi> BME280Type; @@ -49,6 +50,7 @@ struct SensorData BME280Data bme280_data; MPU9255Data mpu9255_data; GPSData gps_data; + CurrentSenseData current_data; }; /** @@ -117,6 +119,11 @@ private: */ void onSimplePressCallback(); + /** + * Simple, 20 Hz Current sense Callback. + */ + void onSimpleCurrentCallback(); + /** * Simple, 200 Hz SensorSampler Callback. */ @@ -126,11 +133,13 @@ private: SimpleSensorSampler sampler_gps_simple; SimpleSensorSampler sampler_press_simple; SimpleSensorSampler sampler_imu_simple; + SimpleSensorSampler sampler_current_simple; // Sensors BME280Type* bme280 = nullptr; MPU9255* mpu9255 = nullptr; GPS* gps = nullptr; + CurrentSensor* current_sensor = nullptr; SensorData data; diff --git a/src/shared/Sensors/CurrentSenseData.h b/src/shared/Sensors/CurrentSenseData.h new file mode 100644 index 0000000000000000000000000000000000000000..6a224c3d2c7f3d167361707d2f8091215581e83c --- /dev/null +++ b/src/shared/Sensors/CurrentSenseData.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Skyward Experimental Rocketry + * Authors: Luca Erbetta + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +struct CurrentSenseData +{ + long long timestamp = 0; + + float current_value = 0; + float current_max_value = 0; +}; \ No newline at end of file diff --git a/src/shared/Sensors/CurrentSensor.h b/src/shared/Sensors/CurrentSensor.h new file mode 100644 index 0000000000000000000000000000000000000000..cc7ac0a73ec9f55a320965e7f2a23873edaa6e2f --- /dev/null +++ b/src/shared/Sensors/CurrentSensor.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2019 Skyward Experimental Rocketry + * Authors: Luca Erbetta + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <drivers/adc/ADC.h> +#include <sensors/Sensor.h> + +#include "CurrentSenseData.h" +#include "SharedConfig.h" + +/** + * Abstracts the current sense reading on the adc as if it was an + * independent sensor. + */ +class CurrentSensor : public Sensor +{ +public: + CurrentSensor() { memset(¤t_data, 0, sizeof(CurrentSenseData)); } + + bool init() override { return true; } + + bool selfTest() override { return true; } + + /** + * @brief Converts the channels associated with the current sense. + */ + bool onSimpleUpdate() override + { + current_data.current_value = adc.convertChannel(CS_CHANNEL); + if(current_data.current_value > current_data.current_max_value) + { + current_data.current_max_value = current_data.current_value; + } + + return true; + } + + /** + * @brief Returns a pointer to the last conveted current sense value + */ + CurrentSenseData* getCurrentDataPtr() { return ¤t_data; } + +private: + /*float adcToCurrent(uint16_t adc_in) + { + float v = (adc_in * 3.3f) / 4096; + float iout = v / 525; + return (iout - 0.000030) * 10000; + }*/ + + const ADC_t::Channel CS_CHANNEL = + static_cast<ADC_t::Channel>(CURRENT_SENSE_ADC_CH); + + ADC_t adc{}; + CurrentSenseData current_data; +}; \ No newline at end of file diff --git a/src/shared/SharedConfig.h b/src/shared/SharedConfig.h index 21eacf38aadf403fc35fe3c8569385edb5f03a82..df45f19fe357b0b4473d1216fac53374d44fb0c0 100644 --- a/src/shared/SharedConfig.h +++ b/src/shared/SharedConfig.h @@ -22,6 +22,8 @@ #pragma once +#include <drivers/adc/ADC.h> +#include <miosix.h> /* * GLOBAL SETTINGS */ @@ -44,6 +46,14 @@ static const int PRESS_SAMPLE_RATE = 50; static constexpr int ADC_SAMPLE_RATE = 20; +using CURRENT_CSENSE = miosix::Gpio<GPIOC_BASE, 4>; + +static constexpr unsigned int CURRENT_SENSE_ADC_NUM = 1; +static constexpr unsigned int CURRENT_SENSE_ADC_CH = 14; + +using ADC_t = SensorADC<CURRENT_SENSE_ADC_NUM>; + + #if !defined SD_LOGGING && !defined DEBUG #error "SD LOGGER IS NOT ENABLED" //If we are compiling for release, SD logger should be enabled #endif \ No newline at end of file