Repository navigation
Expand file tree
/
Copy pathSerialWombatUltrasonicDistanceSensor.py
More file actions
144 lines (110 loc) · 5.04 KB
/
Copy pathSerialWombatUltrasonicDistanceSensor.py
File metadata and controls
144 lines (110 loc) · 5.04 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
"""
Copyright 2023-2024 Broadwell Consulting Inc.
"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
the United States. See SerialWombat.com for usage guidance.
Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
"""
import SerialWombat
from SerialWombatPin import SerialWombatPin
import SerialWombatAbstractProcessedInput
from SerialWombat import SW_LE16
"""! @file SerialWombatUltrasonicDistanceSensor.h
"""
"""! @brief A Class which uses one or two Serial Wombat 18AB pins to measure distance using an Ultrasonic distance sensor.
A video Tutorial on this pin mode is available:
@htmlonly
//TODO
@endhtmlonly
//TODO https://youtu.be/
"""
class SerialWombatUltrasonicDistanceSensor ( SerialWombatAbstractProcessedInput.SerialWombatAbstractProcessedInput):
"""
@brief Class constructor for SerialWombatPulseTimer
@param serialWombat The Serial Wombat chip on which the SerialWombatPulseTimer pinmode will be run
"""
def __init__(self,serial_wombat):
super().__init__(serial_wombat)
self._sw = serial_wombat
self._pinMode = SerialWombat.SerialWombatPinMode_t.PIN_MODE_ULTRASONIC_DISTANCE
#enum driver {
#HC_SR04 = 0, < Standard buffered mode. Colors are uploaded by the host
#}
"""
@brief Initialization routine for SerialWombatUltrasonicDistanceSensor
@param echoPin Pin used to time input pulses. For 5V sensors, 5V tolerant pins 9,10,11,12, 14 and 15 are good choices
@param driver Chip used for distance measurement. Currently only HC_SR04 is supported.
@param triggerPin Pin used for triggering the sensor. If same as echo pin (e.g. 3 pin sensors) set equal to echoPin
@return 0 or higher if successful, negative error code if not successful.
"""
def begin(self,echoPin, driver, triggerPin,autoTrigger = True, pullUp = False):
self._pin = echoPin
tx = [ 200,self._pin,self._pinMode,driver, triggerPin, pullUp,autoTrigger, 0x55 ]
result,rx = self._sw.sendPacket(tx)
self.abstractProcessedInputBegin(self._pin,self._pinMode)
return(result)
"""
@brief get the number of pulses that have been sent.
@return The number of pulses that have been sent. Rolls over at 65536
"""
def readPulseCount(self):
tx = [ 202,self._pin,self._pinMode,0x55,0x55,0x55,0x55, 0x55 ]
result,rx = self._sw.sendPacket(tx)
if(result >= 0):
return (rx[5] + 256 * rx[6])
else:
return 0
"""
@brief Manually trigger a distance reading
Use this interface to trigger a reading if begin was called with autoTrigger = false
@return 0 or higher if successful, negative error code if not successful.
"""
def manualTrigger(self):
tx = [ 201,self._pin,self._pinMode,1,0x55,0x55,0x55, 0x55 ]
result,rx = self._sw.sendPacket(tx)
return(result)
"""
@brief Configure a Servo sweep of Ultrasonic Distance Sensor
"""
def configureServoSweep(self, servoPin, memoryIndex, servoPositions_, servoIncrement, reverse = False, servoMoveDelay = 100, servoReturnDelay = 1000):
tx = [ 203, self._pin, self._pinMode, servoPin,memoryIndex & 0xFF, memoryIndex >>8,
servoPositions_ & 0xFF,servoPositions_ >>8]
result,rx = self._sw.sendPacket(tx)
if (result < 0):
return result
tx = [204, self._pin, self._pinMode, servoIncrement & 0xFF, servoIncrement >> 8,
0x55,0x55, reverse]
result,rx = self._sw.sendPacket(tx)
if (result < 0) :
return result
tx = [205, self._pin, self._pinMode, (servoMoveDelay & 0xFF), (servoMoveDelay >> 8),
servoReturnDelay & 0xFF,servoReturnDelay >>8, 0x55]
result,rx = self._sw.sendPacket(tx)
if (result < 0):
return result
self.servoMemoryIndex = memoryIndex
self.servoPositions = servoPositions_
return 0
def enableServoSweep(self, enable):
tx = [206, self._pin, self._pinMode, enable ,0x55,0x55,0x55, 0x55]
result,rx = self._sw.sendPacket(tx)
return result
def readServoSweepEntry(self, servoSweepEntry):
b = self._sw.readUserBuffer(self.servoMemoryIndex + 2 * servoSweepEntry, 2)
return (b[0] + 256 * b[1])
def readServoSweepEntries(self, entries, count):
return self._sw.readUserBuffer(self.servoMemoryIndex, 2 * count)