How to create multiple commands for a single stepper motor

Hiya all,

I have been reading up and learning Arduino sketches.

One thing I am trying to find out about is how to control a stepper motor with different commands that relate to predetermined distances.

I will be having a home position and from that home position I need to provide a unique command that will initiate the motor to move to point A (then other motors will perform some actions) then the stepper motor requires to move to point B (the same other motors will perform other actions) and then stepper motor will return to home.

There are approximately 15 variations of the above.

The question is where can I read up and learn how to set up a input structure that differentiates between each of the 15 commands.
In other words when I push a button on a touch screen that is assigned to on of the 15 commands and performs that specific loop.

The other stepper motors will perform the same functions every time so I am not worried about that. I just want to find out how I can write a sketch that lists all the commands and are assigned to a specific input.

many thanks

Rebekah Anderson

Sending a simple message that is unique for each option should be sufficient. If the total number of messages is less than 66 then you could send a single alpha-numeric character for each option. If you need more unique messages then use two characters - that will allow for about 4000 unique options.

Using a single character makes the Arduino code simple.

A very different type of solution (which i use with a small CNC lathe) is to keep track of the position on the PC and send to the Arduino a command with the number of steps to move - for example <376>. See the 3rd example in Serial Input Basics

Or have I completely misunderstood the problem?

...R

I think you may have grasped what I was on about.

having quickly looked at the serial input, it looks like it's what I am after.

to make life easier to associate stuff with the action. I think it would serve me well to use two characters eg a1 to a4, b1 to b4, c1 to c4 etc...

basically I will be building a transmitter that will send the command a1 and the stepper motor will perform a function, then if i send command b3 it will perform a different function. all it will do is effectively rotate a screw bar until the carrier reaches certain points like your CNC.

I will see how this will work with my idea.

thank you. if you have any other suggestions please let me know. But i now have a starting point. :slight_smile:

Rebekah Anderson

If you are sending 2-part commands that are always only 2 characters they can be easily parsed something like this (assuming the data is in receivedChars[])

firstPart = receivedChars[0];
secondPart = receivedChars[1];

However if your commands are more complex that 2 characters it would probably be easier to parse them if you separate the parts with commas such as <b,123>

...R

rebekah_harper:
...thank you. if you have any other suggestions please let me know. But i now have a starting point. :slight_smile:

Don't write your program all in one go, build it up.

You are going to have about 15 commands. Write a function that does the actions required for one command, say a1. Call the command, check it does what you want. Once you have one command working add the others one at a time testing as you go.

Next write a sketch that reads commands and prints out what is received. Once you have that working change it to call the appropriate function in response to each command.

Sorry if these instructions are a bit basic for you, some people just pile in and then end up in a tangle.

Happy days,
I am just working my way through some resource material.

I will take on your method of working with one step at a time.

The basic structure will represent something like this (I know it's not proper sketch but I'm just creating the idea of a frame work)

setup is:
Home is a micro switch end stop
Stepper motor 1 = main screw bar with carrier
Stepper motor 2 = locking pins on carrier
Stepper motor 3 = Unlocking pin boom

command A1 // move boom 1 to first position (125mm) on the main boom

stepper motor 1 = travel 15mm // move into place to pick up boom
stepper motor 2 = travel 4mm // locking the carrier to the boom
stepper motor 3 = travel -4mm // unlocking boom from parent boom
stepper motor 1 = travel 125mm // move to first location
stepper motor 3 = travel 4mm // lock boom into parent boom
stepper motor 2 = travel -4mm // unlocking carrier from boom
stepper motor 1 = travel home // carrier moves to the home location as set by end stop switch.

command A2 // move boom 1 to second position (325mm) on the main boom from home
command A3 // move boom 1 to second position (525mm) on the main boom from home

command B2 // move boom 2 to first position (125mm) on boom 1 from home

stepper motor 1 = travel 25mm // move into place to pick up boom
stepper motor 2 = travel 4mm // locking the carrier to the boom
stepper motor 3 = travel -4mm // unlocking boom from parent boom
stepper motor 1 = travel 125mm // move to first location
stepper motor 3 = travel 4mm // lock boom into parent boom
stepper motor 2 = travel -4mm // unlocking carrier from boom
stepper motor 1 = travel home // carrier moves to the home location as set by end stop switch.

I know that I need to add some steps in between to so that I can move the carrier from home to the first position, pick up the boom and then move it to second or third position.
which would make it:

command A5 // move boom 1 to third position (525mm) on the main boom from first position
stepper motor 1 = travel 140mm // move into place to pick up boom
stepper motor 2 = travel 4mm // locking the carrier to the boom
stepper motor 3 = travel -4mm // unlocking boom from parent boom
stepper motor 1 = travel 385mm // move to first location
stepper motor 3 = travel 4mm // lock boom into parent boom
stepper motor 2 = travel -4mm // unlocking carrier from boom
stepper motor 1 = travel home // carrier moves to the home location as set by end stop switch.

hence I think a two character identifier for the commands. allows me to allocate A to boom 1, B to boom 2, C to boom 3 etc...

These commands will be sent using a touchscreen interface, the structure of the commands I am still trying to work out. There are some conditionals I need to have so it prohibits some actions and allow others as long as certain rules are met.

If there is any further reading resources out there I can tap into please let me know. in the mean time I will be reading up on the suggested material.

Many thanks

Rebekah Anderson

rebekah_harper:
...These commands will be sent using a touchscreen interface, the structure of the commands I am still trying to work out....

I don't really know much about your application so this may be rubbish but here are some thoughts;

I find your terminology a bit confusing;
At one point you are saying there is only one stepper motor you are concerned with because its actions are variable whereas the other steppers perform fixed actions.
At other points though you are talking about booms.
Then you say things like move "boom 2 to first position (125mm) on boom 1 "
You also use the term "main boom" which presumably is boom 1.

It is obvious that you have three booms, but does each boom only have only one stepper?
How are the booms interrelated e.g. how can boom 2 move to a position on boom 1?
Is the booms or the steppers that you want to go through a sequence?

I would not worry about the number of characters you use for a command, it would be perfectly easy to interpret three or four, but try to make them meaningful.

For example you might prefix a command with B1/B2/B3 to identify the boom the command is for.
Then you might indicate if you want an Absolute move command a Relative move command or a Sequence A/R/S
Then give the command number 0/1/2/3/4/5/6/7/8/9

I would make Absolute command A0 always mean go home, so B2A0 would move boom 2 to its end stop.

Other examples of absolute positions;
B1A1 - move boom 1 to first absolute position (125mm)
B1A2 - move boom 1 to second absolute position (325mm)
B1A3 - move boom 1 to third absolute position (525mm)

Examples of relative positions would be;
B3R1 - move boom 3 by some predefined amount 1 from its current position e.g. -4mm
B3R2 - move boom 3 by some predefined amount 2 from its current position e.g. +4mm

Examples of sequences would be;
B2S4 - boom 2 is to perform predefined sequence 4
B3S5 - boom 3 is to perform predefined sequence 5

If the sequences themselves only contained relative instructions then you have quite a flexible setup because you can string the commands together e.g.

B1A3 - you have moved to one of ten absolute positions (including the end stop)
B1R9 - you have shifted away from that position by a relative amount
B1S5 - you have executed one of ten sequences

ardly:
I would not worry about the number of characters you use for a command, it would be perfectly easy to interpret three or four, but try to make them meaningful.

My suggestion in Replies #1 and #3 to use a very simple message style was based on the question in the Original Post that said "how to control a stepper motor with different commands" (my emphasis).

...R

Robin2:
My suggestion in Replies #1 and #3 to use a very simple message style was based on the question in the Original Post that said "how to control a stepper motor with different commands" (my emphasis).

...R

I did say my reply might be rubbish.

However the OP did also originally write "the same other motors will perform other actions". Then in post #6 the OP wrote "allows me to allocate A to boom 1, B to boom 2, C to boom 3 etc...".

So it is a bit confusing. There are definitely multiple things that commands are sent to.

ardly:
I did say my reply might be rubbish.

I certainly did not mean to imply that. The confusion seems to be caused by the OP changing the spec.

...R

Hiya,

the original spec hasn't changed. I may have jumped a bit which added confusion. I tried to keep it minimal so I could focus the question and get the answer. however I managed to confuse things.

the feature to be controlled is a multi stage boom which is telescopic. the main boom does not move in relation to the stepper motor. it is fixed and provides the frame work for booms 1 to 5. there is only one stepper motor that will move the boom sections. the single stepper motor moves a carrier that locks into the base of a selected boom and moves it to a predetermined location. it then locks that section into place. It can then pick up another boom section and move it as directed by the input and sketch.

the other stepper motors (2 and 3) are to do with the locking and unlocking of the boom sections and don't actually move any booms)

I really appreciate the help and direction. I will clarify what I am looking to achieve so I can maintain the support and advice.

many thanks

Rebekah Anderson (Bex)

rebekah_harper:
there is only one stepper motor that will move the boom sections. the single stepper motor moves a carrier that locks into the base of a selected boom and moves it to a predetermined location. it then locks that section into place. It can then pick up another boom section and move it as directed by the input and sketch.

I seem to remember you had another Thread about that with diagrams. It makes it easier to help if you keep all the questions in one Thread so all the info is in one place.

...R

rebekah_harper:
...the feature to be controlled is a multi stage boom which is telescopic. the main boom does not move in relation to the stepper motor. it is fixed and provides the frame work for booms 1 to 5. there is only one stepper motor that will move the boom sections. the single stepper motor moves a carrier that locks into the base of a selected boom and moves it to a predetermined location. it then locks that section into place. It can then pick up another boom section and move it as directed by the input and sketch....

I think I am starting to get the picture, it is a telescopic system with one fixed boom and five moveable booms.

Is the objective to get the end of the final boom to fixed position i.e. to telescope the whole thing out to a specific length?
If it is why don't you just enter the length you want and have the system calculate the optimal way of doing that?

There would seem to be three obvious ways you might achieve that;

  • Extend the near booms fully, then extend the next boom to make up any slack to reach the target.
  • Extend all the booms by equal amouts to reach the taget.
  • Extend the far booms fully, then extend the next boom to take up any slack to reach the target.

I don't know how big the booms are but if you entered a five digit number you could specify any length upto almost 100m with a precision of 1mm.
Commands could just be;
M03000 - extent to 3m
M04250 - extend to 4.25m
M10000 - extend to 10m
M00000 - extend to 0m i.e. go home.
Just thinking out loud.

hiya

@Robin2,

yes that is true. I just wanted to separate out specific questions out so it doesn't get lost in all the posts. Also I wanted to ask a detailed question.
but you're right and I apologise for the confusion

@ardly,
I think you have understood what my attempt on a question was. although I need to be able to control induvidual boom sections. but I am pretty sure what you conveyed is what I am looking for. The total length of the boom fully extended is just over 3m.
I think I am right in saying that within each of those command I describe the sequence of events for a specific boom section to move a specific distance.

sounds encouraging.

thank you.

Bex

rebekah_harper:
....The total length of the boom fully extended is just over 3m.
I think I am right in saying that within each of those command I describe the sequence of events for a specific boom section to move a specific distance.
...

If all you are concerned with is the total amount the system extends then why are you bothering to give commands to individual boom sections? Why not just send a command that says, for example, extend to 2.65m and let the Arduino calculate how to move the individual sections?

Since the maximum extent is 3m a five character command would let you specify positions with millimeter accuracy e.g. M2650.

rebekah_harper:
hence I think a two character identifier for the commands. allows me to allocate A to boom 1, B to boom 2, C to boom 3 etc...

It is MUCH easier to use a single character (even if reading two characters at a time - what happens if a single character gets lost in transmission?). Then you can always make your code more human readable by using #define statements.

E.g.:

#define B1A1 11
#define B1A2 12
#define B3R1 13
#define B3R2 14

loop() {
  if (Serial.available()) {
    byte c = Serial.read();
    switch (c) {
      case B1A1: // value 11 was received
        // Do the B1A1 sequence.
        break;
      case B1A2: // value 12 was received
        // Do the B1A2 sequence.
        break;

       // etc.
    }
  }

  // To send command:
  Serial.write(B3R1);
}

Best of both worlds: the ease of single character transmission (you have 255 numbers available for a single byte), ease of human readable code.

wvmarle:
Best of both worlds: the ease of single character transmission (you have 255 numbers available for a single byte), ease of human readable code.

And if you don't need more than about 62 variants just use alphanumeric characters for even easier debugging.

...R