diff --git a/pico/inc/devices/stepper-motor.hpp b/pico/inc/devices/stepper-motor.hpp new file mode 100644 index 0000000000000000000000000000000000000000..3aa29e2ebcbd27d98d7542988bae8da2b896cfc7 --- /dev/null +++ b/pico/inc/devices/stepper-motor.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "pico/stdlib.h" +#include <cstdlib> + +class Motor { + public: + Motor(uint Pin_A, uint Pin_B, uint Pin_C, uint Pin_D, uint stepsPerRevolution = 4096, uint delayMs = 2); + void initialize(); + void turnSteps(int steps, bool reverse = false); + + private: + uint Pin_A, Pin_B, Pin_C, Pin_D; // GPIO pins + uint stepsPerRevolution; // Steps per full revolution + uint delayMs; // Delay between steps (in ms) + uint8_t currentStep; // Current step index + const uint8_t stepSequence[8][4] = { + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1}, + {1, 0, 0, 1} + }; + + void motorStep(uint8_t stepIndex); + uint8_t adjustStep(uint8_t currentStep, bool reverse); +}; \ No newline at end of file diff --git a/pico/src/devices/stepper-motor.cpp b/pico/src/devices/stepper-motor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f144c1599af6c972914f6c767a3c981114bac08 --- /dev/null +++ b/pico/src/devices/stepper-motor.cpp @@ -0,0 +1,39 @@ +#include "stepper-motor.hpp" + +Motor::Motor(uint Pin_A, uint Pin_B, uint Pin_C, uint Pin_D, uint stepsPerRevolution, uint delayMs) + : Pin_A(Pin_A), Pin_B(Pin_B), Pin_C(Pin_C), Pin_D(Pin_D), + stepsPerRevolution(stepsPerRevolution), delayMs(delayMs), currentStep(0) {} + +void Motor::initialize() { + gpio_init(Pin_A); + gpio_init(Pin_B); + gpio_init(Pin_C); + gpio_init(Pin_D); + + gpio_set_dir(Pin_A, GPIO_OUT); + gpio_set_dir(Pin_B, GPIO_OUT); + gpio_set_dir(Pin_C, GPIO_OUT); + gpio_set_dir(Pin_D, GPIO_OUT); +} + +void Motor::motorStep(uint8_t stepIndex) { + gpio_put(Pin_A, stepSequence[stepIndex][0]); + gpio_put(Pin_B, stepSequence[stepIndex][1]); + gpio_put(Pin_C, stepSequence[stepIndex][2]); + gpio_put(Pin_D, stepSequence[stepIndex][3]); +} + +uint8_t Motor::adjustStep(uint8_t currentStep, bool reverse) { + if (reverse) { + return (currentStep == 0) ? 7 : currentStep - 1; + } + return (currentStep + 1) % 8; +} + +void Motor::turnSteps(int steps, bool reverse) { + for (int i = 0; i < abs(steps); i++) { + currentStep = adjustStep(currentStep, reverse); + motorStep(currentStep); + sleep_ms(delayMs); + } +} \ No newline at end of file diff --git a/pico/src/main.cpp b/pico/src/main.cpp index e19b31e7eccea97d18bed89dc9276ada8b9fb49a..2038373ba7aa282355f255cedee8fc75d384a620 100644 --- a/pico/src/main.cpp +++ b/pico/src/main.cpp @@ -1,5 +1,7 @@ #include "PicoUart.hpp" #include "gps.hpp" +#include "compass.hpp" +#include "stepper-motor.hpp" #include "pico/stdio.h" #include "pico/stdlib.h" #include <iostream> @@ -12,7 +14,12 @@ int main() { sleep_ms(1000); DEBUG("Booted"); - Compass compass; + Motor motor_one(6, 7, 8, 9); + motor_one.initialize(); + for (int i = 0; i < 100; ++i) { + motor_one.turnSteps(1024); + } + /* Compass compass; compass.init(); float heading; @@ -45,6 +52,7 @@ int main() { gps->locate_position(300); } } +*/ return 0; }