Troubles with multiple functions in a component

More
08 Jan 2014 03:40 #42455 by JR1050
Arceye,
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.

More
08 Jan 2014 16:52 #42482 by ArcEye

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.

More
08 Jan 2014 18:20 #42485 by andypugh

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.

More
08 Jan 2014 19:44 - 08 Jan 2014 19:58 #42493 by ArcEye

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. :laugh:
Last edit: 08 Jan 2014 19:58 by ArcEye.

Please Log in or Create an account to join the conversation.

More
19 Jan 2014 02:59 #42981 by JR1050
At the risk of stirring the pot.....Im stuck at a counter in my toolchanger program. Ill try to make this breif. I have 25 tools on a chain that is driven by a 50 tooth sprocket. Each tool station takes 2 teeth to advance or retract to the next tool. Im subracting the the tool number called by M6 from the present postion and multiplying that by 2 to get the amount of teeth on the sprocket to the new tool number( if the number comes out negative, it is multiplied by -1). I have to slow down the tool chain 1 tool before my target tool so i dont miss it.The teeth are counted by a hall switch. I have this

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.

More
19 Jan 2014 03:15 #42982 by andypugh

{
(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
as
sprocket_count++

Please Log in or Create an account to join the conversation.

More
19 Jan 2014 03:36 #42983 by JR1050
Break at the top of case 17 is a typo in the message..

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.

More
19 Jan 2014 03:55 - 19 Jan 2014 03:58 #42984 by andypugh
I think you might have too many states :-)

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;
Last edit: 19 Jan 2014 03:58 by andypugh.
The following user(s) said Thank You: JR1050

Please Log in or Create an account to join the conversation.

More
19 Jan 2014 07:22 #43004 by JR1050
That snippit works, thank you!!! I had a version close, but I hadnt considered waiting for the hall switch to go low, thank you again!!

JR

Please Log in or Create an account to join the conversation.

Time to create page: 0.105 seconds
Powered by Kunena Forum