Troubles with multiple functions in a component
Thanks for your input. The coding style is.from.the.book" The C programming language, by Dennis Richie, the other book I reference is " C Through Design". I used their examples as my style, it compiles and it has worked in the past, so.....
Anyway, the first case is an event handler, there are several things this function does, it frees the spindle, runs it, shifts it and most importantly orients it. If I write the first 8 possible events with breaks, it will never fall through to.the other possibilities. Using a case statements with out a break is like just if- else over and over. I didnt make it up, it was explained the C design book.
The posted comp was basicly an experiment, a first try, I.didnt expect it to.work, but had to.start somewhere. I did rewrite it as seperate cases with breaks after the first 8 cases, and it works so far. I do think the event handler part could be better, and then might work with seperate functions.
Yes, one function was calling another, as you can run the spindle unless the transmission is.in the correct.gear, gears cannot be shifted uless the spindle is oriented and the spindle cant be run unless the the orient shot pin is out.
My first exprience programming a cnc had 16 k of memory!!! We wrote programs in subs to shorten the programs. It also allowed us to.change seperate subroutine instead of the main program.in the case of the inevitable mistake. Ive carried on that train of thought with plc programming, short blocks called by a main program.
I always appreciate everybodys input,thanks again.
JR
Please Log in or Create an account to join the conversation.
Anyway, the first case is an event handler, there are several things this function does, it frees the spindle, runs it, shifts it and most importantly orients it. If I write the first 8 possible events with breaks, it will never fall through to.the other possibilities. Using a case statements with out a break is like just if- else over and over. I didnt make it up, it was explained the C design book.
The programming style like that is fine for userspace programs, I often let case: statements fall through onto each other when they all do the same thing for instance.
The easiest way to get your head around the problem with kernel modules is to work out how many time per second the base thread polls.
Then assuming that your module is called each time, what can you do in that space of time?
The answer, next to f*** all, so whatever you do make it short and save where you were and return.
Good luck with it
Please Log in or Create an account to join the conversation.
The easiest way to get your head around the problem with kernel modules is to work out how many time per second the base thread polls.
I would rather expect this code to run in the servo thread, so there is a little more time available.
But, it is probably best if it runs approximately the same amount of code every iteration.
I am still a bit confused by the "else" and "if" of an "else if" being on separate lines.
Please Log in or Create an account to join the conversation.
I am still a bit confused by the "else" and "if" of an "else if" being on separate lines.
I think it may be a case of advances in compilers and the age of the references.
The C Programming Language was < 1988 (EDIT - appears to be 1978!) and The C++ programming language was <1991 ( I have later editions)
When I first learnt, else if was never used.
Instead constructs of nested if's inside else statements were
if()
{
......
}
else
{
if()
{
...
}
else
{
.....
}
}
else if makes it easier to read, although to be completely dogmatic an
if, else if, else if, else if sequence should be terminated by a catchall else
in the same way a switch() conditional test should always have a default: as the last case: to catch anything unexpected.
I would rather expect this code to run in the servo thread, so there is a little more time available.
You may well be right, it would have 10 times the thousandths of seconds on the base thread to complete
Brings it up to almost next to f*** all.
Please Log in or Create an account to join the conversation.
teeth to one is 1 tool before the target tool
teeth to count is the totalamount of teeth to get from where we are to where we are going
prs1 is the hall switch
case 16:
if((prs1_tooth_count)==1)
{
(sprocket_count)=(sprocket_count)+1; // set counter for teeth
tc_cond=17;
}
case 17: break;
if((sprocket_count)==(teeth_to_one)) //slow down for position one before
{
sl11_toolchain_hs=0;
tc_cond=18;
}
break;
case 18:
if((sprocket_count)==(teeth_to_count)) // stop the chain at the indicated pocket
{
sl12_tcplunger_out=0;
sl8_toolchain_up =0;
ect
My problem being, the counter isnt working , the program jumps from case 16 to case 18 and the tool chain just rotates as it hasnt met the count. Ive tried several iterations of this code with pretty much the same result. Im sure Im missing some elementry C programming technique, where did I blow it?If you need the whole file, let me know, I can post it, and thanks...
JR
Please Log in or Create an account to join the conversation.
{
(sprocket_count)=(sprocket_count)+1; // set counter for teeth
tc_cond=17;
}
case 17: break;
if((sprocket_count)==(teeth_to_one)) //slow down for position one before
{
sl11_toolchain_hs=0;
tc_cond=18;
}
break;
case 18:
My problem being, the counter isnt working , the program jumps from case 16 to case 18
I think the problem is the "break" at the start of case 17. Is that meant to be at the end of case 16?
Incidentally, you don't need brackets round variable names. So you could write
(sprocket_count)=(sprocket_count)+1
sprocket_count++
Please Log in or Create an account to join the conversation.
its actually
case 16:
if((prs1_tooth_count)==1)
{
(sprocket_count)=(sprocket_count)+1; // set counter for teeth
tc_cond=17;
}
break;
case 17:
if((sprocket_count)==(teeth_to_one)) //slow down for position one before
{
sl11_toolchain_hs=0;
tc_cond=18;
}
break;
sorry, im a bad typist, and thank you...
Please Log in or Create an account to join the conversation.
You only seem to increment the count in state 17, and you exit state 17 the first time the Hall sensor goes high.
You probably want something more like.
case 16: // waiting for Hall sensor
if (prs1){
sprocket_count ++;
tc_cond = 17;
if (sprocket_count == tooth_to_count){
sl12_tcplunger_out=0;
sl8_toolchain_up =0;
tc_cond = 18;
} else if (sprocket_count == (tooth_to_count - 1)){
sl11_toolchain_hs=0;
}
}
break;
case 17: // waiting for falling edge of Hall sensor
if ( ! prs1){
tc_cond = 16;
}
break;
Please Log in or Create an account to join the conversation.
JR
Please Log in or Create an account to join the conversation.