Part 1 should work, unable to test it. position() always gives 0 even when i turn so speed has the same problem
This commit is contained in:
@@ -13,4 +13,3 @@ int main()
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
""
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
.pio
|
||||||
|
.vscode/.browse.c_cpp.db*
|
||||||
|
.vscode/c_cpp_properties.json
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/ipch
|
||||||
@@ -12,6 +12,7 @@ public:
|
|||||||
void init();
|
void init();
|
||||||
void update();
|
void update();
|
||||||
int position();
|
int position();
|
||||||
|
int speed();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Digital_in c1;
|
Digital_in c1;
|
||||||
@@ -20,6 +21,9 @@ private:
|
|||||||
|
|
||||||
bool previousC1;
|
bool previousC1;
|
||||||
volatile int count;
|
volatile int count;
|
||||||
|
int pastCount;
|
||||||
|
uint32_t pastTime;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <encoder.h>
|
#include <encoder.h>
|
||||||
|
#include <Arduino.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
@@ -42,6 +43,11 @@ void Encoder::init()
|
|||||||
|
|
||||||
// Enable global interrupts
|
// Enable global interrupts
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
|
// Set initial states for speed
|
||||||
|
pastCount = count;
|
||||||
|
pastTime = micros();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Encoder::update()
|
void Encoder::update()
|
||||||
@@ -79,3 +85,20 @@ int Encoder::position()
|
|||||||
|
|
||||||
return currentCount;
|
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;
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ int main()
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
Serial.println(encoder.position());
|
Serial.println(encoder.position());
|
||||||
|
//Serial.println(encoder.speed());
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user