Go Down

Topic: Control two Arduino uno boards using Firmata on Raspberry Pi with Python(HELP) (Read 1 time) previous topic - next topic

Wolny

I want to connect two uno boards to one raspberry pie with USB and control them with python using Firmata.
But the problem to the value of the analog read.
Value works well when only one uno board is connected.
However, if I connect the two uno boards, the read value will be None.
I will attach the code below.
Please help me. :(

****
from pyfirmata import Arduino, util
import time

port = '/dev/ttyACM1'
board = Arduino(port)
time.sleep(1)
port2 = '/dev/ttyACM2'
board2 = Arduino(port2)
time.sleep(1)

servoPin = board.get_pin('d:9:s')

sensor1 = board.get_pin('a:0:i')
sensor2 = board.get_pin('a:1:i')


it = util.Iterator(board)
it.start()
sensor1.enable_reporting()
sensor2.enable_reporting()


   
while(True):
    beam1 = sensor1.read()
    beam2 = sensor2.read()
    print('beam1: ', beam1)
    print('beam2: ', beam2)
     
board.exit()
****

ieee488

I don't know Firmdata, but I see that you "create" a board2 but never do anything with it.
All your code is for board .


Wolny

Thank you for your response. :)

it's not Firmdata, it's Firmata.

The Firmata library implements the Firmata protocol for communicating with software on the host computer. This allows you to write custom firmware without having to create your own protocol and objects for the programming environment that you are using.

There are other moves in board2,
but I removed them because they were not related to the question.
I'll show you the whole code.

*****

from pyfirmata import Arduino, util
import time
import pymysql

conn = pymysql.connect(host='localhost',
                       user='root',
                       password='1234',
                       db='taro',
                       charset='utf8')

port = '/dev/ttyACM1'
board = Arduino(port)
time.sleep(1)
port2 = '/dev/ttyACM2'
board2 = Arduino(port2)
time.sleep(1)


pin2 = board.get_pin('d:2:o')
pin3 = board.get_pin('d:3:o')
pin4 = board.get_pin('d:4:o')
pin5 = board.get_pin('d:5:o')
pin6 = board.get_pin('d:6:o')
pin7 = board.get_pin('d:7:o')

SERVO = 9
servoPin = board.get_pin('d:9:s')

sensor1 = board.get_pin('a:0:i')
sensor2 = board.get_pin('a:1:i')

robotA_6 = board2.get_pin('d:11:s')

num = 0
i = 0

it = util.Iterator(board)
it.start()
sensor1.enable_reporting()
sensor2.enable_reporting()

print('start')
servoPin.write(142)

pin2.write(1)
pin3.write(0)
pin4.write(1)
pin5.write(0)
pin6.write(1)
pin7.write(0)
   
while(True):
    beam1= sensor1.read()
    beam2 = sensor2.read()


    if beam1 is None:
        print('1:',beam1)
    elif num == 0 and beam1 <= 0.1 :
        print('A')
        print('1:',beam1)
        i = i + 1
        time.sleep(5)
        num = i%2
    elif num == 1 and beam1 <= 0.1 :
        print('B')
        print('1:',beam1)
        time.sleep(2)
        servoPin.write(45)
        time.sleep(6)
        servoPin.write(142)
        i = i + 1
        num = i%2
       

    if beam2 is None:
        print('2:',beam2)
    elif beam2 <= 0.1 :
        print('2:',beam2)
        check = check + 1
       

    if check > 0 :
        time.sleep(2)
        for n in range(20, 70, 1):
            robotA_6.write(n)
            time.sleep(0.002)
        time.sleep(2)
        for n in range(70, 20, -1):
            robotA_6.write(n)
            time.sleep(0.002)
        time.sleep(2)

board.exit()

*****

It is not a completed code but it is being tested to get the input value properly.

Robin2

Value works well when only one uno board is connected.
However, if I connect the two uno boards, the read value will be None.
Does that mean that with the identical Python program it works when only one Uno is connected and neither Uno works when the second Uno is connected?

Or, are you using one Python program for the single Uno and a different Python program for when you use two Unos?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Wolny

I connected two uno boards to one Python file.
Analog values read well when a board is connected in one Python program.
But when connecting the board2 analog value is brought into none.

Robin2

I connected two uno boards to one Python file.
Analog values read well when a board is connected in one Python program.
But when connecting the board2 analog value is brought into none.
Your description is still not crystal clear.

For the working version are you connecting the Uno to the RPi before you start the Python program?

For the non-working version are you connecting the two Unos to the RPI before you start the Python program?


What I don't understand is why the Python program does not show an error if you try to run it with one of the Unos disconnected as it cannot make a connection to it. My Python programs don't work if I forget to plug in my Uno.


I have never used Firmata or PyFirmata so if the problem is connected with them I won't be able to help. If you want to try working without Firmata this Simple Python - Arduino demo may be of interest.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

Light-bulb moment .....

Which of the Unos is allocated to ttyACM1 and ttyACM2 depends on the order you connect them. Maybe you have accidentally got board on ACM2 even though the program expects it to be on ACM1.

In my Arduino - PC communication I get the Arduino to send a message to the PC from setup(). Something simple like
Code: [Select]
Serial.println("Starting Arduino A")
which provides simple means to know that the correct Arduino is active on the correct serial port.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ballscrewbob

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Wolny

To : Robin2

Thank you for thinking about my problem.
But it is not a connection issue.
The entry of none has been resolved.
However, the reading by the sensor is not updated and the first reading continues to appear.
I don't know well because I tried it for the first time.
Google has found many people with the same problems as mine.
However, there was no clear solution.

Robin2

The entry of none has been resolved.
It would be useful for other readers if you explain how it has been resolved.

Quote
However, the reading by the sensor is not updated and the first reading continues to appear.
If you need help with that please post the latest version of your program.

...R

PS ... When posting code please use the code button </>


Code: [Select]
so your code
looks like this

and is easy to copy to a text editor See How to use the Forum

Also please use the AutoFormat tool to indent your code for easier reading.
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up