diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ccb5e17716b689c5b28033fd4cffd48328e35ce..efa075cf3412dceea6cf076d6f2200e6168ab7f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,7 +109,7 @@ add_executable(test-sensormanager src/tests/test-sensormanager.cpp) sbs_target(test-sensormanager stm32f429zi_death_stack_v2) add_executable(test-serial src/tests/test-serial.cpp) -sbs_target(test-serial stm32f767zi_lyra_biscotto) +sbs_target(test-serial stm32f767zi_lyra_gs) add_executable(test-taskscheduler src/tests/scheduler/test-taskscheduler.cpp) sbs_target(test-taskscheduler stm32f407vg_stm32f4discovery) @@ -302,6 +302,9 @@ sbs_target(test-wiz5500 stm32f767zi_gemini_gs) add_executable(test-bsram src/tests/drivers/test-bsram.cpp) sbs_target(test-bsram stm32f767zi_lyra_biscotto) +add_executable(test-dipswitch src/tests/drivers/test-dipswitch.cpp) +sbs_target(test-dipswitch stm32f767zi_lyra_gs) + #-----------------------------------------------------------------------------# # Tests - Events # #-----------------------------------------------------------------------------# diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 067c01b4ec674882dbf6d7e5eee58a528a112800..2d773978aa5dbfcdb7770a23a6ab0360cc89e521 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -66,6 +66,7 @@ set(BOARDCORE_SRC ${BOARDCORE_PATH}/src/shared/drivers/i2c/I2CDriver-f7.cpp ${BOARDCORE_PATH}/src/shared/drivers/i2c/I2C.cpp ${BOARDCORE_PATH}/src/shared/drivers/WIZ5500/WIZ5500.cpp + ${BOARDCORE_PATH}/src/shared/drivers/DipSwitch/DipSwitch.cpp # Events ${BOARDCORE_PATH}/src/shared/events/EventBroker.cpp diff --git a/src/shared/algorithms/Follower/Follower.cpp b/src/shared/algorithms/Follower/Follower.cpp index a6006fdeea4620a473c9d929c04f631c5b3291f6..7ffc9b200a98bbe7c9d108b7fdf5db9baf989ab3 100644 --- a/src/shared/algorithms/Follower/Follower.cpp +++ b/src/shared/algorithms/Follower/Follower.cpp @@ -174,6 +174,10 @@ void Follower::step() state.verticalSpeed = verticalSpeed; setState(state); + // Log the target angles + Boardcore::Logger::getInstance().log( + static_cast<Boardcore::AntennaAngles>(targetAngles)); + #ifndef NDEBUG std::cout << "[FOLLOWER] STEPPER " << "Angles: [" << state.yaw << ", " << state.pitch << "] " diff --git a/src/shared/drivers/DipSwitch/DipSwitch.cpp b/src/shared/drivers/DipSwitch/DipSwitch.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d33a089e2952f1cace282f86d5f9bd22fa58c10 --- /dev/null +++ b/src/shared/drivers/DipSwitch/DipSwitch.cpp @@ -0,0 +1,61 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Author: Nicolò Caruso + * + * 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. + */ + +#include "DipSwitch.h" + +uint8_t DipSwitch::read() +{ + uint8_t read = 0; + + // Write to the shift register (CS == Not LD) + sh.low(); + miosix::delayUs(microSecClk); + clk.high(); + miosix::delayUs(microSecClk); + sh.high(); + miosix::delayUs(microSecClk); + + // Read first register GS(0)/ARP(1) + + read |= readBit(); + read |= readBit() << 1; + read |= readBit() << 2; + read |= readBit() << 3; + read |= readBit() << 4; + read |= readBit() << 5; + read |= readBit() << 6; + read |= readBit() << 7; + + return read; +} + +uint8_t DipSwitch::readBit() +{ + uint8_t bit; + clk.high(); + miosix::delayUs(microSecClk); + bit = qh.value(); + miosix::delayUs(microSecClk); + clk.low(); + miosix::delayUs(microSecClk); + return bit; +} \ No newline at end of file diff --git a/src/shared/drivers/DipSwitch/DipSwitch.h b/src/shared/drivers/DipSwitch/DipSwitch.h new file mode 100644 index 0000000000000000000000000000000000000000..1d7026f675fefb873acf50351a159a4fb94b7aca --- /dev/null +++ b/src/shared/drivers/DipSwitch/DipSwitch.h @@ -0,0 +1,59 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Author: Nicolò Caruso + * + * 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. + */ + +#include <miosix.h> + +/** + * @brief Dip switch driver to read the current status of the switch + */ +class DipSwitch +{ +public: + DipSwitch(miosix::GpioPin& sh, miosix::GpioPin& clk, miosix::GpioPin& qh, + uint32_t microSecClk) + : sh{sh}, clk{clk}, qh{qh}, microSecClk{microSecClk} + { + } + + /** + * @brief Performs a read of the dipSwitch + * + * @return uint8_t the read of the dip switch in big endian order. Dip 1 as + * most significant value, dip 8 as less significant. + * @note The order may depend on the dipswitch + * @note visually the order in a 8bit word is: + * dip order: [ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ] + * bits order: 12345678 + */ + uint8_t read(); + +private: + miosix::GpioPin& sh; + miosix::GpioPin& clk; + miosix::GpioPin& qh; + uint32_t microSecClk; + + /** + * @brief Reads a bit from the dip switch shift register + */ + uint8_t readBit(); +}; \ No newline at end of file diff --git a/src/tests/drivers/test-dipswitch.cpp b/src/tests/drivers/test-dipswitch.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86b69119e9f56b41e6eadc2eef01b0d1547941d5 --- /dev/null +++ b/src/tests/drivers/test-dipswitch.cpp @@ -0,0 +1,48 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Author: Nicolò Caruso + * + * 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. + */ + +#include <drivers/DipSwitch/DipSwitch.h> +#include <interfaces-impl/hwmapping.h> +#include <miosix.h> + +using namespace miosix; + +/** + * @brief Tests the read from a dipswitch using the DipSwitch driver + */ +int main() +{ + uint32_t microSecClk = 100; + GpioPin sh = dipSwitch::sh::getPin(); + GpioPin clk = dipSwitch::clk::getPin(); + GpioPin qh = dipSwitch::qh::getPin(); + + DipSwitch dipSwitch(sh, clk, qh, microSecClk); + while (true) + { + uint8_t status; + Thread::sleep(1000); + status = dipSwitch.read(); + printf("Read from dipSwitch: %d\n", status); + } + return 0; +}