Dawn Robotics Forum Support and community forums for Dawn Robotics Ltd 2015-05-20T11:09:19+01:00 http://forum.dawnrobotics.co.uk/feed.php?f=10&t=1365 2015-05-20T11:09:19+01:00 2015-05-20T11:09:19+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1796#p1796 <![CDATA[Re: get_sensor_readings]]>

Thanks for the vote!

Regards

Alan

Statistics: Posted by Alan — Wed May 20, 2015 11:09 am


]]>
2015-05-18T23:24:17+01:00 2015-05-18T23:24:17+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1795#p1795 <![CDATA[Re: get_sensor_readings]]>
I've cast the vote, good luck!

Statistics: Posted by Puma — Mon May 18, 2015 11:24 pm


]]>
2015-05-15T12:47:15+01:00 2015-05-15T12:47:15+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1792#p1792 <![CDATA[Re: get_sensor_readings]]>
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:
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

Statistics: Posted by Alan — Fri May 15, 2015 12:47 pm


]]>
2015-05-14T08:28:09+01:00 2015-05-14T08:28:09+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1791#p1791 <![CDATA[Re: get_sensor_readings]]>
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:
#! /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

Statistics: Posted by Puma — Thu May 14, 2015 8:28 am


]]>
2015-05-14T01:32:54+01:00 2015-05-14T01:32:54+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1790#p1790 <![CDATA[Re: get_sensor_readings]]>
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'

Statistics: Posted by Puma — Thu May 14, 2015 1:32 am


]]>
2015-05-14T01:08:10+01:00 2015-05-14T01:08:10+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1789#p1789 <![CDATA[Re: get_sensor_readings]]>
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

Statistics: Posted by Alan — Thu May 14, 2015 1:08 am


]]>
2015-05-13T23:40:03+01:00 2015-05-13T23:40:03+01:00 http://forum.dawnrobotics.co.uk/viewtopic.php?t=1365&p=1788#p1788 <![CDATA[get_sensor_readings]]>
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.

Statistics: Posted by Puma — Wed May 13, 2015 11:40 pm


]]>