Running an external program from a HAL pin
- PCW
-
- Away
- Moderator
-
Less
More
- Posts: 18434
- Thank you received: 5034
11 Apr 2025 21:51 - 11 Apr 2025 21:58 #326122
by PCW
Replied by PCW on topic Running an external program from a HAL pin
Another possibility would be a script with halcmd in a loop,
watching the desired pin/signal (halcmd -s show pin spindle.0.on | grep TRUE)
(this could also turn off the spindle if LinuxCNC was not running)
watching the desired pin/signal (halcmd -s show pin spindle.0.on | grep TRUE)
(this could also turn off the spindle if LinuxCNC was not running)
Last edit: 11 Apr 2025 21:58 by PCW.
Please Log in or Create an account to join the conversation.
- Grotius
-
- Offline
- Platinum Member
-
Less
More
- Posts: 2376
- Thank you received: 2295
11 Apr 2025 22:12 #326123
by Grotius
Replied by Grotius on topic Running an external program from a HAL pin
@Pcw,
Yes, that should work.
Script below works.
$ halcmd -s show pin halui.estop.is-activated
prints:
halui bit OUT FALSE halui.estop.is-activated
Then get the FALSE or TRUE in a script:
Yes, that should work.
Script below works.
$ halcmd -s show pin halui.estop.is-activated
prints:
halui bit OUT FALSE halui.estop.is-activated
Then get the FALSE or TRUE in a script:
Warning: Spoiler!
#!/bin/bash
# Script to monitor halui.estop.is-activated every second
echo "Monitoring halui.estop.is-activated (Ctrl+C to stop)"
while true; do
# Get the status (extract TRUE/FALSE from the output)
status=$(halcmd -s show pin halui.estop.is-activated | awk '{print $4}')
# Get current timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Print the result
echo "[$timestamp] E-Stop Status: $status"
# Wait 1 second before checking again
sleep 1
done
Please Log in or Create an account to join the conversation.
- pgf
- Offline
- Senior Member
-
Less
More
- Posts: 74
- Thank you received: 10
12 Apr 2025 14:23 #326167
by pgf
Replied by pgf on topic Running an external program from a HAL pin
I spent my career trying to write reasonably efficient and fast code in firmware, device drivers and system level utilities. I'm afraid I just couldn't stomach adding a polling loop on top of the polling loop (every .2 seconds) that I discovered at the core of halui.
So I've done an end-run around the problem, which works nicely.
My mill runs from a Raspberry Pi, connected via a Mesa ethernet card. It occurred to me that if HAL could twiddle an actual gpio pin, then I could easily read that, and hook it up to action. And of course if there's one thing the RPi has, it's plenty of gpio.
So I loaded hal_gpio, and hooked up a couple of output pins:
Those pins were chosen because they were eacy immediately adjacent to another unused pin. Gpio 16 is next to 19, and 20 is next to 26. I jumpered those two pairs.
Then I wrote a short script using gpiomon to watch pins 19 and 26 for transitions.
This works perfectly.
While I'm pleased to have found this solution, it sure seems silly to have had to consume 4 gpio pins in order to do it.
Thanks for all your suggestions!
paul
So I've done an end-run around the problem, which works nicely.
My mill runs from a Raspberry Pi, connected via a Mesa ethernet card. It occurred to me that if HAL could twiddle an actual gpio pin, then I could easily read that, and hook it up to action. And of course if there's one thing the RPi has, it's plenty of gpio.
So I loaded hal_gpio, and hooked up a couple of output pins:
loadrt hal_gpio outputs=GPIO16,GPIO20
addf hal_gpio.write servo-thread
net external-estop-off \
estop-latch.0.fault-out => hal_gpio.GPIO16-out
net coolant-mist => hal_gpio.GPIO20-out
Those pins were chosen because they were eacy immediately adjacent to another unused pin. Gpio 16 is next to 19, and 20 is next to 26. I jumpered those two pairs.
Then I wrote a short script using gpiomon to watch pins 19 and 26 for transitions.
#!/bin/bash
estop=19
vacuum=26
switch()
{
echo Turning $1 $2 # e.g. "Turning vacuum on"
wget -q -O /dev/null http://service:9901/event:cnc-mill-$1-$2 &
}
while read pinevent
do
echo got $pinevent # this will be "19-1", "19-0", "26-1", or "26-0"
case $pinevent in
$estop-1)
switch spindle off
;;
$vacuum-0)
switch vacuum off
;;
$vacuum-1)
switch vacuum on
;;
esac
done < <( gpiomon --line-buffered --format="%o-%e" gpiochip0 19 26)
This works perfectly.
While I'm pleased to have found this solution, it sure seems silly to have had to consume 4 gpio pins in order to do it.
Thanks for all your suggestions!
paul
The following user(s) said Thank You: tommylight
Please Log in or Create an account to join the conversation.
Time to create page: 0.289 seconds