# Sudden raspbery pi power cut or random long delay when running stepper motor using pigpiod

So I wrote another program that can run stepper motor nema 34 using leadshine DM860 driver and this is the example of my code:

import time
import pigpio

START_DELAY=580
FINAL_DELAY=480
STEP=1

GPIO=20

pi = pigpio.pi()

pi.set_mode(GPIO, pigpio.OUTPUT)
pi.set_mode(21, pigpio.OUTPUT)
pi.set_mode(26,pigpio.INPUT)
pi.set_mode(16,pigpio.INPUT)
#pi.write(21,1)
pi.wave_clear()

statee = 0
run = True
try:
while run == True:

pi.write(21,statee)
pi.wave_clear()

wf=[]
offset = pi.wave_get_micros()
for delay in range(START_DELAY, FINAL_DELAY, -STEP):
wf.append(pigpio.pulse(1<<GPIO, 0,       delay))
wf.append(pigpio.pulse(0,       1<<GPIO, delay))

for i in range(500):
wf.append(pigpio.pulse(1<<GPIO, 0,       FINAL_DELAY))
wf.append(pigpio.pulse(0,       1<<GPIO, FINAL_DELAY))

for delay in range(FINAL_DELAY, START_DELAY, STEP):
wf.append(pigpio.pulse(1<<GPIO, 0,       delay))
wf.append(pigpio.pulse(0,       1<<GPIO, delay))

pi.wave_add_generic(wf)

wid2 = pi.wave_create()

#pi.wave_send_once(wid2)
pi.wave_send_using_mode(wid2, pigpio.WAVE_MODE_ONE_SHOT_SYNC)

if pi.read(26) == 0:
pi.wave_tx_stop()
run = False
if pi.read(16) == 0:
pi.wave_tx_stop()
run = False

time.sleep(0.7429)
if statee == 0:
statee = 1
elif statee == 1:
statee = 0
except KeyboardInterrupt:
print ("\nCtrl-C pressed.  Stopping PIGPIO and exiting...")
pi.wave_tx_stop()
pi.stop()


It runs without code error but sometimes the stepper motor when moving got random long delay even I already decrease the value from START_DELAY,FINAL_DELAY, and time.sleep() (with the range between START_DELAY and FINAL_DELAY value is 100 and time.sleep() value depend how smooth movement).

When I use START_DELAY,FINAL_DELAY, and time.sleep() value like the example code (the lowers speed) sometime the raspberry pi got power cut (stepper motor stop moving and lcd monitor show blank screen with text 'no signal'). So, is the problem realy cause from my code? I already update pigpiod to v68.

This is how it is wired:

I use jumper wires (using male to male and female to female, because I dont have male to female jumper wires) between driver to raspberry pi 3 and connection between DIR- and PUL- using ordinary copper wire.

This is the pulse width modulation result that I check using piscope:

NEMA34 bipolar stepper motor specification:

1. Holding torque 5.9 Nm
2. Step angle 1.8°
3. Resistance / phase 0.33±10% Ω
4. Inductance / phase 3.00±20% mH
5. Max load axial 65 N
6. Max load radial 200 N

https://www.igus.com/info/drive-technology-nema-34-ca

DM860 leadshine with an output current of 1.0 - 7.2A , and an input voltage of 24 - 80 VDC. I have the driver configured with 24V power supply.

This is the driver current and pulse/rev setting:

• It sounds like you may have a power problem perhaps in addition to others. How is the pi powered? How are the motors powered? – Chris Stratton Dec 5 '18 at 18:12
• @ChrisStratton I poweed the stepper motor using power supply 24VDC 10A. Raspberry Pi powered by 5V 2A charger adapter – Jan sebastian Dec 5 '18 at 18:18
• Edit the question to show how everything is wired, both logically and physically (routing of wires, size, length, etc). Specify the stepper current setpoints. Leave a serial console or monitor running tailing /dev/kmesg. Modify you code to produce periodic output there, also have an independent daemon which produces periodic output. – Chris Stratton Dec 5 '18 at 18:19
• @ChrisStratton I already add some detail, is that enough? – Jan sebastian Dec 6 '18 at 17:43
• A 200 ms pause in usermode code is probably just the system being busy. But you described a "shutdown" which would be something else. You didn't really supply any of the requested information. – Chris Stratton Dec 6 '18 at 17:51

## 1 Answer

I think you need to stop for a moment, and do some reading about the pigpio library.

One of your last questions involved misusing one of the simplest pigpio methods (pi.stop)

This looks to be much more complicated, and you really need to understand what is going on.

From a quick look at the documentation and some random examples on the internet, it appears to me that you need to be more careful about when you change or add waves to the queue. Changing things at the wrong time will cause lost or skipped waves.

In general, your code could be improved.

1. Get more familiar with how the wave functions work and how (and when) you can change (or add or delete) waves from the queue.

2. You are generating the waves on every run through the loop. The parameters to the wave generation don't seem to change, so you are continuously generating the same patterns. Make them once outside the loop and reuse them.

3. You seem to be sending the same sequence over and over, and stopping when some external event (pins 16 and 26) happens. You can set a wave to repeat until you stop it. that would drastically simplify your code. Look into "wave_send_repeat" and "WAVE_MODE_REPEAT."

4. Your pins are unnamed. If you used a name like "Stop_Button" instead of "26" in your "pi.read" then it would be clearer what you are actually doing.

5. In general you should assign your constants to variables with good names.

Doing the repeats in code as you do leaves you open to delays in execution. Python runs in user space and can be interrupted by pretty much anything. This can cause delays in starting the next wave.

Pigpio itself runs in kernel space, and can only be interrupted by high priority kernel tasks - it is much likelier to run properly than anything in user space. The way it is written (using programmed DMA) makes it very likely that even the kernel won't bother it. This makes it really the best way to do repeats and long sequences.

You are reversing on each repeat. That was obscured by the lack of good names in your code.

You need to figure out how to ask pigpio when a particular wave is finished. You send it your wave, wait until it is complete (or cancel it and wait until complete) then send the next command.

If you flip the reverese and the servo is still moving, then you might get voltage spikes on the ground connecting the pi and the servo controller.

The DM860 appears to have optically isolated inputs, so unless you have the grounds tied together you shouldn't be getting any spikes from the DM860 to the pi.

Are you sharing ground or power between the pi and the motor/motor controller?