get_sensor_readings

The brains of your robot

get_sensor_readings

Postby Puma » Wed May 13, 2015 11:40 pm

Hi Allan, in your script get_sensor_readings. py, the data is read by "data = sensor_dict[ sensor_name ][ "data" ]"

My understanding of Python is still pretty bad, what I would like to do is get each value independently as numeric values, example I'd like to read A0 and A1, but I don't know how to extract them as numerals from "data"; any help would be appreciated, thanks in advance.
Puma
 
Posts: 15
Joined: Wed Apr 15, 2015 10:49 pm

Re: get_sensor_readings

Postby Alan » Thu May 14, 2015 1:08 am

Hi there,

I think that the code you want looks something like this

A0 = sensor_dict[ "analog" ].data[ 0 ]
A1 = sensor_dict[ "analog" ].data[ 1 ]

(although I can't check it right now as I'm at home). This should work because the sensor dictionary contains an entry called analog for the analog readings. All sensor readings are returned as an object which contains a data field and a timestamp field (indicating when it was read). The data field for the analog pins should be an array of 6 numbers with values in the range 0 to 1023 (I hope) where 1023 corresponds to 5V and 0 corresponds to 0V.

Hope that helps.

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: get_sensor_readings

Postby Puma » Thu May 14, 2015 1:32 am

Thanks for helping me out, unfortunately I'm getting error for the example you gave, this is a snippet of the code I'm using from your example

data = sensor_dict[ sensor_name ][ "data" ]
ain0=sensor_dict["analog"].data[0]
ain1=sensor_dict["analog"].data[1]

The error reads as follows...

File "get_sensor_readings.py", line 81, in <module>
ain0=sensor_dict["analog"].data[0]
AttributeError: 'dict' object has no attribute 'data'
Puma
 
Posts: 15
Joined: Wed Apr 15, 2015 10:49 pm

Re: get_sensor_readings

Postby Puma » Thu May 14, 2015 8:28 am

Thanks to a lot of help from a clever guy on twitter, he provided a solution, placing here for anyone else who'd like to use it.

ain0 = sensor_dict["analog"]["data"][0]

ain1 = sensor_dict["analog"]["data"][1]


ain2 =

Don't ask me why it works, I'm still trying to figure Python out, but it definitely works.

Entire script looks like this currently, this of course is just for testing purposes, only function is to read the analogue inputs, additions will be made later to control the motors, the signals I'm reading are coming from a pair of infrared distance measuring devices that use triangulation, this means type of reflected surface has little affect on the measurement.
Code: Select all
#! /usr/bin/python

# This example shows how to configure, and read from, the robot's sensors
import os
import sys
import time
import argparse
import py_websockets_bot
import py_websockets_bot.mini_driver
import py_websockets_bot.robot_config
   
#---------------------------------------------------------------------------------------------------       
if __name__ == "__main__":

    # Set up a parser for command line arguments
    parser = argparse.ArgumentParser( "Gets sensor readings from the robot and displays them" )
    parser.add_argument( "hostname", default="localhost", nargs='?', help="The ip address of the robot" )

    args = parser.parse_args()
 
    # Connect to the robot
    bot = py_websockets_bot.WebsocketsBot( args.hostname )

    # Estimate the offset between our system clock, and the robot's system clock. You only need
    # to do this if you're interested in the age of the sensor readings. If you're not interested
    # then you can leave it out of your own programs
    #print "Estimating robot time offset (this will take about 10 seconds)..."
    #robot_time_offset = bot.estimate_robot_time_offset()
   
    # Configure the sensors on the robot
    sensorConfiguration = py_websockets_bot.mini_driver.SensorConfiguration(
        configD12=py_websockets_bot.mini_driver.PIN_FUNC_ULTRASONIC_READ,
        configD13=py_websockets_bot.mini_driver.PIN_FUNC_DIGITAL_READ,
        configA0=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
        configA1=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
        configA2=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
        configA3=py_websockets_bot.mini_driver.PIN_FUNC_DIGITAL_READ,
        configA4=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
        configA5=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
        leftEncoderType=py_websockets_bot.mini_driver.ENCODER_TYPE_QUADRATURE,
        rightEncoderType=py_websockets_bot.mini_driver.ENCODER_TYPE_QUADRATURE )
   
    # We set the sensor configuration by getting the current robot configuration and modifying it.
    # In this way we don't trample on any other configuration settings
    robot_config = bot.get_robot_config()
    robot_config.miniDriverSensorConfiguration = sensorConfiguration
   
    bot.set_robot_config( robot_config )
   
    # Run in a loop until the user presses Ctrl+C to quit
while True:
   try:
   
       
      bot.update()

            # The sensor readings are returned as part of the status of the robot
      status_dict, read_time = bot.get_robot_status_dict()
           

               
               
      sensor_dict = status_dict[ "sensors" ]
 
                   
      #data = sensor_dict[ sensor_name ][ "data" ]
         
      ain0 = int(sensor_dict["analog"]["data"][0])

      ain1 = int(sensor_dict["analog"]["data"][1])
                   
         
                   
                    # Print out information about the reading
                    #print sensor_name, ":"
      print ain0
      print ain1
                   
            #print "\r"
           
      time.sleep( 0.040 )
      os.system("clear")

   
   except KeyboardInterrupt:
      bot.disconnect()
      print "\r"
      print "Program terminated.\r"
      sys.exit()    # Catch Ctrl+C
Puma
 
Posts: 15
Joined: Wed Apr 15, 2015 10:49 pm

Re: get_sensor_readings

Postby Alan » Fri May 15, 2015 12:47 pm

Ah,

Sorry about the mistake in my code. That's what comes of trying to write code without having a physical system to hand. ;)

As a bit of explanation, my mistake arose because on the Python code that is running on the robot, the sensor reading object does have a member called 'data'. However, when it's transferred over the network, all of the sensor readings are converted to a string in a format called JSON. Then when it gets to the py_websockets_bot library (i.e. your script), the JSON string is converted into a Python dictionary so that you can easily access the values.

Hope that makes a bit of sense. One thing you can do when learning about Python and dictionaries is to print them out, as this can make their structure a lot easier to understand. So for example try

Code: Select all
print sensor_dict


Also, one thing I found very useful when learning Python was just to open up the interpreter by running 'python' on the command line and typing stuff in based on the documentation and the excellent official tutorial. I found Python quite a fun language to learn. :)

On a completely separate note, could I be cheeky and ask for you to vote for us in this competition. Basically it's a chance to win £20,000 in funding which we're hoping to use to create freely available educational materials for schools giving lesson plans for the Raspberry Pi robot (although it should also be applicable to other Raspberry Pi robots as well). We need to reach 250 votes to make it to the next round, so any help is much appreciated.

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: get_sensor_readings

Postby Puma » Mon May 18, 2015 11:24 pm

I hope I didn't come across as knocking your mistake, that was not my intention, would be nice if we humans could commit every little detail to memory, what you did give was helpful in eventually coming up with a solution thanks to the person on twitter.

I've cast the vote, good luck!
Puma
 
Posts: 15
Joined: Wed Apr 15, 2015 10:49 pm

Re: get_sensor_readings

Postby Alan » Wed May 20, 2015 11:09 am

Hey no worries, I didn't think you were knocking me at all. I've been programming long enough now to know that mistakes are one of the things I can do consistently. :)

Thanks for the vote!

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am


Return to Software

Who is online

Users browsing this forum: No registered users and 0 guests