Hello!
I have an installation in which it will draw a spirograph pattern. The motors are set up so motor 1 controls a shaft's yaw rotation, and the other controls the print heads distance down the shaft.
I have written code to generate a spirograph, and it works
See here: Scratch Project Demo for a graphical demo.
I have also written the arduino code, here:
uint16_t ar=30;//Circle A radius
uint16_t br=90;//Circle B radius
uint16_t hr=37;//Hole's distance from B's center
double ac;//Circle a's circumference
double bc;//Circle b's circumference
const double tau=2*PI;
bool reversed=0;
uint8_t resolution=100;
double aTheta=0;
double bTheta=0;
uint16_t maxDist;
uint16_t minDist;
void resetParams(){
ac=ar*tau;
bc=br*tau;
maxDist=ar+hr;
minDist=hr-ar;
aTheta=0;
bTheta=0;
}
void loopSpiral(){
double x=ar*cos(aTheta)+hr*cos(bTheta);
double y=ar*sin(aTheta)+hr*sin(bTheta);
double theta=atan2(y,x);
double r=sqrt(sq(x)+sq(y));
gotoPos(map(theta,0,tau,0,800),r);//map(r,minDist,maxDist,0,1600));
bTheta+=(reversed)?-tau/resolution:tau/resolution;
aTheta=bTheta*(ac/bc);
}
(not all code shown here,
resetParams called in setup,
loopSpiral called in loop,
gotoPos just moves the motors, one theta axis and the other radius axis)
But, the motors move erratically.
One, the theta axis, makes a sweeping motion, from -200 to 200 steps repeatedly
The other, the radial axis, makes little movements back and forth.
I'm thinking that the code that is broken is the cartesian -> polar conversion, but I'm not sure. I know the spirograph code works, as it's copied directly into scratch (do remember that arduino is in rads, and scratch in deg)
Any help as to what's going on here?