CyberAtom Python Library

2.5.0

Tutorial: Getting System Information

This tutorial guides through construction of CyberAtom API objects and retrieving system information (model and firmware version) from AHRS device connected to PC via USB cable.

The API library uses concept of a logical device object that represents connected device in user's code. This is what Device class is for. Once instantiated, plays central role in application-to-device communication.

In order to construct a logical device object, references to two existing interfaces needs to be provided:

Constructing Connection Object

To use connection to the device via USB cable, UsbConnection object can be created:

1 connection = cyberatom.UsbConnection()

Constructing Response Handler

Receiving data from the device, is possible by creating class that derives from IResponse class and override specific methods for the data you are interested in.

E.g. to receive and print system information, you can define your response class like that:

1 class ResponseHandler(cyberatom.IResponse):
2  def __init__(self):
3  self.received = False
4 
5  def sysInfo(self, model, firmware):
6  """Overrides a method to do custom action
7  when response SYS_INFO message is retrieved"""
8 
9  self.received = True
10  # display the response from the device on the console.
11  #! [printing results]
12  print "Model:",model,"Firmware:",firmware
13  #! [printing results]

and then in the main function you can instantiate it with:

1 response = ResponseHandler()

Retrieving Data from the Device

Once this is done, you can create logical device object:

1 device = cyberatom.Device(connection,response)

With this device object, you are now free to request for various information, e.g system details, like:

1 device.getSysInfo()

This method call sends request to the device. Now wee also need a way to obtain the information device is sending back. For that we need to use Device.checkForMessages() method, in a periodic loop until the message we expect arrives:

1  device.checkForMessages()

When the system information message arrives from the device, the IResponse.sysInfo() method will be called back by the device object, which eventually will print retrieved information to the console and help to terminate the loop

1  print "Model:",model,"Firmware:",firmware

Complete Code

With adding some exception handling, the complete code can look like that:

1 import cyberatom
2 
3 # ==================================================================
4 # Example on how-to communicate with the CyberAtom ARHS device
5 # and retrieve system information (model, firmware version).
6 # ==================================================================
7 
8 #! [subclassing response]
9 class ResponseHandler(cyberatom.IResponse):
10  def __init__(self):
11  self.received = False
12 
13  def sysInfo(self, model, firmware):
14  """Overrides a method to do custom action
15  when response SYS_INFO message is retrieved"""
16 
17  self.received = True
18  # display the response from the device on the console.
19  #! [printing results]
20  print "Model:",model,"Firmware:",firmware
21  #! [printing results]
22 #! [subclassing response]
23 
25 
26 # Create connection using USB interface
27 #! [creating connection]
28 connection = cyberatom.UsbConnection()
29 #! [creating connection]
30 
31 # Create response handler object used to retrieve
32 # all response messages from the device.
33 #! [constructing response]
34 response = ResponseHandler()
35 #! [constructing response]
36 
37 # Create logical device object connected to
38 # physical device over given USB connection.
39 #! [creating device]
40 device = cyberatom.Device(connection,response)
41 #! [creating device]
42 
43 # Request SYS_INFO message from the connected device.
44 #! [requesting system info]
45 device.getSysInfo()
46 #! [requesting system info]
47 
48 # Wait for response
49 while (not response.received):
50  #! [getting data back]
51  device.checkForMessages()
52  #! [getting data back]

Summary

This tutorial demonstrates how-to retrieve system information from the CyberAtom AHRS device. The technique is based on using central Device class and implementation of IResponse interface as a handler for incoming data. This is the pattern that works for almost any data retrieval scenario.

Other methods of Device class allows to send data to the device in one-directional way, which is event simpler and does not require implementing subclassing from IResponse.