Asked — Edited

Reading Mpu9150 From Python

Hi all,

Is there a way to get sensor readings from an MPU9150 directly in Python? I've tried writing its values to a file and then reading from that file as it's being written into (to develop a live interface), but that crashes when both ARC and Python are trying to access the same file at once.

Python was able to get the camera image data just fine. Figure there must be a way to grab sensor data as well.

Thanks, Michael


ARC Pro

Upgrade to ARC Pro

Elevate your robot's capabilities to the next level with Synthiam ARC Pro, unlocking a world of possibilities in robot programming.

#1  

I would use the Mqtt broker and client plugins to have ARC push the data to an MQ topic, and then use a Python mqtt client to read the topic (dozens of sample clients if you search python mqtt client).

Alan

PRO
USA
#2  

If you are using Python there are few options available.

If is something simple i would go with a Http Client / Server solution.

PRO
USA
#3  

ARC side (Http Client):

you can use HttpGet function.

Client calling a server mydesktop on port 8899


$quit=false
$CompassHeading = 0

repeatwhile($quit=false)
  #read the imu here 
  #let's simulate a rotating heading
  $CompassHeading = $CompassHeading + 1
  if ($CompassHeading>360)
    $CompassHeading = 0
  endif

  print("Sending heading=" + ToString($CompassHeading))
  
  HTTPGet("http://mydesktop:8899/imu?heading= " + ToString($CompassHeading))
  Sleep(1000) 

endrepeatwhile 


PRO
USA
#4  

Python side (Server)


#!/usr/bin/python
import time
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import urlparse, parse_qs

quit = False
PORT_NUMBER = 8899

#This class will handles any incoming request from the browser 
class myHandler(BaseHTTPRequestHandler):
	
	#Handler for the GET requests
	def do_GET(self):

		try:
			url = urlparse(self.path)
			path = url.path
			query = parse_qs(url.query)

			if path=="/quit":
				print("Got quit")
				quit = True
		
			elif path=="/imu":
				self.send_response(200, "OK")
				self.send_header("Content-type", "text/plain")
				self.end_headers()
				self.wfile.write("OK")

				print("We got heading=", query.get('heading', None))
			else:
				self.send_error(404)
		
		except:
			self.send_error(500)

		self.finish()
		#self.connection.close()
		return

try:
	#Create a web server and define the handler to manage the incoming request
	server = HTTPServer(('', PORT_NUMBER), myHandler)
	print 'Started httpserver on port:', PORT_NUMBER
	
	#Wait for incoming http requests
	while quit == False:
		server.handle_request()

except KeyboardInterrupt:
	print '^C received, shutting down the web server'
	server.socket.close()

PRO
USA
#5  

The Python 2.x code runs both on Linux and Windows, creates a web server on port 8899 (PORT_NUMBER) and handles two requests:

  1. /quit
  2. /imu?heading=nnn

you can create a thread to handle the server requests:


while quit == False:
    server.handle_request()

once you get the values you have a lot options.