Writing a needet HAL Component

More
27 Aug 2019 00:51 - 27 Aug 2019 00:52 #143267 by rodw
Replied by rodw on topic Writing a needet HAL Component
Ok, the ; needs to go after the do{}while loop not on the first character of the next line

All lines in C need to be terminated with a ; sorry about that
		do{
			this_cycle++;
		}while(this_cycle < cycle);

Yes matching braces is important.

But I missed one on this line, so add it in
		if(!is_warping){

so put the last one back!

It will be pretty close then
Last edit: 27 Aug 2019 00:52 by rodw.
The following user(s) said Thank You: Emanresu

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

More
27 Aug 2019 07:10 - 27 Aug 2019 07:15 #143287 by Emanresu
There is no need to say sorry for anything^^
For me C, C# and C++ is some kind of black magic :)

Ive to go to work, so i just give a quick update:
Everything looks good, just no counting so far.

This is while Commpiling:
voxters@Vox-CNC:~/Arbeitsfläche$ sudo halcompile --install runner.comp
make KBUILD_EXTRA_SYMBOLS=/usr/realtime-3.4-9-rtai-686-pae/modules/linuxcnc/Module.symvers -C /usr/src/linux-headers-3.4-9-rtai-686-pae SUBDIRS=`pwd` CC=gcc V=0 modules
make[1]: Entering directory `/usr/src/linux-headers-3.4-9-rtai-686-pae'
  CC [M]  /tmp/tmpLn7hZ4/runner.o
runner.comp: In function '_':
runner.comp:50:9: warning: value computed is not used [-Wunused-value]
runner.comp:57:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /tmp/tmpLn7hZ4/runner.mod.o
  LD [M]  /tmp/tmpLn7hZ4/runner.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.4-9-rtai-686-pae'
cp runner.ko /usr/realtime-3.4-9-rtai-686-pae/modules/linuxcnc/
voxters@Vox-CNC:~/Arbeitsfläche$ 

The code.
description
"""
Example:

loadrt u32runner [count=N|names=name1[,name2...]]

runner.N.run bit in => run
runner.N.from u32 in => Number to start =>(just nice to have)
runner.N.to u32 in => Number to count to =>(if there is no "from" this can also be "THE" number)
runner.N.speed s32 in => count speed =>(must have - can also be an u32 if in ms)
runner.N.cycle bit in => repeat yes/no =>(must have)
runner.N.rev bit in => count backwards =>(just nice to have)
runner.N.warp bit in => one way or up and down =>(one way is need, up and down is nice to have)
runner.N.out u32 out => output =>(need of course)
""";

author "Rod Webster";

// Example Calibration Data: 0v = 122.9 kHz, 10v = 925.7 Khz should be entered as 122900 and 925700
pin in  bit run          			"count if true";
pin in  signed from                 "number to count from";
pin in  signed to                   "number to count to";
pin in  unsigned speed = 1          "number of cycles between counts (default = 1 = one servo thread cycle)";
pin in  bit cycle = 1               "if true, start again or stop";
pin in  bit warp                    "if true, count up and then back down";
pin out unsigned out                "the number to output";


function _;
license "GPL";
;;
#include <rtapi_math.h>


FUNCTION(_) {
	static int first_pass = 1;
	static int this_cycle = 0;
	static int step = 1;
	static int counter = 0;
	static int is_warping = 0;
	
	if(first_pass){
		//set up
		counter = from;
		first_pass = 0;
	}
	if(cycle < 1){
		cycle == 1;
	}
	if(run){
		do{
			this_cycle++;
		}while(this_cycle < cycle);
		this_cycle = 0;
		if(!is_warping)
			if(from > to)		// Set the direction to count 
				step = -1;
			else
				step = 1;
			counter += step;
			if(counter == to){	// we got to the destination
				counter = to;	// stop counting
				if(warp){
					is_warping = 1;{
					if(step > 0)
						step = -1;
					else
						step = 0;
				}
			}
		}
		else{
			counter += step;
			if(counter == to){
				is_warping = 0;
					if(step > 0)
						step = -1;
					else
						step = 0;				
			}
		}
	}
}
Last edit: 27 Aug 2019 07:15 by Emanresu.

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

More
27 Aug 2019 07:29 - 27 Aug 2019 07:37 #143289 by Emanresu
Off Topic

Just noticed you live in one of the most beautiful countries in the world.
I was working on going to Australia since 5 years with my CNC-Shop.
I also had different sponsorships to start with.
But each time the GOV told my sponsors they have to look 3month in Australia
bevore they can take me.

Now i try New Zealand^^

No idea why, just had to mention it. Maybe just in case if you wona live in Germany
and switch Passports :-P
Last edit: 27 Aug 2019 07:37 by Emanresu.

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

More
27 Aug 2019 08:13 #143293 by rodw
Replied by rodw on topic Writing a needet HAL Component
Well it looks like I got your logic wrong.
Try changing to this
		do{
			this_cycle++;
		}while(this_cycle < speed);

I was counting to the wrong variable

I think you need to reconsider the pins cycle and warp because the way I see it it would be much easier to have one pin where:
0 = count once and stop
1 = count from from to to and repeat from the beginning
3 = count from from to too and back to from again and repeat. (warp)

That would be much easier to code as you could use a switch statement

Thanks for your interest in Australia. Clive S is coming to meet me on a holiday later in the year so there is hope for you yet. New Zealand is also an amazing country.
The following user(s) said Thank You: Emanresu

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

More
27 Aug 2019 08:58 #143295 by Emanresu
Awesome!

Cant wait to get home and test it!

In fact a single input with more variables was my first idea.
I just thought this is MORE complex and harder to realize.
(As you see... zero skills in coding :) )
So if this is easyer, just go ahead!

And a question:
Im free to change "signed" and "unsigned" for the
inputs / output or will it interrupt the code?
Atleast i get no message while compiling.

That way i dont need a converter in HAL.

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

More
27 Aug 2019 10:14 #143306 by rodw
Replied by rodw on topic Writing a needet HAL Component
signed and unsigned make no difference really. They are the same size. They are much bigger numbers now but in the 8 bit days signed went from -32767 to +32767 and unsigned went from 0-65535

I can't guarantee I will get time to finish it off for you
The following user(s) said Thank You: Emanresu

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

More
27 Aug 2019 14:26 #143326 by Emanresu
Perfeckt!

Thats an awesome information for further projects and give some new options!

Dont worry, if you dont have the time.
You allready helped me way more then i was thinking off when i started this Topic.

Now i know, its possible, what to look for and i even have a base to work on!
The following user(s) said Thank You: rodw

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

Time to create page: 0.108 seconds
Powered by Kunena Forum