- Posts: 2419
- Thank you received: 2349
custom hal component failed to compile, undeclared (first use in this function)
- Grotius
- Offline
- Platinum Member
-
Less
More
3 years 10 months ago #260081
by Grotius
Replied by Grotius on topic custom hal component failed to compile, undeclared (first use in this function)
Hi,
I share a little experience with you.
When coding in .comp language, there is no editor syntax plugin available.
As a result coding is a struggle.
To make life easyer while coding a more complex component i think you can
consider the following :
1. preprocess your component to .c with halcompile. Then use a code editor like qt.
There are example .c components, that are human readable.
The problems you then have is setting the correct
includepath's etc. Then create a makefile wich an sich is quite difficult for a lcnc component.
After the preprocess, you will be able to extend code in the function area.
Then also qt can help you with the "switch case" function. It has build in auto complete for this.
2. A more powerfull way to create a component is using cmake. This is in my view the most
elegant solution to create a component. Where halcompile gives to user power, using the cmake way
gives you full power and a bird eye view right away.
A example for a cmake component (lcnc compatible) :
github.com/grotius-cnc/hal-core-2.0/blob...mponents/test/test.c
A example for lcnc in cmake, including components etc. (good excersize to compile this, and try out a little bit)
github.com/grotius-cnc/linuxcnc
For what i have seen in your last few posts. You could open a new nml channel inside your new component, to
set the initial tool values, or do other nml related things.
Here you can see how to use nml into your component :
github.com/grotius-cnc/linuxcnc/blob/mai...rojects/qt_nml/nml.h
If you want to try it in your current lcnc install. Try to use a example cmakefile.txt for a component and edit the includepath's, libdir's to your needs.
The only thing i can say, have a try. If you got it running, you won't regret the effort.
I share a little experience with you.
When coding in .comp language, there is no editor syntax plugin available.
As a result coding is a struggle.
To make life easyer while coding a more complex component i think you can
consider the following :
1. preprocess your component to .c with halcompile. Then use a code editor like qt.
There are example .c components, that are human readable.
The problems you then have is setting the correct
includepath's etc. Then create a makefile wich an sich is quite difficult for a lcnc component.
After the preprocess, you will be able to extend code in the function area.
Then also qt can help you with the "switch case" function. It has build in auto complete for this.
2. A more powerfull way to create a component is using cmake. This is in my view the most
elegant solution to create a component. Where halcompile gives to user power, using the cmake way
gives you full power and a bird eye view right away.
A example for a cmake component (lcnc compatible) :
github.com/grotius-cnc/hal-core-2.0/blob...mponents/test/test.c
A example for lcnc in cmake, including components etc. (good excersize to compile this, and try out a little bit)
github.com/grotius-cnc/linuxcnc
For what i have seen in your last few posts. You could open a new nml channel inside your new component, to
set the initial tool values, or do other nml related things.
Here you can see how to use nml into your component :
github.com/grotius-cnc/linuxcnc/blob/mai...rojects/qt_nml/nml.h
If you want to try it in your current lcnc install. Try to use a example cmakefile.txt for a component and edit the includepath's, libdir's to your needs.
The only thing i can say, have a try. If you got it running, you won't regret the effort.
Please Log in or Create an account to join the conversation.
- andypugh
- Offline
- Moderator
-
Less
More
- Posts: 19899
- Thank you received: 4655
3 years 10 months ago - 3 years 10 months ago #260091
by andypugh
You have "switch(progress_level)" three times.
You do not have the contents of the switch in {braces}
No, but my single "if" replaces _all_ of yours.
I am tired of this. I am dropping out of this thread.
Replied by andypugh on topic custom hal component failed to compile, undeclared (first use in this function)
exactly how is my switch statement incorrect, because looking at all the books and articles i have here on C, it doesn't.
You have "switch(progress_level)" three times.
You do not have the contents of the switch in {braces}
the statement
if all these things are true = X.
doesn't change by putting strobe as a condition ahead of the statement.
No, but my single "if" replaces _all_ of yours.
I am tired of this. I am dropping out of this thread.
Last edit: 3 years 10 months ago by andypugh.
Please Log in or Create an account to join the conversation.
- rodw
- Offline
- Platinum Member
-
Less
More
- Posts: 12170
- Thank you received: 4153
3 years 10 months ago - 3 years 10 months ago #260109
by rodw
Replied by rodw on topic custom hal component failed to compile, undeclared (first use in this function)
I am a bit like Andy and don't want to wade in either but I suspect the current decoding algorithm is not a good choice.
So many ways to do this.
I think I would convert bcd1-bcd4 to an integer and use an enum (which might need changing around a bit so the case statement is arranged better.)
I might not quite understand the bcd states, but something like this....
This should end up with clearer code
So many ways to do this.
I think I would convert bcd1-bcd4 to an integer and use an enum (which might need changing around a bit so the case statement is arranged better.)
I might not quite understand the bcd states, but something like this....
Code:
enum tool_state(T0, T1, T2, T3, T4, T5, T6, T7,T8, T9, T10, T11, T12, T13, T14, T15);
/*
Tool States where bcd1 = rightmost (bit0)
T0 = 0000
T1 = 0001
T2 = 0010
T3 = 0011
T4 = 0100
T5 = 0101
T6 = 0110
T7 = 0111
T8 = 1000
T9 = 1001
T10 = 1010
T11 = 1011
T12 = 1100
T13 = 1101
T14 = 1110
T15 = 1111
*/
enum tool_state tool_idx;
tool_idx = (tool_state) bcd1 & (bcd2 << 1) & (bcd3 << 2) & (bcd4 << 3);
if(!strobe){
switch(tool_idx){
case T6: // 0b0110
position = 1;
break;
case T5:
position = 2; // 0b0101
break;
// .......
}
}
This should end up with clearer code
Last edit: 3 years 10 months ago by rodw.
The following user(s) said Thank You: smc.collins
Please Log in or Create an account to join the conversation.
- smc.collins
- Offline
- Platinum Member
-
Less
More
- Posts: 723
- Thank you received: 139
3 years 10 months ago #260209
by smc.collins
Replied by smc.collins on topic custom hal component failed to compile, undeclared (first use in this function)
I'm not really interested or concerned with writing the smallest possible version of a program, particularly when I am prototyping it. I am absolutely choosing to use a very big version of this truth table until I decide that optimizing it is a priority, which it isn't. What is a priority, is having high legible easy to understand code that is easily readable to look for faults. I do appreciate your input Rod, thank you, I will consider adding the lookup table design at a later date. My comments are not directed at you.
so I decided after wading through more of hal, that it would be easier to train the new GPTchat bot with it. BTW this makes writing components easier as you can sketch your code, explain to the chat bot what you are trying to make and it will help you with debugging, logic etc, and it now knows the hal syntax, or my version of the bot does. It is a tremendous learning aid to say the least.
anyhow. I will be testing this version and the powerchuck comp " which I need to work out the gcode mapping for" once this -4 F weather clears out. powerchuck is a chucker program for hydrualic and pnuematic chucks, very simple clean and elegant. offer manual and gcode control options and is stone simple. I am very suprised no one has posted a chuck control component yet, maybe I missed it ?
For those who have contributed positive and helpful comments, thank you very much for your time it is appreciated greatly.
so I decided after wading through more of hal, that it would be easier to train the new GPTchat bot with it. BTW this makes writing components easier as you can sketch your code, explain to the chat bot what you are trying to make and it will help you with debugging, logic etc, and it now knows the hal syntax, or my version of the bot does. It is a tremendous learning aid to say the least.
anyhow. I will be testing this version and the powerchuck comp " which I need to work out the gcode mapping for" once this -4 F weather clears out. powerchuck is a chucker program for hydrualic and pnuematic chucks, very simple clean and elegant. offer manual and gcode control options and is stone simple. I am very suprised no one has posted a chuck control component yet, maybe I missed it ?
For those who have contributed positive and helpful comments, thank you very much for your time it is appreciated greatly.
Please Log in or Create an account to join the conversation.
- smc.collins
- Offline
- Platinum Member
-
Less
More
- Posts: 723
- Thank you received: 139
3 years 10 months ago - 3 years 10 months ago #260491
by smc.collins
Replied by smc.collins on topic custom hal component failed to compile, undeclared (first use in this function)
component cinturnchanger """This component controls the Cincinnati Cinturn 12u Lathe 6 position 12 tool turret. M6 calls this""";
pin in bit toolchange "Receives signal that tool change required";
pin in s32 toolnumber """Receives Tx data (tool number requested) Only allows 1-12""";
pin in s32 currenttoolnumber "Receives old tool number";
pin out bit toolchanged = false "Sends signal when tool change finished";
pin out bit delaystart = false "Starts timerdelay";
pin in bit delaydone =false "Signals timer finished";
pin in bit bcd1 = true "State of bcd switch 1";
pin in bit bcd2 = false "State of bcd switch 2";
pin in bit bcd3 = false "State of bcd switch 3";
pin in bit bcd4 = false "State of bcd switch 4";
pin in bit turretunlocked = false "State of turret unlocked switch";
pin in bit turretlocked = false "State of turret locked switch";
pin in bit strobe = false "State of strobe switch";
pin out bit motor = false "turret motor solenoid";
pin out bit unlock = false "turret unlock solenoid";
pin in bit ishomedX = false "Status of X axis homing";
pin in bit ishomedZ = false "Status of Z axis homing";
param rw s32 pocket = 0 "create pocket and use for debugging of truth table";
param rw float times = 500 """Number of polls of progress_levels 1 & 3 before beginning next move - gives delay for relays""";
pin out s32 progress_level = 0; // show progress of component in stages
variable bool bWarn = false; // first toolnumber reminder
option singleton yes; // change to no for multi turret machine, configured for single turret machine originally
function _;
author "OPEN AI CHATGPT and some guy with a broken lathe";
license "MIT";
;;
#include <unistd.h>
#include <time.h>
int i, d, pow;
int pcalc = 0;
int mod_pocket = 0;
int new_pos = 0;
unsigned int mask;
int align_pin = 0; // used for slow align move, input depends on mode for historical reasons
// BCD or Binary Coded Switch Decoding to determine turret position.
align_pin = strobe;
i = 0;
pow = 1;
pocket = 0;
while (i < 3) { // we only have 3 input bits: bcd1, bcd2, bcd3
int lim;
d = 0;
for (lim = i + 4; i < lim && i < 3; i++) {
d += (bcd1 & (1 << i)) + (bcd2 & (1 << i)) + (bcd3 & (1 << i)) << (i % 4);
pcalc ^= (bcd1 & (1 << i)) + (bcd2 & (1 << i)) + (bcd3 & (1 << i));
}
pocket += d * pow;
pow *= 10;
}
rtapi_print_msg(RTAPI_MSG_INFO, "Decoded pocket number: %d", pocket);
switch (progress_level)
{
case 1: // idle state waiting for tool change request
if (progress_level == 0)
{
if(!currenttoolnumber && !bWarn)
{
bWarn = true; // just warn once, its not an error as such but INFO won't display unless debugging is set 3+
rtapi_print_msg(RTAPI_MSG_ERR, "No tool selected. Use M6Tx to set current tool");
}
}
progress_level = 1;
break;
case 2: // check if toolnumber is within the bounds of the truth table
if (progress_level == 1)
{
if (toolnumber > 1 && toolnumber < 6)
{
progress_level = 2;
break;
}
else
{
rtapi_print_msg(RTAPI_MSG_ERR, "Invalid tool number. Use M6 Tx to select a tool between 1 and 6");
progress_level = 9;
break;
}
}
case 3:
if (progress_level == 2)
{
if (toolnumber == pocket) // If the toolnumber and pocket are the same, exit the program
{
toolchanged = true;
break;
}
else if (toolnumber != pocket) // If the toolnumber and pocket are different, proceed to the next case
{
progress_level = 3;
}
}
break;
case 4:
if (progress_level == 3)
{
if (toolnumber != pocket) // if requested toolnumber is different than truth table perform toolchange
{
// First tool change loop to run turret
unlock = true;
sleep(1); // Wait at least 1 second
motor = true;
// Set a timeout of 10 seconds
time_t start_time = time(NULL);
while (motor)
{
if (pocket == toolnumber)
{
motor = false;
}
if (time(NULL) - start_time > 10)
{
rtapi_print_msg(RTAPI_MSG_ERR, "Error: Motor loop timed out");
motor = false;
progress_level = 9;
break;
}
}
sleep(2); // Wait at least 2 seconds
if (strobe && pocket == toolnumber)
{
unlock = false;
}
else
{
rtapi_print_msg(RTAPI_MSG_ERR, "Error: Turret align failed");
progress_level = 9;
break;
}
}
}
progress_level = 4;
break;
case 5: // alert iocontrol loop that tool change is complete
if (progress_level == 4)
{
if (toolnumber == pocket);
if (turretlocked == true);
toolchanged = true; // signal finished
}
progress_level = 5;
break;
// should never get here but if we do then loop endlessly doing nothing
case 10:
if (progress_level == 9)
{
// rtapi_print_msg(RTAPI_MSG_ERR,
// "Error state in toolchanger - now disabled - unload toolchanger");
}
progress_level = 10;
break;
}
this is the current state of the code after working with chat gpt and learning my way around C more. It appears to have cribbed some of the code from carousel for decoding the bcd inputs, and I will ensure that the proper attributions are given afaict. I will simply put this under mit and allow for anyone who this may have inadvertently copied to put in open claims. Obviously some of this is my work, some is GPT's. It's almost working. maybe soon.adding error checking, fault states etc. and some debugging messages now to. I think the case switch statements are working properly now. haven't had a chance to test yet. I am probably going to end up rewriting the state machine with else if statements, the case logic seems to break constantly and the halcompile preprocessor seems to compile or compose it in such a way that it won\t execute reliably.
pin in bit toolchange "Receives signal that tool change required";
pin in s32 toolnumber """Receives Tx data (tool number requested) Only allows 1-12""";
pin in s32 currenttoolnumber "Receives old tool number";
pin out bit toolchanged = false "Sends signal when tool change finished";
pin out bit delaystart = false "Starts timerdelay";
pin in bit delaydone =false "Signals timer finished";
pin in bit bcd1 = true "State of bcd switch 1";
pin in bit bcd2 = false "State of bcd switch 2";
pin in bit bcd3 = false "State of bcd switch 3";
pin in bit bcd4 = false "State of bcd switch 4";
pin in bit turretunlocked = false "State of turret unlocked switch";
pin in bit turretlocked = false "State of turret locked switch";
pin in bit strobe = false "State of strobe switch";
pin out bit motor = false "turret motor solenoid";
pin out bit unlock = false "turret unlock solenoid";
pin in bit ishomedX = false "Status of X axis homing";
pin in bit ishomedZ = false "Status of Z axis homing";
param rw s32 pocket = 0 "create pocket and use for debugging of truth table";
param rw float times = 500 """Number of polls of progress_levels 1 & 3 before beginning next move - gives delay for relays""";
pin out s32 progress_level = 0; // show progress of component in stages
variable bool bWarn = false; // first toolnumber reminder
option singleton yes; // change to no for multi turret machine, configured for single turret machine originally
function _;
author "OPEN AI CHATGPT and some guy with a broken lathe";
license "MIT";
;;
#include <unistd.h>
#include <time.h>
int i, d, pow;
int pcalc = 0;
int mod_pocket = 0;
int new_pos = 0;
unsigned int mask;
int align_pin = 0; // used for slow align move, input depends on mode for historical reasons
// BCD or Binary Coded Switch Decoding to determine turret position.
align_pin = strobe;
i = 0;
pow = 1;
pocket = 0;
while (i < 3) { // we only have 3 input bits: bcd1, bcd2, bcd3
int lim;
d = 0;
for (lim = i + 4; i < lim && i < 3; i++) {
d += (bcd1 & (1 << i)) + (bcd2 & (1 << i)) + (bcd3 & (1 << i)) << (i % 4);
pcalc ^= (bcd1 & (1 << i)) + (bcd2 & (1 << i)) + (bcd3 & (1 << i));
}
pocket += d * pow;
pow *= 10;
}
rtapi_print_msg(RTAPI_MSG_INFO, "Decoded pocket number: %d", pocket);
switch (progress_level)
{
case 1: // idle state waiting for tool change request
if (progress_level == 0)
{
if(!currenttoolnumber && !bWarn)
{
bWarn = true; // just warn once, its not an error as such but INFO won't display unless debugging is set 3+
rtapi_print_msg(RTAPI_MSG_ERR, "No tool selected. Use M6Tx to set current tool");
}
}
progress_level = 1;
break;
case 2: // check if toolnumber is within the bounds of the truth table
if (progress_level == 1)
{
if (toolnumber > 1 && toolnumber < 6)
{
progress_level = 2;
break;
}
else
{
rtapi_print_msg(RTAPI_MSG_ERR, "Invalid tool number. Use M6 Tx to select a tool between 1 and 6");
progress_level = 9;
break;
}
}
case 3:
if (progress_level == 2)
{
if (toolnumber == pocket) // If the toolnumber and pocket are the same, exit the program
{
toolchanged = true;
break;
}
else if (toolnumber != pocket) // If the toolnumber and pocket are different, proceed to the next case
{
progress_level = 3;
}
}
break;
case 4:
if (progress_level == 3)
{
if (toolnumber != pocket) // if requested toolnumber is different than truth table perform toolchange
{
// First tool change loop to run turret
unlock = true;
sleep(1); // Wait at least 1 second
motor = true;
// Set a timeout of 10 seconds
time_t start_time = time(NULL);
while (motor)
{
if (pocket == toolnumber)
{
motor = false;
}
if (time(NULL) - start_time > 10)
{
rtapi_print_msg(RTAPI_MSG_ERR, "Error: Motor loop timed out");
motor = false;
progress_level = 9;
break;
}
}
sleep(2); // Wait at least 2 seconds
if (strobe && pocket == toolnumber)
{
unlock = false;
}
else
{
rtapi_print_msg(RTAPI_MSG_ERR, "Error: Turret align failed");
progress_level = 9;
break;
}
}
}
progress_level = 4;
break;
case 5: // alert iocontrol loop that tool change is complete
if (progress_level == 4)
{
if (toolnumber == pocket);
if (turretlocked == true);
toolchanged = true; // signal finished
}
progress_level = 5;
break;
// should never get here but if we do then loop endlessly doing nothing
case 10:
if (progress_level == 9)
{
// rtapi_print_msg(RTAPI_MSG_ERR,
// "Error state in toolchanger - now disabled - unload toolchanger");
}
progress_level = 10;
break;
}
this is the current state of the code after working with chat gpt and learning my way around C more. It appears to have cribbed some of the code from carousel for decoding the bcd inputs, and I will ensure that the proper attributions are given afaict. I will simply put this under mit and allow for anyone who this may have inadvertently copied to put in open claims. Obviously some of this is my work, some is GPT's. It's almost working. maybe soon.adding error checking, fault states etc. and some debugging messages now to. I think the case switch statements are working properly now. haven't had a chance to test yet. I am probably going to end up rewriting the state machine with else if statements, the case logic seems to break constantly and the halcompile preprocessor seems to compile or compose it in such a way that it won\t execute reliably.
Last edit: 3 years 10 months ago by smc.collins.
Please Log in or Create an account to join the conversation.
- JPL
- Offline
- Platinum Member
-
Less
More
- Posts: 337
- Thank you received: 118
3 years 10 months ago #260492
by JPL
Replied by JPL on topic custom hal component failed to compile, undeclared (first use in this function)
Looks much better
Although this will not affect how the program works I would now suggest to do a little work on indentation. Have a look here for the 'Why' and 'How'
www2.cs.arizona.edu/~mccann/indent_c.html
Although this will not affect how the program works I would now suggest to do a little work on indentation. Have a look here for the 'Why' and 'How'
www2.cs.arizona.edu/~mccann/indent_c.html
Please Log in or Create an account to join the conversation.
Time to create page: 0.154 seconds