neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Robotic arm

Mon Oct 03, 2016 11:25 am

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

scotty101
Posts: 3738
Joined: Fri Jun 08, 2012 6:03 pm

Re: Robotic arm

Mon Oct 03, 2016 12:14 pm

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 between [code][/code] tags.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Re: Robotic arm

Mon Oct 03, 2016 11:56 pm

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 between tags.
Hi scotty101,

First, I am using nanpy which is to establish serial communication with arduino. The arduino is actually the one that will provide the PWM signal to the motor controllers.....the code for moving the motors is below:

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:
Now below is the encoder file which was from the modmypi site:

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()
How would you be able to call the counter value whilst running the motors??

Thanks,
neteng

Return to “Beginners”