My Raspberry crashes

0

I'm trying to make an omnidirectional robot in Python, I used to use PWM in order to control the dc motor's speed but I thought it caused many troubles, so I erased it and this is my "final" program (some parts are commented because I've been testing the program), when I execute it for the first time, my program works well, once I type the up arrow key it crashes and reboot the Raspberry, I'm using encoders in order to know the motors' position and I'm reading them with Interrupts.

If I execute one by one of the motors then the program works very well, I mean, I comment every part of the others motors and I try it over and over again; but once I "assemble" all my program, at the very first moment I type the arrow key the program crashes down and reboot my Raspberry, so I don't think it's a matter of connections (short circuit or whatever).

import RPi.GPIO as GPIO
import os
import time

##Encoders Counters
contaM1 = 0
##contaM2 = 0
contaM3 = 0
##contaM4 = 0

##GPIO's Encoders
encA1 = 31
encB1 = 32
##encA2 = 33
##encB2 = 35
encA3 = 36
encB3 = 37
##encA4 = 38
##encB4 = 40

##GPIO's to motors control
mot1F = 13#F -> Forward
mot1B = 11#B -> Backward
mot2F = 7
mot2B = 12
mot3F = 16
mot3B = 15
mot4F = 18
mot4B = 22

##setup
GPIO.setmode(GPIO.BOARD)

##Setup Encoders
GPIO.setup(encA1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(encB1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encA2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encB2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(encA3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(encB3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encA4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encB4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encA4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
##GPIO.setup(encB4, GPIO.IN, pull_up_down = GPIO.PUD_UP)

##Setup Motors
GPIO.setup(mot1F, GPIO.OUT)
GPIO.setup(mot1B, GPIO.OUT)
GPIO.setup(mot2F, GPIO.OUT)
GPIO.setup(mot2B, GPIO.OUT)
GPIO.setup(mot3F, GPIO.OUT)
GPIO.setup(mot3B, GPIO.OUT)
GPIO.setup(mot4F, GPIO.OUT)
GPIO.setup(mot4B, GPIO.OUT)

##Imprime en Consola contador
def Print_enc():
    print('Count M1:', contaM1)
##    print('Count M2:', contaM2)
    print('Count M3:', contaM3)
##    print('Count M4:', contaM4)

##callback
def CountM1(channel):
    global contaM1
    contaM1 += 1
    os.system("clear")
    Print_enc()

##def CountM2(channel):
##    global contaM2
##    contaM2 += 1
##    os.system('clear')
##    Print_enc()

def CountM3(channel):
    global contaM3
    contaM3 += 1
    os.system('clear')
    Print_enc()

##def CountM4(channel):
##    global contaM4
##    contaM4 += 1
##    os.system('clear')
##    Print_enc()

def fwd():
    GPIO.output(mot1F, True)
    GPIO.output(mot1B, False)
    GPIO.output(mot2F, True)
    GPIO.output(mot2B, False)
    GPIO.output(mot3F, True)
    GPIO.output(mot3B, False)
    GPIO.output(mot4F, True)
    GPIO.output(mot4B, False)

def Off():
    GPIO.output(mot1F, False)
    GPIO.output(mot1B, False)
    GPIO.output(mot2F, False)
    GPIO.output(mot2B, False)
    GPIO.output(mot3F, False)
    GPIO.output(mot3B, False)
    GPIO.output(mot4F, False)
    GPIO.output(mot4B, False)

Off()

##Interrupts
GPIO.add_event_detect(encA1, GPIO.BOTH, callback = CountM1, bouncetime = 200)
GPIO.add_event_detect(encB1, GPIO.BOTH, callback = CountM1, bouncetime = 200)
##GPIO.add_event_detect(encA2, GPIO.BOTH, callback = CountM2, bouncetime = 200)
##GPIO.add_event_detect(encB2, GPIO.BOTH, callback = CountM2, bouncetime = 200)
GPIO.add_event_detect(encA3, GPIO.BOTH, callback = CountM3, bouncetime = 200)
GPIO.add_event_detect(encB3, GPIO.BOTH, callback = CountM3, bouncetime = 200)
##GPIO.add_event_detect(encA4, GPIO.BOTH, callback = CountM4, bouncetime = 200)
##GPIO.add_event_detect(encB4, GPIO.BOTH, callback = CountM4, bouncetime = 200)
##GPIO.add_event_detect(encA4, GPIO.BOTH, callback = CountM4, bouncetime = 200)
##GPIO.add_event_detect(encB4, GPIO.BOTH, callback = CountM4, bouncetime = 200)

Print_enc()

x = 14

try:

    while 1:
        control = input()
        contaM1 = 0
##        contaM2 = 0
        contaM3 = 0
##        contaM4 = 0

        if control == '\x1b[A':
            while contaM1 <= x:
                adelante()
                Print_enc()
            apagado()
            contaM1 = 0
##            contaM2 = 0
            contaM3 = 0
##            contaM4 = 0
            time.sleep(0.2)
            Print_enc()

        else:
            print('Try again')

except KeyboardInterrupt:
    pass

I don't know what to do.

Please HELP!!!

t0ny1402

Posted 2019-02-07T03:02:08.043

Reputation: 1

Are you sure it's your program crashing, and not the raspberry itself? How much power do all the motors use, together, under full load (stuck?), and can the power supply handle it? Or does the raspberry use relays for the motors, and has it's own dedicated power supply that's unaffected even if the motor's power fails? – Xen2050 – 2019-02-07T04:02:09.200

2

You can get more help here https://raspberrypi.stackexchange.com/.

– Biswapriyo – 2019-02-07T04:15:29.310

@Xen2050 I'm using a LiPo battery (3500mAh-11.1V) for the Raspberry and the motors; there's a parallel connection from the battery, one goes to an L298N driver to control the direction in which the motor rotates, and the other connection goes to a BEC voltage regulator (6V-23V input. 3A-5V output) and then it goes to the Raspberry.

I have two batteries, so when I'm using one, the other one is charging and I always use the full battery. – t0ny1402 – 2019-02-11T17:40:28.427

Do the motors all start to move at the same time when it crashes, or even two motors? Tested it with 2,3... all of them together? I'm not sure if that battery could power everything (not sure how much power a motor uses), but it's an idea to investigate if the crashing seems to line up with the motors. If you could monitor the battery's voltage that might help too – Xen2050 – 2019-02-12T11:34:18.563

No answers