-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprojSimLinux.cpp
More file actions
227 lines (194 loc) · 5.64 KB
/
projSimLinux.cpp
File metadata and controls
227 lines (194 loc) · 5.64 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
============================================================================
Name : projSimLinux.cpp
Author : OpenEL-WG
Version : 3.2
Copyright : Japan Embedded Systems Technology Association(JASA)
Description : OpenEL Sample program
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "openEL.hpp"
#include "Actuator.hpp"
#include "Sensor.hpp"
#include "HAL4RT.hpp"
#include "HALId.hpp"
#include "ReturnCode.hpp"
#include "HALComponent.hpp"
#include "HALObserver.hpp"
#include "EventTimer.hpp"
#include "TimerObserver.hpp"
#include "Property.hpp"
Actuator *halMotor01;
Actuator *halMotor02;
Sensor *halSensor01;
void cbNotifyTimer101();
void cbNotifyTimer102();
void cbNotifyTimer201();
int32_t event_count1,event_count2,event_count3;
HALFLOAT_T simVal1,simVal2,simVal3,simVal4;
int32_t tmSensor;
static void notify_event201a(int32_t eventID) {
printf("notify_event201a : %d\n",eventID);
}
static void notify_error201a(int32_t errorID) {
printf("notify_error201a : %d\n",errorID);
}
static void notify_event201b(int32_t eventID) {
printf("notify_event201b : %d\n",eventID);
}
static void notify_error201b(int32_t errorID) {
printf("notify_error201b : %d\n",errorID);
halSensor01->ReInit();
}
void outProperty(HALComponent *hC);
int main(void) {
int32_t timeWk;
uint8_t flgObs;
printf("openEL Start\n");
HALId motor01;
HALId motor02;
HALId sensor01;
TimerObserver tmObs101;
TimerObserver tmObs102;
TimerObserver tmObs201;
EventTimer halEvtTm100;
EventTimer halEvtTm200;
HALObserver halObs201a;
HALObserver halObs201b;
motor01.deviceKindId=0x00000001;
motor01.vendorId=0x00000000;
motor01.productId=0x00000000;
motor01.instanceId=0x00000001;
motor02.deviceKindId=0x00000001;
motor02.vendorId=0x00000000;
motor02.productId=0x00000000;
motor02.instanceId=0x00000002;
sensor01.deviceKindId=0x00000002;
sensor01.vendorId=0x00000000;
sensor01.productId=0x00000000;
sensor01.instanceId=0x00000001;
halMotor01 = new Actuator(motor01);
halMotor02 = new Actuator(motor02);
halSensor01 = new Sensor(sensor01);
if (halMotor01->Init() == HAL_ERROR) {
printf("Init() Error\n");
if (halMotor01->ReInit() == HAL_ERROR) {
printf("ReInit() Error\n");
exit(0);
};
};
if (halMotor02->Init() == HAL_ERROR) {
printf("Init() Error\n");
if (halMotor02->ReInit() == HAL_ERROR) {
printf("ReInit() Error\n");
exit(0);
};
};
if (halSensor01->Init() == HAL_ERROR) {
printf("Init() Error\n");
if (halSensor01->ReInit() == HAL_ERROR) {
printf("ReInit() Error\n");
exit(0);
};
};
outProperty(halMotor01);
outProperty(halMotor02);
outProperty(halSensor01);
tmObs101.notify_timer = &cbNotifyTimer101;
tmObs102.notify_timer = &cbNotifyTimer102;
tmObs201.notify_timer = &cbNotifyTimer201;
halObs201a.notify_event = ¬ify_event201a;
halObs201a.notify_error = ¬ify_error201a;
halObs201b.notify_event = ¬ify_event201b;
halObs201b.notify_error = ¬ify_error201b;
printf("motor01 getTime ret=%d\n", halMotor01->GetTime(&timeWk) );
printf("Motor time = %d\n",timeWk);
printf("sensor01 getTime ret=%d\n", halSensor01->GetTime(&timeWk) );
printf("Sensor time = %d\n",timeWk);
halMotor01->AddObserver(&halObs201a);
halMotor02->AddObserver(&halObs201b);
flgObs = 1;
halEvtTm100.SetEventPeriod(100);
halEvtTm100.AddObserver(&tmObs101);
halEvtTm100.AddObserver(&tmObs102);
halEvtTm100.StartTimer();
halEvtTm200.SetEventPeriod(200);
halEvtTm200.AddObserver(&tmObs201);
halEvtTm200.StartTimer();
// ループ
while(1) {
usleep(500000); /* 0.5s */
printf("timer %6d , %5d , %6d : ",event_count1,event_count2,event_count3);
printf("%7.3lf %7.3lf %7.3lf %7.3lf %5d(tmSen)\n",simVal1,simVal2,simVal3,simVal4,tmSensor);
fflush(stdout);
if ( (150 <= event_count1) && (1==flgObs) ) {
halMotor01->RemoveObserver(&halObs201a);
halMotor02->RemoveObserver(&halObs201b);
flgObs = 0;
}
if( 300 <= event_count1 ) break;
}
halMotor01->GetTime(&timeWk);
printf("Motor time = %d\n",timeWk);
halSensor01->GetTime(&timeWk);
printf("Sensor time = %d\n",timeWk);
halEvtTm100.StopTimer();
halEvtTm100.RemoveObserver(&tmObs101);
halEvtTm100.RemoveObserver(&tmObs102);
halEvtTm200.StopTimer();
halEvtTm200.RemoveObserver(&tmObs201);
halMotor01->Finalize();
halMotor02->Finalize();
halSensor01->Finalize();
printf("openEL End\n");
return EXIT_SUCCESS;
}
void cbNotifyTimer101() {
DEBUG_PRINTF("%s\n", __func__);
HALFLOAT_T posX,posY;
double ph;
int32_t idxPh;
Request request;
request = Request::HAL_REQUEST_POSITION_CONTROL;
event_count1++;
idxPh = event_count1 % 150;
if ( idxPh <= 100 ) {
ph = ((double)idxPh * 2.0 *M_PI)/100.0;
} else {
ph = ((double)100 * 2.0 *M_PI)/100.0;
}
posX = 10.0*(1.0-cos(ph));
posY = 10.0*(sin(ph));
halMotor01->SetValue(request,posX);
halMotor02->SetValue(request,posY);
}
void cbNotifyTimer102() {
DEBUG_PRINTF("%s\n", __func__);
event_count2 += 10;
Request request;
request = Request::HAL_REQUEST_POSITION_CONTROL;
halMotor01->GetValue(request,&simVal1);
halMotor02->GetValue(request,&simVal2);
}
void cbNotifyTimer201() {
DEBUG_PRINTF("%s\n", __func__);
int size;
event_count3 += 1;
halSensor01->GetValueList(&simVal3, &size) ;
halSensor01->GetTimedValueList(&simVal4,&tmSensor,&size);
}
void outProperty(HALComponent *hC) {
DEBUG_PRINTF("%s\n", __func__);
int32_t i;
Property propertyWk , *property;
property = &propertyWk;
hC->GetProperty(&propertyWk);
printf("PROPERTY - Name : %s\n",property->deviceName.c_str());
for ( i=0; i<sizeof(property->functionList); i++ ) {
printf("PROPERTY - fnc%02X : %s\n",i,property->functionList[i].c_str());
}
}