Raspberry Pi + Pololu Maestro + Python3 + Tornado + Servo Control Part 2

In Part 1 we discussed ways to connect all the hardware together.

For there software component, there are 4 different technologies involved:

  1. Device.py, which provides byte commands to Pololu Maestro discussed previously.
  2. DeviceController.py, which provides servo control via function calls on the Device Driver object.
  3. TornadoServer.py, which serves the web page interface for the user, as well as communicating with DeviceController.py based on websocket messages received.
  4. tornado_client.html which contains all the html, JQuery, and CSS script to load the the actual buttons, status messages and the overall look and feel of the interface.

The DeviceController.py can contain 2 functions for a start.

from Device import Device
import time

class DeviceController:
  def __init__(self):
    self.servo = Device()

  def move_up(self):
    self.set_position(1, 1500) #middle
    time.sleep(1) #wait 1 second
    self.set_position(1, 2000)
    return("moving up")
  
  def move_down(self):
    self.set_position(1, 1500) #middle
    time.sleep(1) #wait 1 second
    self.set_position(1, 1000)
    return("moving down")

TornadoServer.py will call these functions based on user input. TornadoServer will also serve tornado_client.html which is accessible via a laptop’s web browser address such as http://your_raspberry_pi_ip:8888

from DeviceController import DeviceController
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web


class IndexHandler(tornado.web.RequestHandler):
 @tornado.web.asynchronous
 def get(self):
  self.render("tornado_client.html")

class WSHandler(tornado.websocket.WebSocketHandler):

 controller = ""

 def check_origin(self, origin):
  return True

 def open(self):
  print ("connecting...")
  try:
   self.controller = DeviceController()
   self.write_message("Hello world!")
  except Exception as a:
  print(a)

 def on_message(self, message):
  print("Command:", message)
  data=""
  try:

   if(data=="up"):
    data = self.controller.move_up()

   elif(data=="down"):

    data = self.controller.move_down()

   self.write_message(data)
  except Exception as a:
  print("Error: ", a)

 def on_close(self):
  print ("closing sockets")
  self.controller =""

application = tornado.web.Application([
 (r'/', IndexHandler),
 (r"/ws", WSHandler),
])


if __name__ == "__main__":
 http_server = tornado.httpserver.HTTPServer(application)
 print ("Waiting client connection via browser port 8888")
 http_server.listen(8888)
 tornado.ioloop.IOLoop.instance().start()

The web interface will be discussed in part 3.

robopi3

(Visited 1,343 times, 1 visits today)

3 thoughts on “Raspberry Pi + Pololu Maestro + Python3 + Tornado + Servo Control Part 2”

  1. Sebastian says:

    Hi Dipto, many thanks for this! I have a question: I get an error “DeviceController() takes exactly 1 argument (0 given)” when connecting to the Websocket server via the HTML5 page. I suspect it is caused by this line of the TornadoServer.py

    self.controller = DeviceController()

    in combination with this part of the DeviceController.py:

    def DeviceController(object):
    self.servo = Device()

    I am not a python expert, so perhaps you could point me in the right direction: Why does not function “DeviceController” take an “object” as an argument? Where should this argument come from? Why is there no argument given when calling the function in the server script?

    And then: How is it the “self” can be used in a function? I have never seen this in Python?

    Would be great if you could shed some light on this. Many thanks!
    Sebastian

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      Hi Sebastian,

      Have a look into object-oriented programming with Python 3: http://www.python-course.eu/python3_object_oriented_programming.php

      There is a proper way to declare a python class. The above code listing for DeviceController.py was over-simplified and has been updated. Thanks for the pickup and give it a go.

      1. Sebastian says:

        Ok, many thanks for this!

Comments are closed.