Arduino Code Question

Two axis stepper control via joystick

Anything that doesn't fit anywhere else below.

Moderators: Calilasseia, ADParker

Re: Arduino Code Question

#21  Postby theropod » Jun 28, 2018 8:18 pm

Ah ha moment #2!

The placement of comments at the end of the IF conditions is brilliant.

I will give this a go later, as I have shut down the Mac and am now recovering from a wonderful meal my wife plulled together from leftovers. Most of the spare blood in my body is now happily gnawing away, and leaving my brain majorly empty (more than usual anyway).

Thanks Cali. I see what you have done there and I think the end is near.

RS
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Ads by Google


Re: Arduino Code Question

#22  Postby Calilasseia » Jun 28, 2018 10:36 pm

Enjoy the food and the following nap! :)
Signature temporarily on hold until I can find a reliable image host ...
User avatar
Calilasseia
RS Donator
 
Posts: 22034
Age: 58
Male

Country: England
United Kingdom (uk)
Print view this post

Re: Arduino Code Question

#23  Postby theropod » Jun 28, 2018 10:51 pm

No nap, but after some rest I think I see a way to insert commands to send those step speed values to the Serial Monitor. I think all I have to do is include a SerialPrint(Step_speed) statement for the X and Y axis. That just might reveal all I need to do in regards to my issues. No matter what I think I have this from here. I owe ya a cold one!!

RS
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Re: Arduino Code Question

#24  Postby Calilasseia » Jun 28, 2018 11:15 pm

theropod wrote:No nap, but after some rest I think I see a way to insert commands to send those step speed values to the Serial Monitor. I think all I have to do is include a SerialPrint(Step_speed) statement for the X and Y axis. That just might reveal all I need to do in regards to my issues. No matter what I think I have this from here. I owe ya a cold one!!

RS


Right now I could do with a cold one ... bedroom's at 85°F again!

I've been having fun with a JavaScript project - designing my own CPU architecture. A 256 bit CPU, no less. Intel, eat your heart out!
Signature temporarily on hold until I can find a reliable image host ...
User avatar
Calilasseia
RS Donator
 
Posts: 22034
Age: 58
Male

Country: England
United Kingdom (uk)
Print view this post

Re: Arduino Code Question

#25  Postby theropod » Jun 29, 2018 12:03 am

Calilasseia wrote:
theropod wrote:No nap, but after some rest I think I see a way to insert commands to send those step speed values to the Serial Monitor. I think all I have to do is include a SerialPrint(Step_speed) statement for the X and Y axis. That just might reveal all I need to do in regards to my issues. No matter what I think I have this from here. I owe ya a cold one!!

RS


Right now I could do with a cold one ... bedroom's at 85°F again!

I've been having fun with a JavaScript project - designing my own CPU architecture. A 256 bit CPU, no less. Intel, eat your heart out!


I feel ya. The auctual air temperature right now, 7:00 pm is 99° F, with a heat index of 112°. Inside our house, with the AC at full blast it is 81°, and our house is very well insulated. Just before dark I have to install a new oxygen sensor on our Escape as we are fleeing to my wife’s parents house tomorrow. They have a basement, and top notch central air, which will offer a few days relief. The task is relatively simple, but any effort expended in this crap is pure hell. Oregon is calling our name, and by this time next year we may no longer live in a slave state. Note the lack of former prefix.
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Re: Arduino Code Question

#26  Postby Calilasseia » Jun 29, 2018 6:53 am

Oregon will be [1] more bearable, [2] safely far away from the slave state loons, and [3] close enough to Canada for a major escape if needed. :)
Signature temporarily on hold until I can find a reliable image host ...
User avatar
Calilasseia
RS Donator
 
Posts: 22034
Age: 58
Male

Country: England
United Kingdom (uk)
Print view this post

Re: Arduino Code Question

#27  Postby theropod » Jan 24, 2019 4:01 am

Proportional stepper motor response from joystick movements achievement unlocked!

After thinking about my little project, help from Cali and major input from the stepper motor driver boards designer I have finally hacked together a proper set of instructions for my little Alt/Az pointer. I did away with the program halting “delay” commands and replaced them with timing calls that are tied to the joystick movements via mapping. This now works like a charm, and has smooth acceleration and deceleration. I also simplified the joystick reads and employed a state machine to control the on/off pulses sent to the Easy Drivers.

The variables for the joystick mapped values would need to be altered for a different gear ratio. The retruned values for each joystick read should be tested with Serial Monitor to determine a null zone.

Here is what I finally produced:

Code: Select all

/*

Written by Theropod for prototype Pan/Tilt Alt/Az small telescope or
binocular set pointer application.

Significant advice, and editorial guidance, supplied by Brian Schmalz
(designer of the Easy Driver® bipolar stepper motor driver board).

Test equipment:
Arduino® Nano, 2 - Easy Driver® bipolar stepper motor driver boards,
analog 2 axis joystick and salvage bipolar stepper motors,
which are both 1.8 degree per full step, being driven at 1/8 microstepping (Easy Driver default),
which equates to 200 full steps multiplied by 8 = 1600 pulses for one complete revolution of the motor
left right gearing provided by windshield wiper worm and wheel gears (ratio TBD)
up and down gearing provided by paper shredder square cut reduction set (ratio TBD)
project origin; July, 2018, this edit, Version 5.1, began Jan. 2019

last modified: Jan. 23, 2019


*/
//define Arduino pin assignments
//
#define step_pinx 2 // Arduino Digital output pin #2 is step signal pin for X axis L/R
#define dir_pinx 3 // Arduino Digital output pin #3 is direction control pin for X axis L/R
#define x_pin A0 // Arduino analog input from joystick for X axis L/R
#define y_pin A1 // Arduino analog input from joystick for Y axis U/D
#define step_piny 5 // Arduino Digital output pin #5 is step speed select signal pin for Y axis U/D
#define dir_piny 4 // Arduino Digital output pin #4 is direction control pin for Y axis U/D
//
//declare global variable
//
int xaxisState = LOW; //state of x axis stepper pulse set to ON
int yaxisState = LOW; //state of y axis stepper pulse set to ON
//
int XStepDelay;
int YStepDelay;
//
//
const long xhigh = 180; //interval to pulse X axis at slowest speed, change to fit application
const long xlow = 1; //interval to pulse X axis at highest speed, change to fit application
//
const long yhigh = 180; //interval to pulse Y axis at slowest speed, change to fit application
const long ylow = 2; //interval to pulse Y axis at highest speed, change to fit application
//
//
unsigned long previousMillisX = 0; //
unsigned long previousMillisY = 0; //
//
//
//
void setup() {
  //
  delay(1000); //1 second delay to allow easy drivers to power up
  //
  pinMode(dir_pinx, OUTPUT);
  pinMode(step_pinx, OUTPUT);
  pinMode(dir_piny, OUTPUT);
  pinMode(step_piny, OUTPUT);
  digitalWrite(step_pinx, xaxisState) ; //used in state machine switching for the stepper logic final drive (on or off setting)
  digitalWrite(step_piny, yaxisState) ; //used in state machine switching for the stepper logic final drive (on or off setting)
  //
}
//
void loop() {
  //
  unsigned long currentMillisX = millis(); //
  unsigned long currentMillisY = millis(); //
  //
  //
  int xValue = analogRead(A0); // x axis variable for joystick inputs controlling r/l PIN DEPENDENT!
  int yValue = analogRead(A1); // y axis variable for joystick inputs controlling u/d PIN DEPENDENT!
  //
  //
  if (xValue > 520) {
    //if joystick moved RIGHT out of "null zone" to limit jitter
    digitalWrite(dir_pinx, HIGH); // motor direction signal sent to Easy Driver X axis DIR pin (ON)
    XStepDelay = map(xValue, 520, 1023, xhigh, xlow);
  }
  else if (xValue < 480) {
    //if joystick moved LEFT out of null zone to limit jitter
    digitalWrite(dir_pinx, LOW); // motor direction signal sent to Easy Driver X axis DIR pin (OFF)
    XStepDelay = map(xValue, 0, 480, xlow, xhigh);
  }
  else if (yValue > 520) {
    // if joystick moved DOWN out of null zone to limit jitter
    digitalWrite(dir_piny, HIGH); //motor direction signal to Y axis Easy Driver DIR pin (ON)
    YStepDelay = map(yValue, 530, 1023, yhigh, ylow); //
  }
  else if (yValue < 480) {
    //if joystick moved UP out of null zone to limit jitter
    digitalWrite(dir_piny, LOW); //motor direction signal to Y axis Easy Driver DIR pin (OFF)
    YStepDelay = map(yValue, 0, 480, ylow, yhigh); //
  } else {
    digitalWrite(dir_piny, LOW); //motor direction signal to Y axis Easy Driver DIR pin (OFF)
    digitalWrite(dir_pinx, LOW); // motor direction signal sent to Easy Driver X axis DIR pin (OFF)
    // Setting LOW saves a bit of power
    //could include signal to set Easy Driver(s) in "sleep" mode for geared drivetrains
  }
  //_____________
  // If X axis (L/R) (Azimuth) motor needs to take a step, give it a step for the correct length of time,
  //when that time has expired end the pulse signal
  if ((xValue >= 520 || xValue <= 480) && ((unsigned long)(currentMillisX - previousMillisX) >= XStepDelay)) {
    //if the joystick X axis values goes outside the null zone calculate
    //how long it's been against those joystick values and control step pulse length
    xaxisState = !xaxisState; // logic switch between states of step pulse pin on/off
    digitalWrite (step_pinx, xaxisState); //control signal for step pin
    //if the motor pulse is off, turn it on, and if the pulse is on, turn it off
    previousMillisX = currentMillisX; //save the last x channel/axis pulse time on or off
  }
  else if (xValue <= 520 || xValue >= 480){ //if the joystick input isn't out of the null zone
    digitalWrite(step_pinx, LOW); //turn off the step pulse
  }
  //________________
  // If Y axis (U/D) (Altitude) motor needs to take a step, give it a step over the correct length of time,
  //after that time has expired end the pulse signal
  if ((yValue >= 520 || yValue <= 480) && ((unsigned long)(currentMillisY - previousMillisY) >= YStepDelay)){
    //if the joystick Y axis values goes outside the null zone calculate
    //how long it's been against those joystick values and control step pulse length
    yaxisState = !yaxisState; // logic switch between states of step pulse pin on/off
    digitalWrite (step_piny, yaxisState); //control signal for step pin
    //if the motor pulse is off, turn it on, and if the pulse is on, turn it off
    previousMillisY = currentMillisY; //save the last y channel/axis pulse time
  }
  else if (yValue <= 520 || yValue >= 480){ //if the joystick input isn't out of the null zone
    digitalWrite(step_piny, LOW); //turn off the step pulse
    //
    //restart loop
  }
}




Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Ads by Google


Re: Arduino Code Question

#28  Postby Calilasseia » Jan 24, 2019 4:22 am

Always produces a warm feeling when you get your code working, doesn't it? :)
Signature temporarily on hold until I can find a reliable image host ...
User avatar
Calilasseia
RS Donator
 
Posts: 22034
Age: 58
Male

Country: England
United Kingdom (uk)
Print view this post

Re: Arduino Code Question

#29  Postby felltoearth » Jan 24, 2019 4:32 am

Noice!
"Walla Walla Bonga!" — Witticism
User avatar
felltoearth
 
Posts: 12906
Age: 52

Canada (ca)
Print view this post

Re: Arduino Code Question

#30  Postby theropod » Jan 24, 2019 5:05 am

Calilasseia wrote:Always produces a warm feeling when you get your code working, doesn't it? :)


In my case it is more akin to figuring out that a bicycle only has two wheels.

No, you’re absolutely right. I went through several iterations as this version number suggests. I gained a close approximation of what I imagined several versions back, but those darned delays were killing the smooth ramp up/down needed to make this useful in its designed application. I set the slowest speeds so low that if I have a target in my field of view and need to only bump the joystick to move the device a tiny bit. If, however, I want to slew across the sky I can bury the joystick and the little mount will zip right along. Now the Arduino seems to multitask as both motors can turn independently and simultaneously. The little processor is fast enough to produce interleaving steps as the program loops. I can’t imagine the effects a faster Arduino, with the avaiable much speedier processor, would provide.

As silly as it sounds I am next going to try to figure out my gear ratios and set up a zero position so as to simulate a half ass goto function. This will require a good deal of pure math, and implementation of right ascension/declination digital setting circles, and the inclusion of a keypad for inputting the target’s data. Of course this will require delving into even more programing obscurities, and may take months of fiddling around. I may attempt to drive the Arduino via serial communications from a PC laptop where ASCOM control has already been chiphered by those an order of magnitude more insightful.

It would also be a small thing to add another Easy Driver and control the fine focus from the same prototype. The Nano has plenty of input and output pins left for such hardware expansion. Since that control would only be a trivial thing to add to my current program. I would never need to track the pointer and focus at the same time. It would be nice to be able to make fine focus adjustments without having to touch the scope if conditions shifted, and especially so if attempting photography.

RS
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Re: Arduino Code Question

#31  Postby theropod » Aug 03, 2019 9:15 pm

Update:
My little snip of code has been featured on the web site of the designer of the Easy Driver. Just scroll down to section 1.8.

https://www.schmalzhaus.com/EasyDriver/ ... mples.html

Damn!

RS
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Re: Arduino Code Question

#32  Postby felltoearth » Aug 04, 2019 5:34 am

Well that must be satisfying. Congrats!
"Walla Walla Bonga!" — Witticism
User avatar
felltoearth
 
Posts: 12906
Age: 52

Canada (ca)
Print view this post

Re: Arduino Code Question

#33  Postby theropod » Aug 04, 2019 4:05 pm

F2E,

Yes, it is. Considering the “official” Arduino forum ruled my code trash, and generally said my efforts were wasted. One brave poster even claimed there was a fatal bug and that the sketch would never run. Never mind that I have the code up and running in a physical device. I find the Arduino community to be a bunch of condescending pricks. The thing is before this there are no working examples of proportional speed control using an analog joystick to be found anywhere on the net, including the official Arduino site. All I know is I spent several days grinding away at a coding language in which I am far from fluent, and with a LOT of help hacked together a pretty slick little program.

This code could be used to drive any two steppers (within current limits of Easy Drivers), and adding a third axis wouldn’t be that hard.

RS
Sleeping in the hen house doesn't make you a chicken.
User avatar
theropod
RS Donator
THREAD STARTER
 
Name: Roger
Posts: 7529
Age: 66
Male

Country: USA
United States (us)
Print view this post

Previous

Return to General Science & Technology

Who is online

Users viewing this topic: No registered users and 1 guest