User avatar
HermannSW
Posts: 6256
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

new pigpio-stepper-motor fork

Fri Jul 10, 2020 7:03 am

I used stripcode's pigpio-stepper-motor library sofar to control three stepper motors for xyz direction of my Pi microscope. A single step in x or y direction does move view by 5µm, a single step in z direction by 4.4µm:
viewtopic.php?f=43&t=210605&p=1693060#p1693060

The library uses a deque internally for the 4(8) steps done with full(half) stepping, and always starts at position 0. When starting the lib no movement happens, but the end position of stepper motor wrt sequence might be any in 0..7 mod 8 for half-stepping. So the first step will do 1..8 steps in the direction requested for first step, from then on all is fine. But 8 steps does mean 7 steps off, or 35µm. This was not acceptable for my microscope application.

So I did a fork of stripcode's repo:
https://github.com/Hermann-SW/pigpio-st ... r#examples

New example keys_xyz.py does move the three steppers with LEFT/RIGHT/UP/DOWN/NPAGE/PPAGE keys, and ends script when pressing END key. In addition it does output the X/Y/Z coordinate at start and after each step of any of the stepper motors. That way the last position is known. When starting keys_xyz.py with passing last X/Y/Z coordinates as argv[1]..[argv[3], the example makes use of new pigpio-stepper-motor last default arg pos for each motor to rotate the deque to the correct starting position (mod len(sequence)). This way newly starting keys_xyz.py keeps at exactly the position it should in all 3 dimensions.

This is screenshot of open source libcamera qcam app, with "only" 1.05µm/pixel resolution. The centers of micrometer divisons at top are 10µm apart. Using raspistill "--roi" feature resolution is 0.21µm/pixel, centers of micrometer divisions are 48pixel apert then(!). At bottom right you see lanzet ball point:
keys_xyz.png.jpg
keys_xyz.png.jpg
keys_xyz.png.jpg (49.39 KiB) Viewed 1695 times
https://github.com/Hermann-SW/RSA_numbers_factored
https://stamm-wilbrandt.de/GS [304+402+536fps]
https://hermann-sw.github.io/planar_graph_playground
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de

PiGraham
Posts: 5383
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: new pigpio-stepper-motor fork

Fri Jul 10, 2020 7:41 am

Wouldnt it be best to home all your axes at the start so you always start from the same position?

User avatar
neilgl
Posts: 7574
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: new pigpio-stepper-motor fork

Fri Jul 10, 2020 8:16 am

That is looking great!

User avatar
HermannSW
Posts: 6256
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: new pigpio-stepper-motor fork

Fri Jul 10, 2020 10:03 am

PiGraham wrote:
Fri Jul 10, 2020 7:41 am
Wouldnt it be best to home all your axes at the start so you always start from the same position?
After having run keys_xyz.py at least one time, I know the X/Y/Z positions of the three steppers, and can start with them next time.
But before starting keys_xyz.py the first time it is impossible to know what the last motor positions were in 0..7 range for half-stepping.
stripcode's library did home all three steppers at position 0, but that might move first step in each direction 35µm too wide.

In case someone did move the steppers even minimally, the last positions would not help.
But I stopped microscope at night at some position, and next morning typically all is unchanged.
https://github.com/Hermann-SW/RSA_numbers_factored
https://stamm-wilbrandt.de/GS [304+402+536fps]
https://hermann-sw.github.io/planar_graph_playground
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de

PiGraham
Posts: 5383
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: new pigpio-stepper-motor fork

Fri Jul 10, 2020 11:40 am

HermannSW wrote:
Fri Jul 10, 2020 10:03 am

After having run keys_xyz.py at least one time, I know the X/Y/Z positions of the three steppers, and can start with them next time.
True. I'm sure it works.
I was just thinking how it is common for motion control systems to home to a known position at startup. E.g. 3D printers, CNC mills etc.

User avatar
HermannSW
Posts: 6256
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: new pigpio-stepper-motor fork

Sun Jul 12, 2020 6:38 pm

I am working on microstepping, and things become not that easy anymore.
I did change PigpioStepperMotor.py to work with PWM already:

Code: Select all

   def doStepAndDelay(self, step):
-    self.pi.write(self.pin1, step[0])
-    self.pi.write(self.pin2, step[1])
-    self.pi.write(self.pin3, step[2])
-    self.pi.write(self.pin4, step[3])
-    sleep(self.delayAfterStep)
\ No newline at end of file
+    self.pi.set_PWM_dutycycle(self.pin1, 255*step[0])
+    self.pi.set_PWM_dutycycle(self.pin2, 255*step[1])
+    self.pi.set_PWM_dutycycle(self.pin3, 255*step[2])
+    self.pi.set_PWM_dutycycle(self.pin4, 255*step[3])
+    sleep(self.delayAfterStep)

But as I said, things are not easy with microstepping.
So I created a small standalone python script that queries angle in degrees, and then moves single (x dimension) stepper motor to that angle:

Code: Select all

$ cat micro.py 
import pigpio
import math

m = (5, 11, 10, 9)
M = 250

def pos(x):
    return x if x >= 0 else 0

pi = pigpio.pi()

for i in range(len(m)):
    pi.set_PWM_frequency(m[i], 8000)
    pi.set_PWM_range(m[i], M)

str = raw_input()
while str != "q":
    a = float(str)/180*math.pi

    pi.set_PWM_dutycycle(m[0], M*pos(math.cos(a)))
    pi.set_PWM_dutycycle(m[1], M*pos(math.sin(a)))
    pi.set_PWM_dutycycle(m[2], M*pos(math.cos(a+math.pi)))
    pi.set_PWM_dutycycle(m[3], M*pos(math.sin(a+math.pi)))

    str = raw_input()

pi.stop()
$

The biggest magnification of Pi microscope I used sofar was raspistill with "--roi 0.375,0.375,0.25,0.25" for 12MP mode3 frame. That did display center pixels from 12MP frame 1:1 on display (preview "-p 4,4,1014,760"). That way centers of vertical micrometer divisions are 10µm or 48 pixel apart. Now that single digit micrometer movements need to be evaluated, I magnified view by factor 5. So one pixel from 12MP frame is displayed as 5x5 recangle ond display. Centers of vertical micrometer divisions are 240 pixels apart by this. I took raspi2png snapshots after each move to an angle (raspi2png captures raspistill preview HDMI overlay as well), and created a simple animation from that. Have a look at the big white point at bottom, that allows to understand the movements to the angles entered in right bottom window. Maybe script micro.py is only useful for me with the microscope being able to "see" the moves. Posting it here just in case it might be useful for others as well:
Image
https://github.com/Hermann-SW/RSA_numbers_factored
https://stamm-wilbrandt.de/GS [304+402+536fps]
https://hermann-sw.github.io/planar_graph_playground
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de

Return to “Python”