-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMimicJoint.cpp
44 lines (30 loc) · 1.39 KB
/
MimicJoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <Arduino.h>
#define EI_ARDUINO_INTERRUPTED_PIN
#include <EnableInterrupt.h>
#include "MimicJoint.h"
MimicJoint::JointInstances MimicJoint::jointInstances;
void MimicJoint::Setup(int signalPin, double signalMin, int signalToAngleConversion) {
this->SignalPin = signalPin;
this->SignalMin = signalMin;
this->SignalToAngleConversion = signalToAngleConversion;
jointInstances.Append(this->SignalPin, this);
pinMode(this->SignalPin, INPUT);
enableInterrupt(this->SignalPin, Falling, FALLING);
}
int MimicJoint::GetAngle() {
return this->Angle;
}
void MimicJoint::Falling() {
MimicJoint* currentJoint = jointInstances.GetInstance(arduinoInterruptedPin);
unsigned long previousMicrosAtFall = currentJoint->MicrosAtFall;
currentJoint->MicrosAtFall = micros();
unsigned long microsWhileHigh = currentJoint->MicrosAtFall - currentJoint->MicrosAtRise;
unsigned long totalPulseLength = currentJoint->MicrosAtFall - previousMicrosAtFall;
currentJoint->Angle = ((double)microsWhileHigh/totalPulseLength - currentJoint->SignalMin) * currentJoint->SignalToAngleConversion;
enableInterrupt(currentJoint->SignalPin, Rising, RISING);
}
void MimicJoint::Rising() {
MimicJoint* currentJoint = jointInstances.GetInstance(arduinoInterruptedPin);
currentJoint->MicrosAtRise = micros();
enableInterrupt(currentJoint->SignalPin, Falling, FALLING);
}