-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathping1d.py.in
More file actions
125 lines (107 loc) · 4.75 KB
/
ping1d.py.in
File metadata and controls
125 lines (107 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
# ping1d.py
# A device API for the Blue Robotics Ping1D echosounder
# ~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
# THIS IS AN AUTOGENERATED FILE
# DO NOT EDIT
# ~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
from brping import definitions
from brping import PingDevice
from brping import pingmessage
import serial
import time
class Ping1D(PingDevice):
def legacyRequest(self, m_id, timeout=0.5):
msg = pingmessage.PingMessage()
# legacy hack logic is in PingMessage
# TODO: remove that logic and construct/assemble an arbitrary PingMessage
msg.request_id = m_id
msg.pack_msg_data()
self.write(msg.msg_data)
# uncomment to return nacks in addition to m_id
# return self.wait_message([m_id, definitions.COMMON_NACK], timeout)
return self.wait_message([m_id], timeout)
def initialize(self, baudrate=None):
if not PingDevice.initialize(self, baudrate):
return False
if self.legacyRequest(definitions.PING1D_GENERAL_INFO) is None:
return False
return True
{% for msg in messages["get"]|sort %}
##
# @brief Get a {{msg|replace("get_", "")}} message from the device\n
# Message description:\n
# {{messages["get"][msg].description}}
#
# @return None if there is no reply from the device, otherwise a dictionary with the following keys:\n
{% for field in messages["get"][msg].payload %}
# {{field.name}}: {% if field.units %}Units: {{field.units}}; {% endif %}{{field.description}}\n
{% endfor%}
def get_{{msg}}(self):
if self.legacyRequest(definitions.PING1D_{{msg|upper}}) is None:
return None
data = ({
{% for field in messages["get"][msg].payload %}
"{{field.name}}": self._{{field.name}}, # {% if field.units %}Units: {{field.units}}; {% endif %}{{field.description}}
{% endfor %}
})
return data
{% endfor %}
{% for msg in messages["set"]|sort %}
##
# @brief Send a {{msg}} message to the device\n
# Message description:\n
# {{messages["set"][msg].description}}\n
# Send the message to write the device parameters, then read the values back from the device\n
#
{% for field in messages["set"][msg].payload %}
# @param {{field.name}} - {% if field.units %}Units: {{field.units}}; {% endif %}{{field.description}}
{% endfor %}
#
# @return If verify is False, True on successful communication with the device. If verify is False, True if the new device parameters are verified to have been written correctly. False otherwise (failure to read values back or on verification failure)
def {{msg}}(self{% for field in messages["set"][msg].payload %}, {{field.name}}{% endfor %}, verify=True):
m = pingmessage.PingMessage(definitions.PING1D_{{msg|upper}})
{% for field in messages["set"][msg].payload %}
m.{{field.name}} = {{field.name}}
{% endfor %}
m.pack_msg_data()
self.write(m.msg_data)
if self.legacyRequest(definitions.PING1D_{{msg|replace("set_", "")|upper}}) is None:
return False
# Read back the data and check that changes have been applied
if (verify
{% if messages["set"][msg].payload %}
and ({% for field in messages["set"][msg].payload %}self._{{field.name}} != {{field.name}}{{ " or " if not loop.last }}{% endfor %})):
{% endif %}
return False
return True # success
{% endfor %}
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Ping python library example.")
parser.add_argument('--device', action="store", required=True, type=str, help="Ping device port.")
parser.add_argument('--baudrate', action="store", type=int, default=115200, help="Ping device baudrate.")
args = parser.parse_args()
p = Ping1D(args.device, args.baudrate)
print("Initialized: %s" % p.initialize())
{% for msg in messages["get"]|sort %}
print("\ntesting get_{{msg}}")
result = p.get_{{msg}}()
print(" " + str(result))
print(" > > pass: %s < <" % (result is not None))
{% endfor %}
print("\ntesting set_device_id")
print(" > > pass: %s < <" % p.set_device_id(43))
print("\ntesting set_mode_auto")
print(" > > pass: %s < <" % p.set_mode_auto(False))
print("\ntesting set_range")
print(" > > pass: %s < <" % p.set_range(1000, 2000))
print("\ntesting set_speed_of_sound")
print(" > > pass: %s < <" % p.set_speed_of_sound(1444000))
print("\ntesting set_ping_interval")
print(" > > pass: %s < <" % p.set_ping_interval(36))
print("\ntesting set_gain_setting")
print(" > > pass: %s < <" % p.set_gain_setting(3))
print("\ntesting set_ping_enable")
print(" > > pass: %s < <" % p.set_ping_enable(True))
print(p)