-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlMeters.cpp
203 lines (177 loc) · 3.46 KB
/
ControlMeters.cpp
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
193
194
195
196
197
198
199
200
201
202
203
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "global.h"
using namespace std;
using namespace cv;
//引入别的模块定义的函数
extern int CatchImage(int save);
extern void CreateImageShowMeter();
extern int ReadCom(HANDLE hCom, unsigned char data[], int number);
extern int WriteCom(HANDLE hCom, unsigned char data[], int number);
extern void CreateImageWithLine(int linelocation);
// 发送控制命令,旋松流量计
// 返回:0正常;1通信失败
int SendCommandUnscrew()
{
unsigned char senddata, recvdata;
int errortimes;
int retval;
senddata = 0x11;
errortimes = 0;
while (errortimes < 3)
{
retval = WriteCom(hComMeter, &senddata, 1);
if (retval != 1)
{
errortimes ++;
continue;
}
retval = ReadCom(hComMeter, &recvdata, 1);
if ( (retval != 1) || (recvdata != (senddata | 0xF0)) )
{
errortimes ++;
continue;
}
else
{
break; // 成功
}
}
if (errortimes == 3)
{
return 1;
}
return 0;
}
// 发送控制命令,旋紧流量计
// 返回:0正常;1通信失败
int SendCommandScrew()
{
unsigned char senddata, recvdata;
int errortimes;
int retval;
senddata = 0x00;
errortimes = 0;
while (errortimes < 3)
{
retval = WriteCom(hComMeter, &senddata, 1);
if (retval != 1)
{
errortimes ++;
continue;
}
retval = ReadCom(hComMeter, &recvdata, 1);
if ( (retval != 1) || (recvdata != (senddata | 0xF0)) )
{
errortimes ++;
continue;
}
else
{
break; // 成功
}
}
if (errortimes == 3)
{
return 1;
}
return 0;
}
// 控制流量计
// 返回:0正常;1:通信失败;2:获取图像失败
int ControlMeters()
{
int startx, endx;
int i, j, ii, jj, x, y;
int offset;
int sumbright, minbright, minj;
if (CatchImage(0) != 0) // 获取图像
{
return 1;
}
CreateImageShowMeter(); // 生成显示图像
// 当前设置的刻度图像,只显示一次
if (FirstImage == 0)
{
CreateImageWithLine(LocationExpected);
//显示流量计设置图片
::PostMessage(hWndMain, WM_SHOWPICLINEEXPECTED, 0, 0);
FirstImage = 1;
}
// 界面的显示图像显示
::PostMessage(hWndMain, WM_SHOWPICMETER, 0, 0);
// 检测浮球位置,发送电机控制指令
// 流量计图像图像:ImgMeterCorrectedShow, 图像尺寸:ImageSizeXMeterShow * ImageSizeXMeterShow
// 目标位置:LocationExpected
startx = 40;
endx = 100;
minbright = ImageSizeXMeterShow * ImageSizeYMeterShow * 255 + 1;
for (i = startx; i < endx; i++ )
{
for (j = FloatBallSize; j < ImageSizeYMeterShow; j++)
{
sumbright = 0;
for (ii = 0; ii < FloatBallSize; ii++)
{
for (jj = 0; jj < FloatBallSize; jj++)
{
x = i + ii;
y = j - jj;
offset = y * ImgMeterCorrectedShow->widthStep + x;
sumbright += (unsigned char) ImgMeterCorrectedShow->imageData[offset];
}
}
if (sumbright < minbright)
{
minbright = sumbright;
minj = j;
}
}
}
// control meters
if (StartStates==1)
{
if (minj > LocationExpected + 10)
{
return SendCommandUnscrew(); // lower, unscrew
}
if (minj < LocationExpected - 10)
{
return SendCommandScrew(); // higher, screw
}
}
return 0;
}
// 测试流量控制,正确返回0,错误返回1
int TestMeterCom()
{
unsigned char senddata, recvdata;
int errortimes;
int retval;
errortimes = 0;
senddata = 0x5A;
while (errortimes < 3)
{
retval = WriteCom(hComMeter, &senddata, 1);
if (retval != 1)
{
errortimes ++;
continue;
}
retval = ReadCom(hComMeter, &recvdata, 1);
if ( (retval != 1) || (recvdata != 0x0A) )
{
errortimes ++;
continue;
}
else
{
break; // 成功
}
}
if (errortimes == 3)
{
return 1;
}
return 0;
}