papous
Posts: 80
Joined: Fri Jan 05, 2018 5:50 am

Raspberry LX terminal odd behaviour

Sat Jan 13, 2018 7:43 pm

Below there is a fairly simple module to move the two engines that came with Camjam edukit#3.
When run from the Thonny platform it works ok.
When run fron thw LX terminal or IDLE the engines run erratically, stopping and turning back and forward for about 5 secbefore they resume a (almost) normal operation.
Does anyone have any idea why this happens? It is ruining my project.
Thank you

Code: Select all

# CamJam EduKit 3 - Robotics # Worksheet 4 – Driving and Turning
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!  ")

#import RPi.GPIO as GPIO # Import the GPIO Library
from time import sleep # Import the Time library

# Set the GPIO modes
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Set variables for the GPIO motor pins
pinMotorAForwards = 10
pinMotorABackwards = 9
pinMotorBForwards = 8
pinMotorBBackwards = 7


Frequency = 80
# How long the pin stays on each cycle, as a percent (here, it's 30%)
DutyCycleA = 0
DutyCycleB = 0
# Setting the duty cycle to 0 means the motors will not turn
Stop = 0

# Set the GPIO Pin mode
GPIO.setup(pinMotorAForwards, GPIO.OUT)
GPIO.setup(pinMotorABackwards, GPIO.OUT)
GPIO.setup(pinMotorBForwards, GPIO.OUT)
GPIO.setup(pinMotorBBackwards, GPIO.OUT)

# Set the GPIO to software PWM at 'Frequency' Hertz
pwmMotorAForwards = GPIO.PWM(pinMotorAForwards, Frequency)
pwmMotorABackwards = GPIO.PWM(pinMotorABackwards, Frequency)
pwmMotorBForwards = GPIO.PWM(pinMotorBForwards, Frequency)
pwmMotorBBackwards = GPIO.PWM(pinMotorBBackwards, Frequency)

pwmMotorAForwards.start(0)
pwmMotorABackwards.start(0)
pwmMotorBForwards.start(0)
pwmMotorBBackwards.start(0)

def Motor_dc(a,b,c,d):
    pwmMotorAForwards.ChangeDutyCycle(a)
    pwmMotorABackwards.ChangeDutyCycle(b)
    pwmMotorBForwards.ChangeDutyCycle(c)
    pwmMotorBBackwards.ChangeDutyCycle(d)




Motor_dc(50,0,50,0) #forward
sleep(15)

Motor_dc(0,50,0,50) #back
sleep(3)


GPIO.cleanup()



User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Raspberry LX terminal odd behaviour

Mon Jan 15, 2018 5:52 pm

Try putting print statements after each chunk of code with a small delay afterwards to see where the problem is starting, eg.

Code: Select all

# Set the GPIO modes
print "Setting GPIO modes"
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
time.sleep(5)
(you'll need to import the time module of course).

Dave.
Apple say... Monkey do !!

papous
Posts: 80
Joined: Fri Jan 05, 2018 5:50 am

Re: Raspberry LX terminal odd behaviour

Mon Jan 15, 2018 6:33 pm

Dave thanks so much for your reply.
It actually worked. I even reduced the sleep period to 1 and it is ok
It sounds though that the engines 'initiate' at the point of

Code: Select all

pwmMotorAForwards.start(0)
pwmMotorABackwards.start(0)
pwmMotorBForwards.start(0)
pwmMotorBBackwards.start(0)
That's no problem but do you know why it happens?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Raspberry LX terminal odd behaviour

Mon Jan 15, 2018 7:25 pm

That's no problem but do you know why it happens?
Unfortunately not, I've had problems running simple things from Idle. I now use PyCharm with Anaconda for developing, works for me.

Dave.
Apple say... Monkey do !!

Return to “Python”