new hal modul
15 Dec 2020 16:37 #192052
by chris@cnc
new hal modul was created by chris@cnc
I would like to write a module for a mist extraction system that can also be used for interval control. At the moment I'm still helping myself with a shell script. But I would like to implement it. Unfortunately I couldn't find any examples of a linuxcnc module and my knowledge of C is minimal. Maybe someone can help.
The module must have 3 inputs.
Button 1 on -> output true
Button 2 on -> variable (spindel.enable) -> output true
Button 3 on -> variable (timer.off) -> output false
Variable (timer.on) -> output true
all buttons off -> output false
The module must have 3 inputs.
Button 1 on -> output true
Button 2 on -> variable (spindel.enable) -> output true
Button 3 on -> variable (timer.off) -> output false
Variable (timer.on) -> output true
all buttons off -> output false
Attachments:
Please Log in or Create an account to join the conversation.
- Todd Zuercher
- Offline
- Platinum Member
Less
More
- Posts: 5007
- Thank you received: 1441
15 Dec 2020 17:22 #192057
by Todd Zuercher
Replied by Todd Zuercher on topic new hal modul
Please Log in or Create an account to join the conversation.
15 Dec 2020 17:35 - 15 Dec 2020 18:12 #192058
by chris@cnc
Replied by chris@cnc on topic new hal modul
yes thanks the first two bits are easy too. The problem is to create an interval with timedelay. I failed of it. The third button should switch the output true, for example 30s, then wait 5 minutes and then switch it on again for 30s.
My shell script looks like this.
halcmd unlinkp hm2_7i76e.0.7i76.0.0.output-01
while true
do
halcmd setp hm2_7i76e.0.7i76.0.0.output-01 1
sleep 30
halcmd setp hm2_7i76e.0.7i76.0.0.output-01 0
sleep 300
done
exit 0
My shell script looks like this.
halcmd unlinkp hm2_7i76e.0.7i76.0.0.output-01
while true
do
halcmd setp hm2_7i76e.0.7i76.0.0.output-01 1
sleep 30
halcmd setp hm2_7i76e.0.7i76.0.0.output-01 0
sleep 300
done
exit 0
Last edit: 15 Dec 2020 18:12 by chris@cnc.
Please Log in or Create an account to join the conversation.
15 Dec 2020 19:25 - 16 Dec 2020 11:38 #192067
by chris@cnc
Replied by chris@cnc on topic new hal modul
my first draft full of bugs. but i'm a mechanic. I hope you can see what I want.
Warning: Spoiler!
component mist "3 way input mist modul";
pin in bit in0 "Manual mist on";
pin in bit in1 "mist on if Programm run";
pin in bit in2 "mist intervall on if Programm run";
pin in bit in3 "On Timer default 30s" ;
pin in bit in4 "Off Timer default 300s";
pin out bit out0;
function _;
description """
Modul with 3 Input and possibility of interval out
1. input.0 true -> output true
2. input.1 true -> Variable halui.program.run true -> output true
2. input.2 true -> timer.0 true && timer.1 false -> output true""";
license "GPL";
;;
FUNCTION(_){
if ( in0 )
{ out0 = 1; }
else if ( in1 && halui.program.run )
{ out0 = 1; }
else if (in2 && halui.program.run )
{ while true
out0 = 1;
sleep(in3);
out = 0;
sleep(in4);
}
else
{ out0 = 0; }
}
Last edit: 16 Dec 2020 11:38 by chris@cnc.
Please Log in or Create an account to join the conversation.
15 Dec 2020 21:59 - 15 Dec 2020 22:39 #192077
by Grotius
Replied by Grotius on topic new hal modul
Hi Chris.
Good luck !
Good luck !
Warning: Spoiler!
component mist " ";
description
"""
//Compile :
//halcompile --compile mist.comp
//Halfile load :
//loadrt mist
//addf mist.0 servo-thread
""";
author "Chris@cnc";
license "GPL";
// Input pins
pin in bit Enable=0;
pin in float TimerOn = 30; //this value can be overwrited by setp command.
pin in float TimerOff = 300;
// Output pins
pin out bit MistOn=0;
pin out float totalseconds=0;
// Global variables
variable float totalnsec=0;
function _;
;;
#include "rtapi_math.h"
FUNCTION(_) {
// Activate
if(Enable){
// Timer
totalnsec = totalnsec + period;
totalseconds = totalnsec * 0.000000001;
if(totalseconds<=30 /*replace 30 by TimerOn */) {
MistOn=1;
}
if(totalseconds>30 && totalseconds<=330 /*replace 330 by (TimerOff+TimerOn) */){
MistOn=0;
}
if(totalseconds > 330){
totalseconds=0;
totalnsec=0;
}
}
}
Attachments:
Last edit: 15 Dec 2020 22:39 by Grotius.
The following user(s) said Thank You: chris@cnc
Please Log in or Create an account to join the conversation.
16 Dec 2020 01:17 #192089
by rodw
You can't use sleep or while in a component becasue you are in a loop that is called 1000 times a second. But you can count invocations or you can read the time on start and add fperiod to it every time though the component.
Often its good to think in terms of states and create a numbered list of states with enum and write a state machine with a switch statement.
Its good to see grotius helping. I helped him a lot when he was learning C so now he's paying it forward!
Replied by rodw on topic new hal modul
my first draft full of bugs. but i'm a mechanic. I hope you can see what I want.
component mist "3 way input mist modul"; pin in bit in0 "Manual mist on"; pin in bit in1 "mist on if Programm run"; pin in bit in2 "mist intervall on if Programm run"; pin in bit in3 "On Timer default 30s" ; pin in bit in4 "Off Timer default 300s"; pin out bit out0; function _; description """ Modul with 3 Input and possibility of interval out 1. input.0 true -> output true 2. input.1 true -> Variable halui.program.run true -> output true 2. input.2 true -> timer.0 true && timer.1 false -> output true"""; license "GPL"; ;; FUNCTION(_){ if ( in0 ) { out0 = 1; } else if ( in1 && halui.program.run ) { out0 = 1; } else if (in2 && halui.program.run ) { while true out0 = 1; sleep(in3); out = 0; sleep(in4); } else { out0 = 0; } }
You can't use sleep or while in a component becasue you are in a loop that is called 1000 times a second. But you can count invocations or you can read the time on start and add fperiod to it every time though the component.
Often its good to think in terms of states and create a numbered list of states with enum and write a state machine with a switch statement.
Its good to see grotius helping. I helped him a lot when he was learning C so now he's paying it forward!
Please Log in or Create an account to join the conversation.
16 Dec 2020 11:20 - 16 Dec 2020 11:31 #192122
by chris@cnc
Many Thanks Grotius. I can feel and see your smile ...
I will try it in the next few days
Many Thanks rodw that you point out that my idea will not work. I had not understand what grotius written.
Could you send me a link for some code examples. One big question is how input halui.program.run or is not possible to input nc signals?
Replied by chris@cnc on topic new hal modul
Hi Chris.
Good luck !
Many Thanks Grotius. I can feel and see your smile ...
I will try it in the next few days
Many Thanks rodw that you point out that my idea will not work. I had not understand what grotius written.
Could you send me a link for some code examples. One big question is how input halui.program.run or is not possible to input nc signals?
Last edit: 16 Dec 2020 11:31 by chris@cnc.
Please Log in or Create an account to join the conversation.
16 Dec 2020 12:06 #192127
by rodw
Replied by rodw on topic new hal modul
The best way to learn programming is to read code. All of the standard code for components lives on git here
github.com/LinuxCNC/linuxcnc/tree/master/src/hal/components
Pick one of the simple components like or2 or and2 that you use and understand, then review the source for it.
The idea behind hal is that each component is stand alone iits own compartment so if it fails, everything else still works. So the only way to use another pin such as halui.program-on is to create a new in pin of the same data type and use a hal net statement to connect it to the in pin.
There are also many ways to skin a cat in C. Grotius used a hal variable called period to count milliseconds and convert it to seconds for your timer. There is another implicit varuable called fperiod which counts in seconds so I would probably do it like this because we are already using floating point numbers.
where using += is shorthand for totalnsec = totalnsec + fperiod;
Its cleaner and easier to read.
floating point numbers are much bigger and take more processing overhead to work with so we only use them when we have to but that is probably a hangover from an earlier era where PC's did not have the grunt they do today. But think of that if you are working with an arduino.
github.com/LinuxCNC/linuxcnc/tree/master/src/hal/components
Pick one of the simple components like or2 or and2 that you use and understand, then review the source for it.
The idea behind hal is that each component is stand alone iits own compartment so if it fails, everything else still works. So the only way to use another pin such as halui.program-on is to create a new in pin of the same data type and use a hal net statement to connect it to the in pin.
There are also many ways to skin a cat in C. Grotius used a hal variable called period to count milliseconds and convert it to seconds for your timer. There is another implicit varuable called fperiod which counts in seconds so I would probably do it like this because we are already using floating point numbers.
// Timer
totalnsec += fperiod;
where using += is shorthand for totalnsec = totalnsec + fperiod;
Its cleaner and easier to read.
floating point numbers are much bigger and take more processing overhead to work with so we only use them when we have to but that is probably a hangover from an earlier era where PC's did not have the grunt they do today. But think of that if you are working with an arduino.
The following user(s) said Thank You: chris@cnc
Please Log in or Create an account to join the conversation.
16 Dec 2020 19:32 #192160
by chris@cnc
Replied by chris@cnc on topic new hal modul
For my first lesson not so bad. Modul is working and timer is one include? May i can count them
with original timer example i got error
with original timer example i got error
mist.comp: In function ‘_’:
mist.comp:71:12: error: invalid storage class for function ‘__comp_get_data_size’
mist.comp:71:1: error: expected declaration or statement at end of input
mist.comp: At top level:
mist.c:45:12: warning: ‘__comp_get_data_size’ used but never defined
static int __comp_get_data_size(void);
^~~~~~~~~~~~~~~~~~~~
Warning: Spoiler!
component mist " ";
description
"""
//Compile :
//halcompile --compile mist.comp
//Halfile load :
//loadrt mist
//addf mist.0 servo-thread
""";
author "Chris@cnc";
license "GPL";
// Input pins
pin in bit in0=0;
pin in bit in1=0;
pin in bit in2=0;
//pin in float TimerOn = 30; //this value can be overwrited by setp command.
//pin in float TimerOff = 300;
// Output pins
pin out bit on=0;
//pin out float totalseconds=0;
// Global variables
variable float totalnsec=0;
variable float totalseconds=0;
function _;
;;
#include "rtapi_math.h"
FUNCTION(_) {
if(in0) on=in0;
// if(in2){
// Timer
// totalnsec = totalnsec + period;
// totalseconds = totalnsec * 0.000000001;
// if(totalseconds<=30 /*replace 30 by TimerOn */) {
// MistOn=1;
// }
// if(totalseconds>30 && totalseconds<=330 /*replace 330 by (TimerOff+TimerOn) */){
// MistOn=0;
// }
//
// if(totalseconds > 330){
// totalseconds=0;
// totalnsec=0;
// }
else {
on=0;
}
}
Attachments:
Please Log in or Create an account to join the conversation.
16 Dec 2020 20:31 #192183
by rodw
Replied by rodw on topic new hal modul
I think in your first example you had a data type error becasue you had a bit as the input pin (which can only be 0 or 1) and you were using it to set it to 300. That likely explains the error messages.
Please Log in or Create an account to join the conversation.
Time to create page: 0.119 seconds