I have two python files. One is for moving a dc motor(for my arm) and the other is for reading its encoder.
How can I run it such that i will be able to run the motor whilst reading the encoder counts...........????
Thanks,
neteng
Hi scotty101,scotty101 wrote:Running two programs at the same time is probably not the best solution.
You need to integrate the two programs together so that they both work by only running one program. With python (and almost every other language) you can import/include functions and features from other source code files to combine the features of two programs in to one.If you do want some help. You need to either give us a link to the code you are using or post it here betweenCode: Select all
tags.Code: Select all
Code: Select all
from nanpy import (ArduinoApi, SerialManager)
from time import sleep
directionPin1 = 38
directionPin2 = 40
pwmPin = 5
speedVal = 100
stopVal = 0
try:
connection = SeriaManager()
a = ArduinoApi(connection = connection)
except:
print"Failed to connect to arduino"
#setup the pinmodes as if we were in ARDUINO IDE
a.pinMode(directionPin1, a.OUTPUT)
a.pinMode(directionPin2,a.OUTPUT)
a.pinMode(pwmPin,a.OUTPUT)
try:
a.digitalWrite(directionPin1, a.LOW)
a.digitalWrite(directionPin2, a.HIGH)
a.analogWrite(pwmPin, speedVal)
sleep(2)
a.analogWrite(pwmPin, stopVal)
sleep(2)
a.digitalWrite(directionPin1, a.HIGH)
a.digitalWrite(directionPin2, a.LOW)
a.analogWrite(pwmPin, speedVal)
sleep(2)
a.analogWrite(pwmPin, stopVal)
sleep(2)
except KeyboardInterrupt:
Code: Select all
from RPi import GPIO
from time import sleep
clk = 17
dt = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
counter = 0
clkLastState = GPIO.input(clk)
try:
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
if dtState != clkState:
counter += 1
else:
counter -= 1
print (counter)
clkLastState = clkState
#sleep(0.01)
finally:
GPIO.cleanup()