Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afd4b230b2 |
@ -1,4 +1,5 @@
|
|||||||
#include <Motion.h>
|
#include <Motion.h>
|
||||||
|
#include <Motor.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
@ -41,15 +42,30 @@ float getAcceleration();
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
MotionA.init(INPUTA0);
|
MotionA.init(INPUTA0);
|
||||||
|
MotorA.init();
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
MotionA.k = 0.2f;
|
||||||
|
MotionA.m = 0.3f;
|
||||||
|
MotionA.d = 0.02f;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
if(MotionA.F < 0) MotorA.direction(FORWARD);
|
||||||
|
else MotorA.direction(BACKWARD);
|
||||||
|
|
||||||
|
float t = abs(MotionA.F);
|
||||||
|
MotorA.torque(t);
|
||||||
|
|
||||||
|
/*
|
||||||
Serial.print("position: "); Serial.println(MotionA.getPosition());
|
Serial.print("position: "); Serial.println(MotionA.getPosition());
|
||||||
Serial.print("velocity: "); Serial.println(MotionA.getVelocity());
|
Serial.print("velocity: "); Serial.println(MotionA.getVelocity());
|
||||||
Serial.print("accel: "); Serial.println(MotionA.getAcceleration());
|
Serial.print("accel: "); Serial.println(MotionA.getAcceleration());
|
||||||
Serial.println("-------");
|
Serial.println("-------");
|
||||||
delay(100);
|
delay(100);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
63
software/apps/Motion/Pendulum/Pendulum.ino
Normal file
63
software/apps/Motion/Pendulum/Pendulum.ino
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// "Pendulum" - spring-mass oscillator
|
||||||
|
#include <Motion.h>
|
||||||
|
#include <Motor.h>
|
||||||
|
|
||||||
|
byte incomingByte;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
// init MotionA & MotorA
|
||||||
|
MotionA.init(INPUTA0);
|
||||||
|
MotorA.init();
|
||||||
|
|
||||||
|
// provide MotionA with initial physics constants
|
||||||
|
MotionA.k = 0.2f; // spring
|
||||||
|
MotionA.m = 0.3f; // mass
|
||||||
|
MotionA.d = 0.02f; // damping
|
||||||
|
|
||||||
|
Serial.begin(9600);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(){
|
||||||
|
|
||||||
|
if(MotionA.F < 0) MotorA.direction(FORWARD);
|
||||||
|
else MotorA.direction(BACKWARD);
|
||||||
|
|
||||||
|
float t = abs(MotionA.F);
|
||||||
|
//if(t > 512) t = 512;
|
||||||
|
//MotorA.torque(t);
|
||||||
|
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
incomingByte = Serial.read();
|
||||||
|
if(incomingByte == '1'){
|
||||||
|
MotionA.m = MotionA.m * 1.1;
|
||||||
|
Serial.print("m: ");
|
||||||
|
Serial.println(MotionA.m);
|
||||||
|
}
|
||||||
|
else if(incomingByte == '!'){
|
||||||
|
MotionA.m = MotionA.m / 1.1;
|
||||||
|
Serial.print("m: ");
|
||||||
|
Serial.println(MotionA.m);
|
||||||
|
}
|
||||||
|
else if(incomingByte == '2'){
|
||||||
|
MotionA.k = MotionA.k * 1.1;
|
||||||
|
Serial.print("k: ");
|
||||||
|
Serial.println(MotionA.k);
|
||||||
|
}
|
||||||
|
else if(incomingByte == '@'){
|
||||||
|
MotionA.k = MotionA.k / 1.1;
|
||||||
|
Serial.print("k: ");
|
||||||
|
Serial.println(MotionA.k);
|
||||||
|
}
|
||||||
|
else if(incomingByte == '3'){
|
||||||
|
MotionA.d = MotionA.d * 1.1;
|
||||||
|
Serial.print("d: ");
|
||||||
|
Serial.println(MotionA.d);
|
||||||
|
}
|
||||||
|
else if(incomingByte == '#'){
|
||||||
|
MotionA.d = MotionA.d / 1.1;
|
||||||
|
Serial.print("d: ");
|
||||||
|
Serial.println(MotionA.d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,80 +0,0 @@
|
|||||||
// "Pendulum" - spring-mass oscillator
|
|
||||||
//
|
|
||||||
|
|
||||||
#include <TimerOne.h>
|
|
||||||
#include <Motor.h>
|
|
||||||
float tf = 0.002; //time constant was .002
|
|
||||||
float kf = 0.2; // spring constant was .15
|
|
||||||
float xf,vf,ff; // floating point versions of pendulum x and v and force
|
|
||||||
int x, f; //int versions of position (from a/d) and force (to Pwm)
|
|
||||||
int duty; // abs(f)
|
|
||||||
byte c=0; //counts up until 0 then prints
|
|
||||||
|
|
||||||
float m = 0.3;
|
|
||||||
float k = 0.01;
|
|
||||||
|
|
||||||
float damp = 0.09;
|
|
||||||
|
|
||||||
byte incomingByte;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
//Timer1.initialize(64); //64 microsecond period
|
|
||||||
MotorA.torque(100); //initializes Timer1, pinModes
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop(){
|
|
||||||
x = analogRead(0); // position [0-1024]
|
|
||||||
ff = kf*(x - xf); // spring
|
|
||||||
f = max(ff,-1024);
|
|
||||||
f = min(f,1024);
|
|
||||||
// if(f>0)digitalWrite(DIRA,HIGH);
|
|
||||||
// else digitalWrite(DIRA,LOW);
|
|
||||||
duty = abs(f);
|
|
||||||
// Timer1.pwm(9,duty);
|
|
||||||
MotorA.torque(duty);
|
|
||||||
if(f>0)MotorA.direction(FORWARD);
|
|
||||||
else MotorA.direction(BACKWARD);
|
|
||||||
|
|
||||||
// update (integrate) floating versions of xf and vf
|
|
||||||
//integrate twice tf is deltaT/mass;
|
|
||||||
vf += ff*tf;
|
|
||||||
xf += vf*tf;
|
|
||||||
|
|
||||||
if(c++==0) // when c gets to 255 it next == 0 and send data
|
|
||||||
{
|
|
||||||
Serial.print(x);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.print(xf);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.println(ff);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c++ == 0)Serial.println(x); //
|
|
||||||
if (Serial.available() > 0) {
|
|
||||||
//Serial.print(v);
|
|
||||||
//Serial.print(" ");
|
|
||||||
//Serial.println(x);
|
|
||||||
incomingByte = Serial.read();
|
|
||||||
//Serial.write(incomingByte);
|
|
||||||
if(incomingByte == '\''){
|
|
||||||
m = m * 1.1;
|
|
||||||
Serial.println(m);
|
|
||||||
}
|
|
||||||
if(incomingByte == ';'){
|
|
||||||
m = m / 1.1;
|
|
||||||
Serial.println(m);
|
|
||||||
}
|
|
||||||
if(incomingByte == '.'){
|
|
||||||
k = k / 1.1;
|
|
||||||
Serial.println(k);
|
|
||||||
}
|
|
||||||
if(incomingByte == '/'){
|
|
||||||
k = k * 1.1;
|
|
||||||
Serial.println(k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,27 +1,16 @@
|
|||||||
//#include <TimerOne.h>
|
|
||||||
|
|
||||||
#include <Motor.h>
|
#include <Motor.h>
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
MotorA.init();
|
||||||
MotorA.torque(500);
|
MotorA.torque(255);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
static int j = 1;
|
|
||||||
static long cnt = 0;
|
|
||||||
|
|
||||||
if(cnt == 50000) {
|
|
||||||
j = -1;
|
|
||||||
MotorA.direction(FORWARD);
|
MotorA.direction(FORWARD);
|
||||||
} else if(cnt == 0) {
|
delay(1000);
|
||||||
j = 1;
|
|
||||||
MotorA.direction(BACKWARD);
|
MotorA.direction(BACKWARD);
|
||||||
}
|
delay(1000);
|
||||||
cnt += j;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,9 +36,9 @@ Motion MotionB(MOTIONA);
|
|||||||
|
|
||||||
uint8_t lb;
|
uint8_t lb;
|
||||||
uint8_t hb;
|
uint8_t hb;
|
||||||
int x, xx;
|
|
||||||
float T = N * 0.004f; // 4ms (see TCNT1)
|
float T = N * 0.004f; // 4ms (see TCNT1)
|
||||||
float v;
|
int xin, dx;
|
||||||
|
|
||||||
float MAX_POS = 1023;
|
float MAX_POS = 1023;
|
||||||
float MAX_VEL = MAX_POS / T;
|
float MAX_VEL = MAX_POS / T;
|
||||||
@ -60,18 +60,11 @@ void Motion::init(INPUT sensor)
|
|||||||
}
|
}
|
||||||
_i = true;
|
_i = true;
|
||||||
_s = sensor;
|
_s = sensor;
|
||||||
}
|
|
||||||
|
|
||||||
int Motion::getPosition() {
|
// initial values
|
||||||
return _x;
|
d = 0;
|
||||||
}
|
k = 1;
|
||||||
|
m = 1;
|
||||||
float Motion::getVelocity() {
|
|
||||||
return _v;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Motion::getAcceleration() {
|
|
||||||
return _a;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Motion::set_force_callback(force_callback fcb, PHY physics) {
|
void Motion::set_force_callback(force_callback fcb, PHY physics) {
|
||||||
@ -84,32 +77,23 @@ void Motion::set_force_callback(force_callback fcb, PHY physics) {
|
|||||||
ISR(TIMER1_OVF_vect) {
|
ISR(TIMER1_OVF_vect) {
|
||||||
TCNT1 = 1000;
|
TCNT1 = 1000;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(MotionA._i) {
|
if(MotionA._i) {
|
||||||
ADMUX = MotionA._s & 0x07;
|
ADMUX = MotionA._s & 0x07;
|
||||||
ADCSRA |= (1 << ADSC);
|
ADCSRA |= (1 << ADSC);
|
||||||
while (ADCSRA & (1 << ADSC));
|
while (ADCSRA & (1 << ADSC));
|
||||||
lb = ADCL;
|
lb = ADCL;
|
||||||
hb = ADCH;
|
hb = ADCH;
|
||||||
x = (hb << 8) | lb;
|
|
||||||
MotionA._xv[MotionA._ix] = x;
|
|
||||||
MotionA._ix++;
|
|
||||||
MotionA._ix %= N;
|
|
||||||
|
|
||||||
xx = x - MotionA._x;
|
xin = (hb << 8) | lb;
|
||||||
if(abs(xx) < 2) {
|
|
||||||
v = 0.0f;
|
|
||||||
} else {
|
|
||||||
v = xx / T;
|
|
||||||
}
|
|
||||||
|
|
||||||
MotionA._a = (v - MotionA._v) / T;
|
MotionA.F = MotionA.k * (xin - MotionA.X) - (MotionA.d * MotionA.V);
|
||||||
MotionA._v = v;
|
MotionA.A = MotionA.F / MotionA.m;
|
||||||
MotionA._x = x;
|
MotionA.V += MotionA.A * T;
|
||||||
|
MotionA.X += MotionA.V * T;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
if(MotionB._i) {
|
if(MotionB._i) {
|
||||||
ADMUX = MotionB._s & 0x07;
|
ADMUX = MotionB._s & 0x07;
|
||||||
ADCSRA |= (1 << ADSC);
|
ADCSRA |= (1 << ADSC);
|
||||||
@ -134,6 +118,7 @@ ISR(TIMER1_OVF_vect) {
|
|||||||
MotionB._x = x;
|
MotionB._x = x;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -57,10 +57,6 @@ public:
|
|||||||
|
|
||||||
void init(INPUT sensor);
|
void init(INPUT sensor);
|
||||||
|
|
||||||
int getPosition();
|
|
||||||
float getVelocity();
|
|
||||||
float getAcceleration();
|
|
||||||
|
|
||||||
void set_force_callback(force_callback fcb, PHY physics);
|
void set_force_callback(force_callback fcb, PHY physics);
|
||||||
|
|
||||||
|
|
||||||
@ -68,9 +64,8 @@ public:
|
|||||||
int _xv[N];
|
int _xv[N];
|
||||||
int _ix;
|
int _ix;
|
||||||
|
|
||||||
int _x;
|
float X, V, A, F;
|
||||||
float _v;
|
float m, k, d;
|
||||||
float _a;
|
|
||||||
|
|
||||||
MOTION _m;
|
MOTION _m;
|
||||||
INPUT _s;
|
INPUT _s;
|
||||||
|
|||||||
@ -48,16 +48,17 @@ void MMotor::init()
|
|||||||
|
|
||||||
//direction pins are outputs
|
//direction pins are outputs
|
||||||
DDRD |= (1 << PD7);
|
DDRD |= (1 << PD7);
|
||||||
DDRB |= (1 << PB0);
|
DDRB = (1 << PB0) | (1 << PB1) | (1 << PB2);
|
||||||
|
|
||||||
DDRB |= (1 << PB1) | (1 << PB2);
|
|
||||||
TCCR1A = (1 << COM1A1) | (1 << COM1B1);
|
TCCR1A = (1 << COM1A1) | (1 << COM1B1);
|
||||||
|
|
||||||
// clear the bits
|
// clear the bits
|
||||||
TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12));
|
TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12));
|
||||||
TCCR1B = (1 << WGM13) | (1 << CS10);
|
TCCR1B = (1 << WGM13) | (1 << CS10);
|
||||||
|
|
||||||
|
//ICR1 = 512;
|
||||||
ICR1 = 512;
|
ICR1 = 512;
|
||||||
|
|
||||||
reg_init = true;
|
reg_init = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user