Motor Directional Control using Mega

I'm hoping that the clever folks in this forum will be able to assist.

I'm using a Mega, with the hope of using it to control a piece of rotational equipment.

The idea is that the equipment is motor driven, with the rotation direction controlled by the Arduino. The Arduino will ultimately energise a relay for 'Clockwise' and another relay for 'Anti-Clockwise', de-energising the relay when a specified position is reached.

Thinking of the equipment rotation as a compass for a moment for illustrative purposes, the equipment has a mechanical 'stop' at the 180 degree position and a potentiometer will give feedback to the Arduino as regards current position.

I'll build the necessary hardware interfaces to provide the relay control and positional feedback for the Arduino.

The level of actual control is that the user would select a 'button' to send the equipment to a preset position from whatever the current position would be.

My problem, is that I can't work-out how to get the software to look at the current position, then look at the required position, then look at how close the current position is to the 180 degree mechanical stop point and then determine the most direct direction of travel so that the 180 degree mechanical stop point is not encountered.

Any help would be much appreciated - maybe someone has done this before or possibly knows of a software library containing something similar that I might be able to modify to my needs?

Thanks!

the equipment has a mechanical 'stop' at the 180 degree position

What amount of rotation are you talking about? From 0 to 180? From 0 to 360? From -180 to 180?

a potentiometer will give feedback

A rotary encoder might be a better choice.

the equipment is motor driven, with the rotation direction controlled by the Arduino. The Arduino will ultimately energise a relay for 'Clockwise' and another relay for 'Anti-Clockwise', de-energising the relay when a specified position is reached.

What kind of motor? AC? DC? Gearmotor? What torque? What are you moving?

If possible, I'd use a stepper motor and motor controller board. All the Arduino then would need to do is to tell the motor controller to step the motor in one direction or the other. No need to monitor position, as that's known at all times.

I can't work-out how to get the software to look at the current position

Why not? If you've got some sensor that reports the current position, analogRead it. Or is it not an analog sensor?

then look at the required position,

How is this defined?

then look at how close the current position is to the 180 degree mechanical stop point

This should be the easiest part. degreeOfTravelToStop = currentPosition - 180.

then determine the most direct direction of travel so that the 180 degree mechanical stop point is not encountered.

This depends on the range of motion for the device. Can it rotate 360 degrees?

Thanks for the reply!

In order of your comments:

The equipment is capable of rotating 360 degrees: 0 to 360.

The equipment itself is already in existence so no alternative positional feedback is possible. The potentiometer works well.

The motor is fed power by a couple of relays - one for clockwise and the other for anticlockwise. Again, the equipment itself is already in existence so I'll have to stay with using two relays switching the power to the motor. Again, not too much of a problem nominating the relevant output pins and energising a relay at the appropriate moment.

I've now got the positional feedback voltage read back into the Arduino and scaled from 0 to 10 volts to read 1 to 360 for display, as 0 degrees is the same as 360 degrees.

Desired position will be chosen from an array of preset positions selected by the user via 'faceplate' buttons, each button being allocated a 'degree figure'.

So, with a mechanical stop at the 180 degree position, an actual of say 210 degrees and then a selected preset desired position of 170 degrees occurs, the most 'direct' route would appear to be to rotate anti-clockwise from 210 degrees to 170 degrees. However, with the 180 degree stop in the way, the code needs to make the roatation of travel as 'clockwise' so as to avoid the mechanical stop.

This is the bit that I'm having trouble getting my head around as regards a methodology generally - some sort of brain block! :sunglasses:

I’ve now got the positional feedback voltage read back into the Arduino and scaled from 0 to 10 volts

This is a typo, right? The maximum input voltage should not exceed 5 volts.

If the from position and the to position are both greater than 180, or both less than 180, the angular amount and direction is straightforward. If the from position is less than 180 and the to position is greater than 180, or if the from position is greater than 180 and the to position is less than 180, then the angular amount is 360 minus the angle between the two positions. The direction depends on whether the to position is greater than, or less than, 180.

Draw a picture. There are only 6 possibilities:
To < 180, From < 180, To < From
To < 180, From < 180, To > From
To > 180, From > 180, To < From
To > 180, From > 180, To > From
To < 180, From > 180
To > 180, From < 180

Draw these out on 6 different circles. Draw an arrow from From to To. It will be easy to see what the sweep should be (From - To, To - From, 360 - From - To, or 360 - To - From) and the direction will be obvious, too.

Thanks PaulS.

The actual voltage from the positional pot is 10 volts, but I've dropped it down to 5 volts to suit the Arduino.

Thank you VERY much for your clarifying thoughts on the overall control philosophy - I simply couln't get my thoughts together on it for some reason.

I'll get on with the actual coding and see how I go with it.

Cheers!