-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbusmeres.py
193 lines (147 loc) · 4.42 KB
/
modbusmeres.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import sys
import serial
import numpy as np
import RPi.GPIO as GPIO
import time
from pymodbus.client.sync import ModbusTcpClient
from helper import Edge, Filter
from statemachine import StateMapElement, StateMachine
from datetime import datetime
from ujkeres import ujkeres
from korkeres import findCircle
from enum import Enum
from collections import defaultdict
#######################################################################################################
class Msg:
msg = ""
cnt = 0
cursorup = '\033[F'
erase = '\033[K'
en = 0
def printMsg(self, msg):
if (self.en == 0):
return
print(msg)
#if (self.msg is msg):
# if (self.cnt > 1) :
# print(self.cursorup+self.erase)
# self.cnt += 1
#print(msg + " ({})".format(self.cnt))
#else:
# self.cnt = 0
#self.msg = msg
#print(msg)
#y 30 110
#x 360 440
#z 25
#406-33
class State(Enum):
SignalWait = 1
GetPosition = 2
CheckPositionList = 3
CalculateNewPosition = 4
WaitScanReady = 5
ReturnMovement = 6
CalculateCenter = 7
Stop = 8
def setNeg(n):
if (n < 0):
return n+pow(2,16)
else:
return n
def getSigned16bit(a):
if (a >> 15) & 1:
return a - (1 << 16)
return a
def readID(port):
line = port.readline()
return line.replace("\x02","").replace("\x03","")
def log(s):
with open(f, "a+") as myfile:
myfile.write(s)
return
###############################################################################################
# Raspberry config
GPIO.setmode (GPIO.BCM)
GPIO.setup(4, GPIO.IN)
serialPort = serial.Serial('/dev/ttyS0',9600)
# Communication registers
dataReadyReg = 500
dataXReg = 1000
dataYReg = 1002
newDataReadyReg = 1006
dataRead = 0
client = ModbusTcpClient('192.168.0.104', 502)
conn = client.connect()
SignalFilter = Filter(16)
SignalEdge = Edge()
ReadyEdge = Edge()
msg = Msg()
edgeDetected = 0
dataReadyEdgeDetected = 0
pointArray = []
currPos = []
t = datetime.now().time().strftime("%H%M%S")
f = "meres/30seb8tav"+t+".txt"
idsToSearch = 2
scanPoints = []
isScanning = False
pointsx = []
pointsy = []
iterationCounter = 0
maxIterations = 3
pointDict = defaultdict(list)
latestID = ""
scanningID = ""
finishedIDs = []
currentState = State.SignalWait
client.write_register(500, 0)
client.write_register(510, 0)
while 1:
signalType = ""
# Waits for RFID signal edge
if (currentState == State.SignalWait):
input_v = GPIO.input(4)
signal = SignalFilter.step(input_v)
signalEdge = SignalEdge.chk(signal)
signalType = signalEdge['type']
# Notify robot of RFID signal change
if (signalEdge['value'] == 1):
latestID = readID(serialPort)
client.write_register(dataReadyReg, 1)
currentState = State.GetPosition
continue
# Gets robot's current position data
elif (currentState == State.GetPosition):
dataReady = client.read_holding_registers(newDataReadyReg,1)
#msg.printMsg("\n Checking if data is ready: {}".format(dataReady))
if (dataReady == None):
msg.printMsg("dataready none")
continue
dataReady = dataReady.registers[0]
readyEdge = ReadyEdge.chk(dataReady)['value']
if (readyEdge):
time.sleep(0.5)
xy = client.read_holding_registers(dataXReg,4)
x = getSigned16bit(xy.registers[0])
y = getSigned16bit(xy.registers[2])
# If the point is too close to the latest one, disregard
if (len(currPos)>0):
d = np.linalg.norm(np.array([x,y])-np.array(currPos))
if (d < 10):
currentState = State.ReturnMovement
continue
currPos = [x,y]
pointsx.append(float(x))
pointsy.append(float(y))
log("\n{},{},{}".format(latestID, x, y))
pointDict[scanningID].append(currPos)
#pointArray.append(currPos)
msg.printMsg("\n Data ready signal changed to {}".format(dataReady))
currentState = State.ReturnMovement
continue
# Need to find more points, return robot movement as it were
elif (currentState == State.ReturnMovement):
client.write_register(500,2)
currentState = State.SignalWait
client.close()