Skip to content

Client Data Scheduler

jguc17 edited this page Dec 20, 2016 · 2 revisions

How it works

The scheduler is a Python-based program that collects data from the encoders, IMU, and MoCap PC. It samples the Pi Car's position, velocity, acceleration and gyroscopic angle at 50Hz, or every ~20ms, with the purpose of supplying these metrics to an external application (e.g. controller program, GUI) at a reliable rate.

Main Programs

There are two scheduler programs we created, main_sensors.py and main_all.py.

main_sensors.py

  • samples just the encoders and IMU

This program schedules separate LoopingCalls for each sensor to sample from, and runs the passed-in function every 20ms.

As a form of benchmarking, we record the epoch times upon entering and exiting each LoopingCall, and output the results to a file.

main_all.py

  • samples the sensors and the MoCap system

This program incorporates the contents of twisted_client.py (discussed in the MoCap section), and uses LoopingCalls as described in main-sensors.py.

In addition, as an example application, a web socket is opened on the Raspberry Pi's IP address, through which the program renders the most recent Pi Car metrics in HTML. To view this page while the program is running, open up a browser tab on any device and enter [RaspPi wlan IP]:[web socket port].

Why Twisted?

We opted to use the Twisted Python framework for its asynchronous processing capabilities. It is able to handle multiple connections and their respective data flows by way of abstracting them into object classes. Further, so long as the user's callback functions are non-blocking, then the Twisted reactor loop pattern can freely switch between tasks to reduce execution time.

How to run

  1. On the MoCap PC, plug in the USB hardware key and start up Motive

  2. Once the cameras are online, identify the Pi Car(s) within the software as a rigid body

  3. In the data streaming pane in Motive, check off 'Enable data streaming', ensuring that the multicast IP address matches up with the C++ Extraction client

  4. Open up two Command Prompts. Use one to run the MotiveClient2.exe program (see 'Position Tracking' for compile-build instructions), and the other to run twisted_server7.py

  5. Once data is passing through the server distribution program, open and run main_all.py on the Raspberry Pi

  6. If you wish to view the web page hosted on the Pi, first find its wlan IP address by typing 'ip addr show' in a separate Terminal process. Then, in a browser on the Pi or any other device in the network, enter [RPi IP address]:8881, where 8881 is the port number we used in main_all.py (changeable).

Future Work

  • Handle multiple Pi Cars; see corresponding sections of the 'Position Tracking' wiki page for details
  • In tandem, consider using Twisted's ProcessProtocol module or Python's multiprocessing library to handle each connection's data flow in its own process; the main challenge associated with this implementation is synchronization between the different processes
  • (Optional) In main_all.py, modify the SocketClientProtocol and SocketClientProtocolFactory classes such that message requests can be sent to the Twisted Server on the PC-side. This can be used for customizing payloads sent over from MoCap.
  • (Optional) Improve the UI of the hosted web page

Credits

Clone this wiki locally