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"
|
|
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
2012-11-26 15:22:39 +01:00
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
//bool motion_reg_init = false;
|
2012-11-26 15:22:39 +01:00
|
|
|
|
|
|
|
|
Motion MotionA(MOTIONA);
|
2013-01-24 21:14:05 +01:00
|
|
|
Motion MotionB(MOTIONB);
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
int MAX_POS = 1023;
|
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;
|
2013-01-30 18:58:19 +01:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
|
_i = true;
|
2013-01-30 18:58:19 +01:00
|
|
|
_s = sensor;
|
2013-01-24 12:11:48 +01:00
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
tick = millis();
|
2013-01-24 21:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
void Motion::update_mass_spring_damper() {
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
// todo: filtering
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
long t = millis();
|
|
|
|
|
float dt = (float)(t - tick) / 100.0f;
|
|
|
|
|
|
|
|
|
|
int xin = analogRead(_s); // may take some time
|
2012-11-26 15:22:39 +01:00
|
|
|
|
2013-01-30 18:58:19 +01:00
|
|
|
Xin = xin;
|
|
|
|
|
F = k * (xin - X) - (d * V);
|
|
|
|
|
V += (F / m) * dt;
|
|
|
|
|
X += V * dt;
|
|
|
|
|
|
|
|
|
|
tick = t;
|
|
|
|
|
}
|