Control script for test rig

5 years 9 months ago #29529912 by SimonPayne
Hi All,

I am currently trying to write a bit of code to control a motor with 2 limit switches. The motor drives a chain up and down that (should) produce a reciprocation motion for a cyclic test rig. So far I can get the motor to run and stop ok when it hits one switch, but reversing it and repeating the operation in the opposite direction, so that the rig runs automatically without manual intervention (or resorting to encoders!) has so far eluded me. Not being a programmer I am a bit stuck, so any help would be most welcome.
Thanks in advance
Simon.

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529913 by TechSupport
So are you trying to say that when the motor runs forward, and you flip the switch, it says to stop moving forward(forward limit switch) and then the same in reverse?

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529917 by SimonPayne
Hi there, thanks for getting back to me.
Yes in a sense that is what I am attempting to do, although the limit switches are fixed on the rig, they are not moved by hand in case that is what you meant.. Basically the rig uses a motor to drive a chain, to get motion up and down between the limits (see pic attached to get a bit more idea of what I mean). I have grasped the basics of moving the motor and reading in the sensor digital inputs but not quite sure how to approach programming this type of task. The controller is an HDC2450 by the way.
Regards,
Simon.
Attachments:

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529918 by TechSupport
It all depends on how those switches get triggered.

When the switch goes high, then you can set the action for forward and reverse limit switches.

Which inputs are you using?

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529919 by SimonPayne
Hi, well not sure yet but was intending to use DIN1 and DIN2 to connect the limit switches, but could use any other of the digital inputs. I completely forgot that you can set up an action for the switches in the config setup, is this a way round it?
I have attached a bit of code that I thought might work, but not entirely sure if it will.. let me know what you think!
Regards
Simon.

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529920 by TechSupport
You can use that or microbasic scripting to do this.

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529921 by TechSupport
Let's assume you go with Din1 and Din2 as your switches.

*Note code is untested or compiled* For purposes of making this easier to read, I've added spaces.

option explicit ' Force declare all variables - troubleshooting purposes

dim Fswitch as integer
dim Rswitch as integer

top: ' loop label

Fswitch = getvalue(_DI, 1)
Rswitch = getvalue(_DI, 2)

if Fsiwtch = 1 then
setconfig(_dina, 1, 4) ' sets forward limit switch digital input 1
else
setconfig(_dina, 1, 0) ' Sets back to no action
end if

if Rswitch = 1 then
setconfig(_dina, 2, 4) ' sets forward limit switch digital input 2
else
setconfig(_dina, 2, 0) ' Sets back to no action
end if

wait(1) ' waits 1mS
goto top ' repeats loop forever

This works in the event there is someone either triggering these switches on and off, or there is something else triggering the switches to be active.
The following user(s) said Thank You: SimonPayne

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529924 by SimonPayne
Hi, many thanks I will give that a go and let you know how it works out.
Cheers
Simon.

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529925 by SimonPayne
Hi, well the code runs and the sensors are responding, all good so far, but presumably I need to start the motor somehow in the script? Should I use setcommand?

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29529927 by TechSupport
Yes you would use the setcommand. You may need to disable the watchdog timer in the script as well in the loop so that the motors don't time out.

setcommand(_RWD, 0)

Motor command would use the setcommand(_G, cc, nn) where cc is the motor channel and nn is the value command.

Please Log in or Create an account to join the conversation.

Time to create page: 0.153 seconds