2012-11-26 15:22:39 +01:00
|
|
|
/*
|
|
|
|
|
Motion.cpp - Motion library
|
|
|
|
|
Copyright (c) 2012 Copenhagen Institute of Interaction Design.
|
|
|
|
|
All right reserved.
|
|
|
|
|
|
|
|
|
|
This library is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU Lesser Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser Public License
|
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
+ author: dviid
|
|
|
|
|
+ contact: dviid@labs.ciid.dk
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Motion.h"
|
|
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#define PB0 PORTB0
|
|
|
|
|
|
|
|
|
|
bool motion_reg_init = false;
|
|
|
|
|
|
|
|
|
|
Motion MotionA(MOTIONA);
|
2013-01-24 21:14:05 +01:00
|
|
|
Motion MotionB(MOTIONB);
|
2012-11-26 15:22:39 +01:00
|
|
|
|
|
|
|
|
uint8_t lb;
|
|
|
|
|
uint8_t hb;
|
2013-01-24 12:11:48 +01:00
|
|
|
|
2012-12-06 16:15:01 +01:00
|
|
|
float T = N * 0.004f; // 4ms (see TCNT1)
|
2013-01-24 12:11:48 +01:00
|
|
|
int xin, dx;
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2012-12-06 16:15:01 +01:00
|
|
|
float MAX_POS = 1023;
|
|
|
|
|
float MAX_VEL = MAX_POS / T;
|
|
|
|
|
float MAX_ACC = MAX_VEL / T;
|
|
|
|
|
|
2012-11-26 15:22:39 +01:00
|
|
|
|
|
|
|
|
Motion::Motion(MOTION m){
|
|
|
|
|
_m = m;
|
|
|
|
|
_i = false;
|
2012-12-06 16:15:01 +01:00
|
|
|
_fcb = NULL;
|
2012-11-26 15:22:39 +01:00
|
|
|
};
|
|
|
|
|
|
2013-01-29 12:41:15 +01:00
|
|
|
void Motion::init(SENSOR_INPUT sensor)
|
2012-11-26 15:22:39 +01:00
|
|
|
{
|
|
|
|
|
if(!motion_reg_init){
|
2013-01-24 21:14:05 +01:00
|
|
|
//TCNT1 = 500; //4 ms (TCNT1)
|
2012-11-26 15:22:39 +01:00
|
|
|
TIMSK1 = (1 << TOIE1);
|
|
|
|
|
motion_reg_init = true;
|
|
|
|
|
}
|
|
|
|
|
_i = true;
|
|
|
|
|
_s = sensor;
|
2013-01-24 12:11:48 +01:00
|
|
|
|
|
|
|
|
// initial values
|
|
|
|
|
d = 0;
|
|
|
|
|
k = 1;
|
|
|
|
|
m = 1;
|
2012-11-26 15:22:39 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-29 12:41:15 +01:00
|
|
|
/*
|
2012-12-06 16:15:01 +01:00
|
|
|
void Motion::set_force_callback(force_callback fcb, PHY physics) {
|
|
|
|
|
_fcb = fcb;
|
|
|
|
|
_fcb_phy = physics;
|
|
|
|
|
}
|
2013-01-29 12:41:15 +01:00
|
|
|
*/
|
2012-12-06 16:15:01 +01:00
|
|
|
|
2013-01-29 12:41:15 +01:00
|
|
|
/*
|
2013-01-24 21:14:05 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2013-01-29 12:41:15 +01:00
|
|
|
*/
|
2013-01-24 21:14:05 +01:00
|
|
|
|
|
|
|
|
int Motion::getX()
|
|
|
|
|
{
|
|
|
|
|
return Xin - 512;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-26 15:22:39 +01:00
|
|
|
// clocked at 4ms period
|
|
|
|
|
ISR(TIMER1_OVF_vect) {
|
2013-01-24 21:14:05 +01:00
|
|
|
//TCNT1 = 500;
|
2012-11-26 15:22:39 +01:00
|
|
|
|
|
|
|
|
if(MotionA._i) {
|
2013-01-24 21:14:05 +01:00
|
|
|
ADMUX = MotionA._s & 0x07; //setting the lower four bits of ADMUX to INPUT bits and upper four bits to 0000.
|
2012-11-26 15:22:39 +01:00
|
|
|
ADCSRA |= (1 << ADSC);
|
|
|
|
|
while (ADCSRA & (1 << ADSC));
|
|
|
|
|
lb = ADCL;
|
|
|
|
|
hb = ADCH;
|
|
|
|
|
|
2013-01-24 12:11:48 +01:00
|
|
|
xin = (hb << 8) | lb;
|
|
|
|
|
MotionA.Xin = xin;
|
2013-01-24 21:14:05 +01:00
|
|
|
|
2012-11-26 15:22:39 +01:00
|
|
|
}
|
2013-01-24 21:14:05 +01:00
|
|
|
|
|
|
|
|
}
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2013-01-24 21:14:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-24 12:11:48 +01:00
|
|
|
/*
|
2012-11-26 15:22:39 +01:00
|
|
|
if(MotionB._i) {
|
|
|
|
|
ADMUX = MotionB._s & 0x07;
|
|
|
|
|
ADCSRA |= (1 << ADSC);
|
|
|
|
|
while (ADCSRA & (1 << ADSC));
|
|
|
|
|
lb = ADCL;
|
|
|
|
|
hb = ADCH;
|
|
|
|
|
x = (hb << 8) | lb;
|
2013-01-24 21:14:05 +01:00
|
|
|
|
2012-11-26 15:22:39 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
2013-01-24 12:11:48 +01:00
|
|
|
*/
|
2012-11-26 15:22:39 +01:00
|
|
|
|