The below script is an example of how to implement the limit switch action by using an analog sensor. This script can replace the build in analog input function of the firmware and can be used in cases that the analog sensor is affected by EMI. By having the analog signal as an input to the script, the noise can be filtered out and the clean signal can be used instead of the original that the firmware uses. 


In the below example, the analog position sensor that is connected to the gearbox of the motor gives a value from 0 to 3.3 V, depending on the motor position. 0.4 V have been configured as reverse limit switch and 3 V have been configured as forward limit switch. So when the motor goes at low position it stops and is allowed only to go at positive direction. If the motor is at high position it will stop and it will be allowed to go only at reverse direction.



The script can implement the same function by configuring the UP_LIMIT and DOWN_LIMIT parameters.



Additionally, it will have a 100 ms filter to smoothen the analog input readings. Because the limit switch function cannot be fully implemented by the script, the scrtipt will act as follows:


  • When the filtered value is at the low limit, it will configure the reverse limit switch at a high voltage so it will be costantly enabled 
  • When the filtered value is at the high limit, it will configure the forward limit switch to a low voltage so it will be constantly enabled
  • In the between, the limit switch operation will be disabled


The script follows. Keep in mind that this is an example and is not fully tested and validated by our side. 




'Disclaimer:


'The script is tested and validated by Roboteq and is believed to be fault-free. 

'The possibility always exists, however, that the particular configuration and/or use 

'condition uncovers a fault that escaped our validation test coverage. Always 

'extensively test the script under your use conditions prior to deploying it in the field. 


option explicit

 

#define FILTER_PERIOD 100 ' the number of samples also

#define PIN_NUMBER 'the analog input that the sensor is connected

#define UP_LIMIT 3100 'voltage of max position

#define DOWN_LIMIT 700 'voltage of low position

 

dim  AI[1000as integer '1000 elements array

dim time as integer

dim as integer

dim sum as integer

dim filterValue as integer

 

 

start:

 

AI[time] = getvalue(_AI,PIN_NUMBER'read analog input

 

 

if (time FILTER_PERIOD)

    

    time 0

    gosub filter

    

end if

 

time ++    

wait(1)

 

setcommand(_VAR,1,getvalue(_AI,PIN_NUMBER)) 'for debugging

 

gosub limitSwitch

 

goto start

 

filter:

 

sum 0

for andwhile FILTER_PERIOD

 

    sum AI[i] + sum

    

next

 

filterValue sum/i

setcommand(_VAR,2filterValue'for debuggng

return

 

limitSwitch:

 

 

if (filterValue UP_LIMIT' configure high limit switch at a low value

 

    setconfig(_AMAX,500)

    setconfig(_AMAXA,7,20)

 

end if

 

if (filterValue DOWN_LIMIT'configure low limit switch at a high value

 

    setconfig(_AMIN,3000)

    setconfig(_AMINA,7,21)

    

end if

 

if ((filterValue < (UP_LIMIT-100)) and (filterValue > (DOWN_LIMIT+100))) 'clear the action

 

    setconfig(_AMAXA,7,16)

    setconfig(_AMINA,7,16)

    

end if

 

 

return