Let's Make Robots! | RobotShop

raspberry pi arduino,

So im pretty newish to robotics and programming in general, but ive done some reading and have started down my own path.

my robot(including a picture, description, wiring diagram and code) is posted here https://www.robotshop.com/letsmakerobots/node/40350

im having some trouble with the python arduino interface.  I can get python to send commands quite well to the arduino, but am having trouble getting the arduino to send commands back, ive tried pyserials readline() and had trouble.  I guess im not asking for specifics on how to get it done but would love it if someone could look over what ive done and see if im even remotley on the right path

Thanks!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

import serial


ser = serial.Serial(2)  ' change 2 to the value of your com port -1. Here I'm using COM3
while True:
    c = ser.readline()
    print c

To change baudrate looks like ser = serial.Serial( port='COM3', baudrate='19200' )
you'll also notice that you can select com port this way too and actually use the correct number unlike in the example above.
all other options normally available for the com port can be set inside the brackets as well.

This simple code snippet just reads a continual stream from the arduino. The sketch is analogserial in example folder.

On the Arduino Side:

(I send a CRLF terminated line starting with a data type descriptor: in this case "SNSR" for sensor data)

void Send_Sensors() {
   Serial.print("SNSR,");
   Serial.print( now);   Serial.print(",");   
   Serial.print(motion);   Serial.print(",");  
   
   Serial.print(AccScaled.XAxis);   Serial.print(",");   
   Serial.print(AccScaled.YAxis);   Serial.print(",");   
   Serial.print(AccScaled.ZAxis);   Serial.print(",");
   Serial.print(CurrentHeading);    Serial.print(",");
   
   
    Serial.print(DirectAhead); Serial.print(",");      Serial.print(DirectBehind);  Serial.print(",");
   Serial.print(DirectRight);  Serial.print(",");  Serial.print(DirectLeft); Serial.print(",");
   Serial.print(FrontRight);  Serial.print(",");  Serial.print(FrontLeft);   Serial.print(",");  
   Serial.print(Temperature);  Serial.print(",");  Serial.print(Pressure);   Serial.print(",");  
   Serial.print(Altitude);  Serial.print(",");  Serial.println(batvolt);
   

}   


On the Raspberry Pi Python Side:

(Caveat: this is just a snippet from my code... it may not function as is:)

I import Serial

I declare a function to read and parse a line from Serial

I set up the serial port and open it.

I then wait for incoming data in my main loop.  If the first field matches what I'm looking for, I update my variables.


#! /usr/bin/python
#

import serial

########################################################################
# Global Variables   -  Yes I know these are bad...

accel_x = 0.0
accel_y = 0.0
accel_z = 0.0
heading = 0.0
directahead = 0.0
directbehind = 0.0
directleft = 0.0
directright = 0.0
voltage = 0

ready=0                        # ready to send command to arduino
timeout = 0                    # To avoid infinite wait state, we will count 100 loops and assume failure
line = None                     # Holds current response from Arduino Serial
line_len = 0                    # number of items in "line"


##########################################################################
# Declare functions

def get_arduino_response():
   global line
   global line_len
  
   if (arduino.inWaiting() > 0):              # Number of characters in arduino buffer    
       line = arduino.readline()
       line = line.split(",")
       line_len = len(line)
       print 'Receiving: line[0]= ' , line, 'Num Items= ', line_len, 'timeout= ', timeout


##########################################################################
# Set up serial port to Arduino
arduino = serial.Serial('/dev/ttyACM0', 115200, timeout = 10)
arduino.open()


time.sleep(10)    # Time for Arduino to reset and settle
arduino.flushInput()    


##########################################################################
# Main Loop

try:
    running = True

    while 1:                    # Continuous loop
       # os.system('clear')
       stamp = time.time()
 
       get_next_command()            # find the next available command to process

       get_arduino_response()                   # If data available from arduino, retrieve into "line"
      

       if line[0] == "SNSR" and line_len == 13:    # Ensure entire line came through
          ardtime = line[1]
              motion = line[2]
          accel_x = float(line[3])
          accel_y = float(line[4])
          accel_z = float(line[5])
          heading = float(line[6])
          directahead = float(line[7])
          directbehind = float(line[8])
          directleft = float(line[9])
          directright = float(line[10])
          voltage = line[11]
          UID = line[12]
          UID = UID.strip()


          print 'line ' , ardtime, accel_x, accel_y, accel_z, heading ,'done'


 
   
 
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    print "\nKilling Thread..."
    db.close()
print "Done.\nExiting."

 


Thanks for sharing your code. I know my example was a bit light on and I've borrowed some of your code now to improve something I'm working on.

Hope the code helps.  I've borrowed graciously from others... even much of this, I believe came from somewhere else...

You'll have to let me know how it works out.

 

thank you for your thourough reply, Ill try to impiment what I see once i get some time off work, Ill let you know how it goes