position feedback sanity check

More
19 Oct 2022 02:44 #254488 by cychu18
position feedback sanity check was created by cychu18
I struggled with the feedback for a few days. I am very new to linuxcnc, and I am not sure the way I am doing my position feedback is proper. My current problem is that the following error is huge because there are errors in the sensor output while the motor is not moving, but before I explain what I do and how this error is, I think it is important for you, my saviors, to know the actual problem I am trying to solve to the implement a distance sensor that shows the absolute position of the axes. Now the context is out, I think we can avoid any potential XY problem

The way I am doing right now, ( i am not sure this is the way I should implement what I want):
I have a distance sensor, with 4-20mA output (it is an old spare sensor I found in the lab). I built a circuit with a series of 250 ohms, so I can produce up to 5V. and has a high impedance to the input of an ADC (ADS1158_EVM board). The sensor output gives a good enough voltage across the resistor using a multimeter, but some noise fluctuation on the scope (so a high-level filter should work). Since the laser sensor will represent the absolute distance of the moving part against the fixed frame of the gantry, I want this value to be the position value of the axis (currently I have it hooked up to Y).  I can successful communication via SPI using the py-spidev via a python component, and i am using linuxcnc on Rpi 4. Then, I net the output of the sensor in float to the actual motor position (this is how encoder from mesa board is feeding back to the linuxcnc, I follow their logic to change mine, just replace the encoder output with the distance sensor output in float). The result is that the UI does update the Y axis position with the sensor position. see screencap picture

I attached the full hal file, and the section where I make the changes are here. 
# load the hal-pi-gpio modules
# 67108863, older
# 67107903, gpio doesn't use SPI pins, these are for userspace
# thus xlucde is 960
loadrt hal_pi_gpio dir=67107903 exclude=960

loadusr -W /home/pi/ADS1158_SPIhal.py
.
.
.
#net ypos-fb stepgen.1.position-fb => joint.1.motor-pos-fb
#net ADS1158_SPIhal.out => joint.1.pos-fb
net ypos-fb ADS1158_SPIhal.out => joint.1.motor-pos-fb

Yeah, i sort of hijack the open loop feedback with the sensor output

the way I load the python component is in userspace, because I can't find any documentation to load it in rt space and I couldn't see loadrt-pycomp-error picture. 

The issue I think I am having:
the Following error keeps linuxcnc refusing to turn on. I suspect userspace is inherently slowing than rt space, and the motor-pos-fd is an rt component, and by the time it is updated from the userspace, the error is too large (pos-cmd = stepgen-pos-fd >> motor-pos-fd)

Currently I am using DIR/PUL command to control the stepper. 

AGAIN, I am not sure the way I am doing right now is the most appropriate. And these are the resource I have right now. I don't have a mesa board, and I am just retrofitting with the component that my lab has lying around from old spare parts. Since I can get the sensor to throw a feedback to the actual motor position. If I can just tell linuxcnc to ignore the difference between stepgen and the motor-pos, and let the motor follow the sensor position, that will be the goal of my method. 

If there is a better to-do position feedback where the motor will use the sensor feedback as the absolute position without causing Ferror, I will be a happy man.


 
Attachments:

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

More
19 Oct 2022 03:40 #254490 by PCW
Replied by PCW on topic position feedback sanity check
You can set the following error limits to huge numbers to see if you
at least have the feedback working (not backwards for example)

Other issues:

The non-real time nature of the Python SPI interface will likely prevent good performance of the feedback loop.

You probably need to use a PID component and run the stepgens in velocity mode
to have anything controllable/tunable.

You don't actually need DDT for joint velocity and acceleration as LinuxCNC provides these.

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

More
19 Oct 2022 05:03 #254493 by cychu18
Replied by cychu18 on topic position feedback sanity check
action: increase the Ferror to 500 and Min_Ferror to 100.
result: linuxcnc allows me to turn it on, but the movement goes back and forward, and that is when the motion drops out of range again. since the sensor is always a distance from the reference, it seem like the printer are keep moving forward. seem like it is trying to move to that position. i believe sensor position => pos-fd, actually tell the pos-cmd to move to that position instead of we are that much distance away from 0. which is not really what i want, I want the sensor just tell me the position, and when i homed, it is like an offset from the home.

Regarding another issue:
QUOTE" The non-real time nature of the Python SPI interface will likely prevent good performance of the feedback loop."

I don't see another SPI comp available. I already have the ADS1158 register figured out I just need a generic full duplex read and write function, ideally, an existing component that I can load to the rt-thread in the hal file.

Plus, I only need it to be within 1-2mm accuracy,

AGAIN, I am not an expert in this enough to stick to my method, if there is a better to integrate the laser distance sensor (4-20mA) via ADC SPI as the absolute position feedback for the axis, I am all ears.

QUOTE" You probably need to use a PID component and run the stepgens in velocity mode"
an example resource would be nice. I was under the impression that the cmd we send thru the GUI for example Y1000, the PUL and DIR will keep active under the pos-fd reach 1000?

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

More
19 Oct 2022 14:47 #254531 by PCW
Replied by PCW on topic position feedback sanity check
Are you sure the feedback direction is correct?
(not reversed so you have positive feedback=runaway)

Without PID and relying on the stepgens built-in un-tunable
control loop will also lead to instability, it is not designed
to deal with lags between command and effect.

You can use a pncconf created hal file as an example of stepgen
PID setup (though you will need much lower P term than its default)

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

More
20 Oct 2022 05:22 #254586 by cychu18
Replied by cychu18 on topic position feedback sanity check
just an update: i am not even sure what is going on

Yesterday it runwaway, because I accidental change from stepgen-pos-cmd to stepgen-vel-cmd without realise it was is in the pos-cmd, so it always run off. I fixed that and the results is that I have a open-loop stepper with sensor as the report of the distance. see picture, open-loop. (i always want to ask how do I embed picture in the reply?)

Now, the system won't move and it is controlled by velocity. but now it has two issues:

1) the sensor reading drop off a lot even when the op-amp and sensor output signal is constant. MY SPECULATION is that spidev is too slow, and linuxcnc rt component just took away the processing and cause the input reading to be weird. Despite the fact that both the analog voltage and ADS is transferred the right signal (observe from the scope). so everything is working just linuxcnc is not reading the ADC output correctly. I am sure that it is linuxcnc not loading the input properly, net ypos-fb sensor.out -> motor-pos-fb, becuase i can run the python code for hours without a single incorrect reading. 

The second thing is, with open loop control. there are no following error, but I am not sure how to tell if I am moving it accurately, since i got the Ferror so huge at 500, I guess I would have to move an assumed fixed distance (Y2000), and check if the sensor feedback increase by 2000? 

Then I tried to do the PID control,.see code, XYZA-7-fb.hal and ini
i couldn't do much most of the time since it still have incorrect reading problem. and for the short time it works, the movement seem .....exotic. I guess tuning PID will take a while. But I believe open loop work reasonbly well after I calibrated it. 

Man, I really need a beer after this. 

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

More
20 Oct 2022 06:59 #254588 by cychu18
Replied by cychu18 on topic position feedback sanity check
ok, i know that we loadrt stepgen and pwmgen, and in the component folder of the code, they are is .c file
meanwhile, there are other components for example a not and sum2, they are in .comp

they are written quite differently. . I also notice that in the .c file, there is a rtapi_main function, i believe this function run when the component is called, this is perfect for the setup if I want to configure the sensor register when I load the component in the hal code. However, I don't see many .comp do that. meanwhile, .comp doesn't seem to be loading many libraries, Is there any comp that use function from another libaray?

Is it possible to also have the main function for .comp, so I can run some functions only once?

and what are the major different between writing in .c and .comp directly? It seem like there are two way to make comp and strange to find that most basic function are writing in .comp and the more complex in .c

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

More
20 Oct 2022 08:06 #254590 by arvidb
The following user(s) said Thank You: cychu18

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

More
20 Oct 2022 11:28 - 20 Oct 2022 11:30 #254596 by cychu18
Replied by cychu18 on topic position feedback sanity check
Thanks!, this is mind blowmind, this has way better documentation than the one i was reading, sometime when you search on google they give you link that are years ago.

Anyway, Here quoted"
"option userspace yes - (default: no) If specified, this file describes a userspace (ie, non-realtime) component, rather than a regular (ie, realtime) one. A userspace component may not have functions defined by the function directive. Instead, after all the instances are constructed, the C function void user_mainloop(void); is called. When this function returns, the component exits. Typically, user_mainloop() will use FOR_ALL_INSTS() to perform the update action for each instance, then sleep for a short time. Another common action in user_mainloop() may be to call the event handler loop of a GUI toolkit."

this make me think that userspace shouldn't cause the SPI communication drop out I experience. Now, I can't even understand how my dropout occur!


i will type out the full code in a few hours. I am thinking

component ADS1158_SPI;
option userspace;
option userinit //i believe this option allow me to have a run once routine at the beginning of the code
pin out float out;
license "GPL"; // indicates GPL v2 or later
;;
#include <pigpio.h> // i foudn this llibrary allow me to use SPI in C code on Rpi


void user_mainloop(void) {
while(1) {
/* sudo code
SPI open
get data from SPI 32 bits long for 16 bits ADC data
SPI close
feed raw 16bits data to moving medium
feed again to moving average
convert moving average output from count to (float) mm

out = float_mm;
*/


}
}

void userinit(argc,argv){

SPI open
SPI Xfer 5 bytes of configuration
SPi close

}

let me know if this sudo code make sense. i will work on the actual code now
Last edit: 20 Oct 2022 11:30 by cychu18.

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

More
20 Oct 2022 16:34 #254616 by cychu18
Replied by cychu18 on topic position feedback sanity check
Sharing my drafted code, hope it makes sane. 

I am not sure I am using FOR_ALL_INSTS(), and void userinit(int argc, char *argv){} function properly in userspace?

another point to consider is, that I refresh some variables inside the while loop, i did that out of habit for microcontroller, because they will have a very little allocation to static memory/global variables and such. Not sure if this is the best case here. Since each loop, the program will create and destroy this memory volume at the lower level.  

also, any suggestion for better coding is welcome, i am thinking of developing this so we can use more ICs with SPI interface, like a generic ones with different sizing of register.

anyway, i will test the code on the machine after a short sleep. sometimes I just hate my work. 
Attachments:

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

More
20 Oct 2022 17:01 #254618 by arvidb
Replied by arvidb on topic position feedback sanity check

Another point to consider is, that I refresh some variables inside the while loop, i did that out of habit for microcontroller, because they will have a very little allocation to static memory/global variables and such. Not sure if this is the best case here. Since each loop, the program will create and destroy this memory volume at the lower level.


I would say it's generally a good thing to reduce the scope of variables as much as possible - i.e. don't declare them until you need them. This makes the code easier to read. (Compare the other extreme of using only global variables declared at the head of the file: it makes understanding how the program works very difficult! This is BTW pretty much how LinuxCNC's core code is written, unfortunately, which makes it so hard to maintain.)

The compiler should be smart enough to handle allocation of those variables in a sane way, so don't worry about that. (They are stack variables so no "allocation" to do really. Memory fragmentation only becomes an issue with heap allocations.)

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

Time to create page: 0.095 seconds
Powered by Kunena Forum