humotion / examples / demo_korg_slider / main.cpp @ 8136fb72
History | View | Annotate | Download (5.588 KB)
1 |
#include <cstdio> |
---|---|
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
|
5 |
|
6 |
#include "korg_input.h" |
7 |
#include "humotion/client/client.h" |
8 |
#include "boost/date_time/posix_time/posix_time.hpp" |
9 |
#include <boost/thread/thread_time.hpp> |
10 |
#include <boost/thread/thread.hpp> |
11 |
|
12 |
using namespace boost; |
13 |
humotion::GazeState last_awake_state; |
14 |
bool awake_state;
|
15 |
|
16 |
enum JOINT_SLIDER_ID{
|
17 |
MOUTH_POSITION_LEFT=0,
|
18 |
MOUTH_POSITION_CENTER, |
19 |
MOUTH_POSITION_RIGHT, |
20 |
PAN, |
21 |
TILT |
22 |
//NECK_ROLL,
|
23 |
}; |
24 |
|
25 |
enum JOINT_POTI_ID{
|
26 |
MOUTH_OPENING_LEFT=0,
|
27 |
MOUTH_OPENING_CENTER, |
28 |
MOUTH_OPENING_RIGHT, |
29 |
EYE_VERGENCE, |
30 |
EYELID_OPENING_UPPER, |
31 |
EYELID_OPENING_LOWER, |
32 |
EYEBROW_RIGHT, |
33 |
EYEBROW_LEFT |
34 |
}; |
35 |
|
36 |
|
37 |
|
38 |
float to_float(unsigned char val){ |
39 |
return ((float)(val))/127.0; |
40 |
} |
41 |
|
42 |
void slidervalues_to_mouthstate(KorgInput *input, humotion::MouthState *result){
|
43 |
unsigned char tmp; |
44 |
|
45 |
//do not handle updates when not awake
|
46 |
if (!awake_state){
|
47 |
return;
|
48 |
} |
49 |
|
50 |
if (input->get_slider(MOUTH_POSITION_LEFT, &tmp))result->position_left = 0.0 + 40.0 * (1.0-to_float(tmp)); |
51 |
if (input->get_slider(MOUTH_POSITION_CENTER, &tmp))result->position_center = 0.0 + 40.0 * (1.0-to_float(tmp)); |
52 |
if (input->get_slider(MOUTH_POSITION_RIGHT, &tmp))result->position_right = 0.0 + 40.0 * (1.0-to_float(tmp)); |
53 |
|
54 |
if (input->get_pot(MOUTH_OPENING_LEFT, &tmp)) result->opening_left = 40.0 * to_float(tmp); |
55 |
if (input->get_pot(MOUTH_OPENING_CENTER, &tmp)) result->opening_center= 40.0 * to_float(tmp); |
56 |
if (input->get_pot(MOUTH_OPENING_RIGHT, &tmp)) result->opening_right = 40.0 * to_float(tmp); |
57 |
|
58 |
//printf("OOO %4.2f %4.2f\n",result.opening_center, result.position_center);
|
59 |
} |
60 |
|
61 |
void wake_up(humotion::GazeState *result){
|
62 |
if (awake_state){
|
63 |
//already awake, return
|
64 |
return;
|
65 |
} |
66 |
|
67 |
//restore last awake state
|
68 |
*result = last_awake_state; |
69 |
//force reopening
|
70 |
result->eyeblink_request_left = 10;
|
71 |
result->eyeblink_request_right = 10;
|
72 |
|
73 |
awake_state = true;
|
74 |
} |
75 |
|
76 |
void sleep(humotion::GazeState *result){
|
77 |
if (!awake_state){
|
78 |
//already sleeping, return
|
79 |
return;
|
80 |
} |
81 |
|
82 |
//store last awake state
|
83 |
last_awake_state = *result; |
84 |
awake_state = false;
|
85 |
|
86 |
//override values:
|
87 |
result->pan = 0.0; |
88 |
result->tilt = -20.0; |
89 |
result->roll = 0.0; |
90 |
|
91 |
//result->eyelid_opening = 0.0;
|
92 |
result->eyebrow_left = 0.0; |
93 |
result->eyebrow_right = 0.0; |
94 |
|
95 |
result->eyeblink_request_left = -1;
|
96 |
result->eyeblink_request_right = -1;
|
97 |
|
98 |
|
99 |
} |
100 |
|
101 |
void slidervalues_to_gazestate(KorgInput *input, humotion::GazeState *result){
|
102 |
unsigned char tmp; |
103 |
|
104 |
//PLAY
|
105 |
if (input->get_button(0x29, &tmp)){ |
106 |
if (tmp != 0){ |
107 |
wake_up(result); |
108 |
} |
109 |
} |
110 |
|
111 |
//STOP
|
112 |
if (input->get_button(0x2A, &tmp)){ |
113 |
if (tmp != 0){ |
114 |
sleep(result); |
115 |
} |
116 |
} |
117 |
|
118 |
//do not handle updates when not awake
|
119 |
if (!awake_state){
|
120 |
return;
|
121 |
} |
122 |
|
123 |
if (input->get_slider(PAN, &tmp)) result->pan = -50.0 + 100.0 * to_float(tmp); |
124 |
if (input->get_slider(TILT, &tmp)) result->tilt = -40.0 + 80.0 * to_float(tmp); |
125 |
result->roll = 0.0; |
126 |
|
127 |
if (input->get_pot(EYELID_OPENING_UPPER, &tmp)) result->eyelid_opening_upper = 0.0 + 120.0*to_float(tmp); |
128 |
if (input->get_pot(EYELID_OPENING_LOWER, &tmp)) result->eyelid_opening_lower = 0.0 + 120.0*to_float(tmp); |
129 |
|
130 |
|
131 |
if (input->get_pot(EYEBROW_LEFT, &tmp)) result->eyebrow_left = 60.0 - 120.0*to_float(tmp); |
132 |
if (input->get_pot(EYEBROW_RIGHT, &tmp)) result->eyebrow_right = 60.0 - 120.0*to_float(tmp); |
133 |
|
134 |
if (input->get_pot(EYE_VERGENCE, &tmp)) result->vergence = -20.0 * to_float(tmp); |
135 |
|
136 |
|
137 |
//check for blink requests:
|
138 |
if (input->get_button(0x24, &tmp)){ |
139 |
if (tmp != 0){ |
140 |
//blink left
|
141 |
result->eyeblink_request_left = 300;
|
142 |
} |
143 |
} |
144 |
if (input->get_button(0x44, &tmp)){ |
145 |
if (tmp != 0){ |
146 |
result->eyeblink_request_right = 300;
|
147 |
} |
148 |
} |
149 |
if (input->get_button(0x34, &tmp)){ |
150 |
if (tmp != 0){ |
151 |
//blink both
|
152 |
result->eyeblink_request_right = 150;
|
153 |
result->eyeblink_request_left = 150;
|
154 |
} |
155 |
} |
156 |
|
157 |
//printf("PTR = %3.1f %3.1f %3.1f\n",result->pan,result->tilt,result->roll);
|
158 |
} |
159 |
|
160 |
int main(int argc, char **argv){ |
161 |
humotion::MouthState mouth_state; |
162 |
humotion::GazeState gaze_state; |
163 |
awake_state = true;
|
164 |
|
165 |
if (argc != 2){ |
166 |
printf("> ERROR: invalid parameter count.\n\nusage: %s <scope>\n (e.g. %s /flobi)\n\n",argv[0],argv[0]); |
167 |
exit(EXIT_FAILURE); |
168 |
} |
169 |
|
170 |
printf("> starting korg kontrol demo. please make sure the korg device is connected via usb\n");
|
171 |
|
172 |
KorgInput *kinput = new KorgInput();
|
173 |
|
174 |
if (!kinput->device_available()){
|
175 |
printf("> no korg kontrol device found on usb bus... exiting\n");
|
176 |
return 0; |
177 |
} |
178 |
|
179 |
//set led status for blink requests:
|
180 |
kinput->set_led(0x24, true); |
181 |
kinput->set_led(0x34, true); |
182 |
kinput->set_led(0x44, true); |
183 |
|
184 |
//set led for stop/play
|
185 |
kinput->set_led(0x29, true); |
186 |
kinput->set_led(0x2A, true); |
187 |
|
188 |
|
189 |
//human motion connection:
|
190 |
humotion::client::Client *client = new humotion::client::Client(argv[1], "ROS"); |
191 |
|
192 |
//send values to human motion server
|
193 |
float loop_delay = 1000.0 / 50.0; //run with 50Hz |
194 |
|
195 |
boost::system_time timeout = get_system_time() + posix_time::milliseconds(loop_delay); |
196 |
|
197 |
while(client->ok()){
|
198 |
//allow middleware to process data
|
199 |
client->tick(); |
200 |
|
201 |
//clear previous eyeblinks
|
202 |
gaze_state.eyeblink_request_left = 0;
|
203 |
gaze_state.eyeblink_request_right = 0;
|
204 |
|
205 |
//handle slider data
|
206 |
if (kinput->fetch_data() > 0){ |
207 |
//debug
|
208 |
mouth_state.dump(); |
209 |
gaze_state.dump(); |
210 |
printf("\n");
|
211 |
|
212 |
//transfer values to robot:
|
213 |
slidervalues_to_mouthstate(kinput, &mouth_state); |
214 |
slidervalues_to_gazestate(kinput, &gaze_state); |
215 |
} |
216 |
|
217 |
//printf("BLINK: %d %d\n",gaze_state.eyeblink_request_left, gaze_state.eyeblink_request_right);
|
218 |
|
219 |
//periodically send mouth & gaze target
|
220 |
client->update_mouth_target(mouth_state); |
221 |
client->update_gaze_target(gaze_state); |
222 |
client->send_all(); |
223 |
|
224 |
thread::sleep(timeout); |
225 |
timeout = get_system_time() + posix_time::milliseconds(loop_delay); |
226 |
|
227 |
} |
228 |
|
229 |
} |