Fixed Motion
This commit is contained in:
parent
dbb6a746be
commit
f0556802f5
@ -23,121 +23,46 @@
|
|||||||
|
|
||||||
#include "Motion.h"
|
#include "Motion.h"
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define PB0 PORTB0
|
//bool motion_reg_init = false;
|
||||||
|
|
||||||
bool motion_reg_init = false;
|
|
||||||
|
|
||||||
Motion MotionA(MOTIONA);
|
Motion MotionA(MOTIONA);
|
||||||
Motion MotionB(MOTIONB);
|
Motion MotionB(MOTIONB);
|
||||||
|
|
||||||
uint8_t lb;
|
int MAX_POS = 1023;
|
||||||
uint8_t hb;
|
|
||||||
|
|
||||||
float T = N * 0.004f; // 4ms (see TCNT1)
|
|
||||||
int xin, dx;
|
|
||||||
|
|
||||||
float MAX_POS = 1023;
|
|
||||||
float MAX_VEL = MAX_POS / T;
|
|
||||||
float MAX_ACC = MAX_VEL / T;
|
|
||||||
|
|
||||||
|
|
||||||
Motion::Motion(MOTION m){
|
Motion::Motion(MOTION m){
|
||||||
_m = m;
|
_m = m;
|
||||||
_i = false;
|
_i = false;
|
||||||
_fcb = NULL;
|
_fcb = NULL;
|
||||||
};
|
}
|
||||||
|
|
||||||
void Motion::init(SENSOR_INPUT sensor)
|
void Motion::init(SENSOR_INPUT sensor)
|
||||||
{
|
{
|
||||||
if(!motion_reg_init){
|
|
||||||
//TCNT1 = 500; //4 ms (TCNT1)
|
|
||||||
TIMSK1 = (1 << TOIE1);
|
|
||||||
motion_reg_init = true;
|
|
||||||
}
|
|
||||||
_i = true;
|
_i = true;
|
||||||
_s = sensor;
|
_s = sensor;
|
||||||
|
|
||||||
// initial values
|
tick = millis();
|
||||||
d = 0;
|
|
||||||
k = 1;
|
|
||||||
m = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
void Motion::update_mass_spring_damper() {
|
||||||
void Motion::set_force_callback(force_callback fcb, PHY physics) {
|
|
||||||
_fcb = fcb;
|
// todo: filtering
|
||||||
_fcb_phy = physics;
|
|
||||||
|
long t = millis();
|
||||||
|
float dt = (float)(t - tick) / 100.0f;
|
||||||
|
|
||||||
|
int xin = analogRead(_s); // may take some time
|
||||||
|
|
||||||
|
Xin = xin;
|
||||||
|
F = k * (xin - X) - (d * V);
|
||||||
|
V += (F / m) * dt;
|
||||||
|
X += V * dt;
|
||||||
|
|
||||||
|
tick = t;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
float Motion::calculateFAVX(int x_in)
|
|
||||||
{
|
|
||||||
F = k * (x_in - X) - (d * V);
|
|
||||||
//MotionA.A = MotionA.F / MotionA.m;
|
|
||||||
V += (F / m) * T;
|
|
||||||
X += V * T;
|
|
||||||
return F;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
int Motion::getX()
|
|
||||||
{
|
|
||||||
return Xin - 512;
|
|
||||||
}
|
|
||||||
|
|
||||||
// clocked at 4ms period
|
|
||||||
ISR(TIMER1_OVF_vect) {
|
|
||||||
//TCNT1 = 500;
|
|
||||||
|
|
||||||
if(MotionA._i) {
|
|
||||||
ADMUX = MotionA._s & 0x07; //setting the lower four bits of ADMUX to INPUT bits and upper four bits to 0000.
|
|
||||||
ADCSRA |= (1 << ADSC);
|
|
||||||
while (ADCSRA & (1 << ADSC));
|
|
||||||
lb = ADCL;
|
|
||||||
hb = ADCH;
|
|
||||||
|
|
||||||
xin = (hb << 8) | lb;
|
|
||||||
MotionA.Xin = xin;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
if(MotionB._i) {
|
|
||||||
ADMUX = MotionB._s & 0x07;
|
|
||||||
ADCSRA |= (1 << ADSC);
|
|
||||||
while (ADCSRA & (1 << ADSC));
|
|
||||||
lb = ADCL;
|
|
||||||
hb = ADCH;
|
|
||||||
x = (hb << 8) | lb;
|
|
||||||
|
|
||||||
MotionB._xv[MotionB._ix] = x;
|
|
||||||
MotionB._ix++;
|
|
||||||
MotionB._ix %= N;
|
|
||||||
|
|
||||||
|
|
||||||
xx = x - MotionB._x;
|
|
||||||
if(abs(xx) < 2) {
|
|
||||||
v = 0;
|
|
||||||
} else {
|
|
||||||
v = xx / T;
|
|
||||||
}
|
|
||||||
|
|
||||||
MotionB._a = (v - MotionB._v) / T;
|
|
||||||
MotionB._v = v;
|
|
||||||
MotionB._x = x;
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|||||||
@ -53,8 +53,6 @@ public:
|
|||||||
|
|
||||||
void update_mass_spring_damper();
|
void update_mass_spring_damper();
|
||||||
|
|
||||||
int getX();
|
|
||||||
|
|
||||||
// raw position vector
|
// raw position vector
|
||||||
int _xv[N];
|
int _xv[N];
|
||||||
int _ix;
|
int _ix;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user