1
votes

We want to control a P3DX robot using values obtained from a potentiometer connected to a GPIO pin on the Raspberry pi 2. Currently we have two Python files: one that moves the robot, and one that reads the GPIO pin connected to a potentiometer. Both files are already working on their own. But is it possible to combine the two files into just one file and run it? Because I was thinking that the Python file interfacing with ROS would need to run separately. If not, what's the easiest way to implement what we want to do?

***Update: Essentially we are trying to use a catenary method to move two robots in formation. So we want to measure the angle the potentiometer makes with a rope, in which the other end is attached to another robot. Based on the angle, it gives the orientation of the robot, and then we can make the proper adjustments to realign the robot to the centerline so that the robot follows the first robot in front.

We followed this link on how to connect potentiometer to Raspberry Pi 2 and read values. It also includes the script file: https://learn.adafruit.com/downloads/pdf/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.pdf

Here's the file for moving the P3DX:`

#!/usr/bin/env python
import getch
import roslib; roslib.load_manifest('p3dx_mover')
import rospy

from geometry_msgs.msg import Twist

KEY_UP = 65
KEY_DOWN = 66
KEY_RIGHT = 67
KEY_LEFT = 68
USER_QUIT = 100

MAX_FORWARD = 1.1
MAX_LEFT = 0.3
MIN_FORWARD = -1.1
MIN_LEFT = -0.3

forward = 0.0
left = 0.0
keyPress = 0

while(keyPress != USER_QUIT):
    pub = rospy.Publisher('RosAria/cmd_vel', Twist)
    rospy.init_node('p3dx_mover')

    twist = Twist()

    keyPress = getch.getArrow()

    if((keyPress == KEY_UP) and (forward <= MAX_FORWARD)):
        forward += 0.1
    elif((keyPress == KEY_DOWN) and (forward >= MIN_FORWARD)):
        forward -= 0.1
    elif((keyPress == KEY_LEFT) and (left <= MAX_LEFT)):
        left += 0.1
    elif((keyPress == KEY_RIGHT) and (left >= MIN_LEFT)):
        left -= 0.1

    twist.linear.x = forward
    twist.angular.z = left
    pub.publish(twist)

pub = rospy.Publisher('RosAria/cmd_vel', Twist)
rospy.init_node('p3dx_mover')
twist = Twist()
pub.publish(twist)
exit()

` Here's the link we used for reading the potentiometer voltage level values: https://gist.github.com/ladyada/3151375 Ignore the volume stuff, we didn't include them. Just reading the pot.

2
Welcome to Stack Overflow! Welcome to Stackoverflow! Can you please elaborate your question having your effort like code or something so that people could get your problem early and help you? Thanks!Enamul Hassan
1) Please show your code (or a simple example of what you're doing). Otherwise, this is too broad. However, the real issue comes down to this--do you need both of these operations to happen continuously, independent of eachother? Either way, you can combine this into one program/file. However, if you need them to run independently without blocking eachother, we might need some multiprocessing!Will
"one that reads the GPIO pin connected to a potentiometer" How are you reading the potentiometer given that the Raspi has no built-in analog-to-digital-converter (ADC)? External ADC? Poor man's ADC (capacitor + resistor)? Something else?jDo
@Will I posted the code that we are using. Yes it needs to run continuously. I would assume they are dependent of each other since we want to use the potentiometer values as input to move the robot. We are trying to measure the angle the potentiometer makes with a rope. Based on the angle, it gives the orientation of the robot, and then we can make the proper adjustments to realign the robot to the centerline.Adrian
@jDo We are using the MCP3008 as an external ADC chip to communicate the data from the potentiometer to the Raspberry Pi 2.Adrian

2 Answers

1
votes

Yes you can do this - And what you need to look into is threading.

This can be rather a difficult task (not sure of your programming level) - It also depends on how you want to display the information.

PyQt5 has VERY Good support for running multiple threads and outputting the display in a common Gui Interface, if you are not planning on using a GUI (and on a Raspberry I guess you would not) then you need to look at the native threading module.

0
votes

As a matter of design, if you are wanting to get that working in ROS, I won't recommend getting them combined. The reason is that ROS nodes shoud have a specific and the simplest possible task to perform as explained on ROS Nodes wiki. So in your case, one reading and the other controling, fine.

In the other part, if you really to get them combined, you may use what Tim suggested. Multithreading is a good solution. So, you have to implement two threads working separately and maybe sharing a deque, the one reading from the GPIO uses a .push_back and the one who drives uses a .pop_front.