From c0073a582042607f3031d14b130c636c8bf9f2b9 Mon Sep 17 00:00:00 2001 From: nokkvinetti <82093782+nokkvinetti@users.noreply.github.com> Date: Thu, 10 Sep 2026 15:21:13 +0000 Subject: [PATCH] Part 1 should work, unable to test it. position() always gives 0 even when i turn so speed has the same problem --- .../Part_1_Polling/src/main.cpp | 1 - .../Part_1_Motor_Speed/.gitignore | 5 ++++ .../Part_1_Motor_Speed/include/encoder.h | 4 ++++ .../Part_1_Motor_Speed/src/encoder.cpp | 23 +++++++++++++++++++ .../Part_1_Motor_Speed/src/main.cpp | 2 ++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Project_2_Speed_Controller/Part_1_Motor_Speed/.gitignore diff --git a/Project_1_Rotary_Encoder/Part_1_Polling/src/main.cpp b/Project_1_Rotary_Encoder/Part_1_Polling/src/main.cpp index 474fd01..93c7295 100644 --- a/Project_1_Rotary_Encoder/Part_1_Polling/src/main.cpp +++ b/Project_1_Rotary_Encoder/Part_1_Polling/src/main.cpp @@ -13,4 +13,3 @@ int main() return 0; } -"" \ No newline at end of file diff --git a/Project_2_Speed_Controller/Part_1_Motor_Speed/.gitignore b/Project_2_Speed_Controller/Part_1_Motor_Speed/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Project_2_Speed_Controller/Part_1_Motor_Speed/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Project_2_Speed_Controller/Part_1_Motor_Speed/include/encoder.h b/Project_2_Speed_Controller/Part_1_Motor_Speed/include/encoder.h index 272d7ce..ea9307a 100644 --- a/Project_2_Speed_Controller/Part_1_Motor_Speed/include/encoder.h +++ b/Project_2_Speed_Controller/Part_1_Motor_Speed/include/encoder.h @@ -12,6 +12,7 @@ public: void init(); void update(); int position(); + int speed(); private: Digital_in c1; @@ -20,6 +21,9 @@ private: bool previousC1; volatile int count; + int pastCount; + uint32_t pastTime; + }; #endif \ No newline at end of file diff --git a/Project_2_Speed_Controller/Part_1_Motor_Speed/src/encoder.cpp b/Project_2_Speed_Controller/Part_1_Motor_Speed/src/encoder.cpp index 39f7c5a..373e7d3 100644 --- a/Project_2_Speed_Controller/Part_1_Motor_Speed/src/encoder.cpp +++ b/Project_2_Speed_Controller/Part_1_Motor_Speed/src/encoder.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -42,6 +43,11 @@ void Encoder::init() // Enable global interrupts sei(); + + // Set initial states for speed + pastCount = count; + pastTime = micros(); + } void Encoder::update() @@ -78,4 +84,21 @@ int Encoder::position() SREG = oldSREG; return currentCount; +} + + + +int Encoder::speed() +{ + int currentCount = position(); + uint32_t currentTime = micros(); //gives arduino microseconds since program started running + + //from spec sheet we get 1400 counts per revolution from driver + // or 700 from red beard?? + int rpm = ((currentCount-pastCount)*6e7)/((currentTime-pastTime)*1400); // we turn counts to rotations by dividing by 1400 and microsecond to minutes by multiplying with 60 and 10^6 + + pastTime = currentTime; + pastCount = currentCount; + + return rpm; } \ No newline at end of file diff --git a/Project_2_Speed_Controller/Part_1_Motor_Speed/src/main.cpp b/Project_2_Speed_Controller/Part_1_Motor_Speed/src/main.cpp index 4df60e6..1c72869 100644 --- a/Project_2_Speed_Controller/Part_1_Motor_Speed/src/main.cpp +++ b/Project_2_Speed_Controller/Part_1_Motor_Speed/src/main.cpp @@ -14,6 +14,8 @@ int main() while (1) { Serial.println(encoder.position()); + //Serial.println(encoder.speed()); + delay(100); } return 0;