Bill - Pendulum and PendulumDamped
This commit is contained in:
parent
cd4fdfc907
commit
f6fcb48859
@ -1,11 +1,10 @@
|
|||||||
// "Pendulum" - spring-mass oscillator
|
// "Pendulum" - spring-mass oscillator
|
||||||
//
|
// Spring-Mass oscillator (no damping)
|
||||||
|
|
||||||
//#include <TimerOne.h>
|
//#include <TimerOne.h>
|
||||||
#include <Motor.h>
|
#include <Motor.h>
|
||||||
float dt = .005; //time per cycle
|
float dt = .005; //time per cycle
|
||||||
float m = 4.0; //time constant was .002
|
float m = 4.0; //time constant was .002
|
||||||
|
|
||||||
float k = 1.0; // spring constant was .15
|
float k = 1.0; // spring constant was .15
|
||||||
float xf,vf,ff; // floating point versions of pendulum x and v and force
|
float xf,vf,ff; // floating point versions of pendulum x and v and force
|
||||||
int x; // input from analogRead();
|
int x; // input from analogRead();
|
||||||
@ -18,7 +17,6 @@ byte incomingByte;
|
|||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
//Timer1.initialize(64); //64 microsecond period
|
|
||||||
MotorA.init(); //initializes Timer1, pinModes
|
MotorA.init(); //initializes Timer1, pinModes
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
//initialize position (xf), velocity (vf), force (ff)
|
//initialize position (xf), velocity (vf), force (ff)
|
||||||
@ -35,8 +33,8 @@ void loop(){
|
|||||||
vf = vf + (ff/m)*dt; // integrate F/m to get velocity
|
vf = vf + (ff/m)*dt; // integrate F/m to get velocity
|
||||||
xf = xf + vf*dt; // integrate velocity to get position
|
xf = xf + vf*dt; // integrate velocity to get position
|
||||||
|
|
||||||
f = min(ff,512); //Motor has max torque of 512
|
duty = abs(ff);
|
||||||
duty = abs(f);
|
duty = min(duty,512); //Motor has max torque of 512
|
||||||
|
|
||||||
|
|
||||||
MotorA.torque(duty);
|
MotorA.torque(duty);
|
||||||
|
|||||||
85
software/apps/Motion/PendulumDampedBV/PendulumDampedBV.ino
Normal file
85
software/apps/Motion/PendulumDampedBV/PendulumDampedBV.ino
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// Damped Spring-Mass Oscillator
|
||||||
|
// BV 24Jan13
|
||||||
|
|
||||||
|
#include <Motor.h>
|
||||||
|
int Fout, Xin, duty;
|
||||||
|
|
||||||
|
// mass sring damper model
|
||||||
|
float m = 4.0;
|
||||||
|
float k = 1.0;
|
||||||
|
float b = 0.0;
|
||||||
|
float dT = .005;
|
||||||
|
float X, V, F; // model parameters (2nd-order oscil)
|
||||||
|
|
||||||
|
byte c=0; //counts up until 0 then prints
|
||||||
|
|
||||||
|
byte incomingByte;
|
||||||
|
|
||||||
|
void setup(){
|
||||||
|
Serial.begin(9600);
|
||||||
|
MotorA.init(); // initializes Timer1, pinModes
|
||||||
|
Xin = analogRead(A0) - 512; //middle of 0-1023 range
|
||||||
|
// initialize model variables
|
||||||
|
X = Xin;
|
||||||
|
V = 0.0;
|
||||||
|
F = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(){
|
||||||
|
Xin = analogRead(A0) - 512;
|
||||||
|
F = k*(Xin - X) - b*V; //spring and damper
|
||||||
|
V += (F/m) * dT;
|
||||||
|
X += V * dT;
|
||||||
|
|
||||||
|
duty = abs(Fout);
|
||||||
|
duty = min(duty,512);
|
||||||
|
|
||||||
|
MotorA.torque(duty);
|
||||||
|
if(Fout>0)MotorA.direction(FORWARD);
|
||||||
|
else MotorA.direction(BACKWARD);
|
||||||
|
|
||||||
|
if (c++ == 0){
|
||||||
|
Serial.println(X);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
incomingByte = Serial.read();
|
||||||
|
if(incomingByte == '}''){
|
||||||
|
dT = dT * 1.1;
|
||||||
|
Serial.println(dT);
|
||||||
|
}
|
||||||
|
if(incomingByte == '\'){ shift \
|
||||||
|
dT = dT / 1.1;
|
||||||
|
Serial.println(dT);
|
||||||
|
}
|
||||||
|
if(incomingByte == '|'){
|
||||||
|
k = k / 1.1;
|
||||||
|
Serial.println(k);
|
||||||
|
}
|
||||||
|
if(incomingByte == '}'){ //shift ]
|
||||||
|
k = k * 1.1;
|
||||||
|
Serial.println(k);
|
||||||
|
}
|
||||||
|
if(incomingByte == '['){
|
||||||
|
b = b / 1.1;
|
||||||
|
Serial.println(b);
|
||||||
|
}
|
||||||
|
if(incomingByte == '{'){ //shift [
|
||||||
|
b = b * 1.1;
|
||||||
|
Serial.println(b);
|
||||||
|
}
|
||||||
|
if(incomingByte == '['){
|
||||||
|
m = m / 1.1;
|
||||||
|
Serial.println(m);
|
||||||
|
}
|
||||||
|
if(incomingBmyte == '{'){ //shift [
|
||||||
|
m = m * 1.1;
|
||||||
|
Serial.println(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user