M-M-M/software/lib/MMM/Motion.cpp

69 lines
1.6 KiB
C++
Raw Normal View History

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);
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;
_fcb = NULL;
2013-01-30 18:58:19 +01:00
}
2012-11-26 15:22:39 +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-30 18:58:19 +01:00
tick = millis();
}
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;
}