Skip to content

Commit f520775

Browse files
daniellasryAndrey1994
authored andcommitted
fix daisy bug (#158)
1 parent 08267d5 commit f520775

File tree

2 files changed

+102
-89
lines changed

2 files changed

+102
-89
lines changed

src/board_controller/openbci/cyton_daisy.cpp

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void CytonDaisy::read_thread ()
2929
int res;
3030
unsigned char b[32];
3131
double package[30] = {0.};
32-
bool first_sample = false;
32+
bool first_sample = true;
3333
double accel[3] = {0.};
3434
while (keep_alive)
3535
{
@@ -64,14 +64,36 @@ void CytonDaisy::read_thread ()
6464
continue;
6565
}
6666

67+
// For Cyton Daisy Serial, sample IDs are sequenctial
68+
// (0, 1, 2, 3...) so even sample IDs are the first sample (daisy)
69+
// and odd sample IDs are the second sample (cyton)
70+
// after the second sample, we commit the package below
71+
first_sample = b[0] % 2 == 0;
72+
6773
// place unprocessed bytes to other_channels for all modes
68-
if ((b[0] % 2 == 0) && (first_sample))
74+
if (first_sample)
6975
{
76+
package[0] = (double)b[0];
7077
// eeg
7178
for (int i = 0; i < 8; i++)
7279
{
7380
package[i + 9] = eeg_scale * cast_24bit_to_int32 (b + 1 + 3 * i);
7481
}
82+
// other_channels
83+
package[21] = (double)b[25];
84+
package[22] = (double)b[26];
85+
package[23] = (double)b[27];
86+
package[24] = (double)b[28];
87+
package[25] = (double)b[29];
88+
package[26] = (double)b[30];
89+
}
90+
else
91+
{
92+
// eeg
93+
for (int i = 0; i < 8; i++)
94+
{
95+
package[i + 1] = eeg_scale * cast_24bit_to_int32 (b + 1 + 3 * i);
96+
}
7597
// need to average other_channels
7698
package[21] += (double)b[25];
7799
package[22] += (double)b[26];
@@ -87,23 +109,6 @@ void CytonDaisy::read_thread ()
87109
package[26] /= 2.0;
88110
package[20] = (double)b[31];
89111
}
90-
else
91-
{
92-
first_sample = true;
93-
package[0] = (double)b[0];
94-
// eeg
95-
for (int i = 0; i < 8; i++)
96-
{
97-
package[i + 1] = eeg_scale * cast_24bit_to_int32 (b + 1 + 3 * i);
98-
}
99-
// other_channels
100-
package[21] = (double)b[25];
101-
package[22] = (double)b[26];
102-
package[23] = (double)b[27];
103-
package[24] = (double)b[28];
104-
package[25] = (double)b[29];
105-
package[26] = (double)b[30];
106-
}
107112

108113
// place processed accel data
109114
if (b[31] == END_BYTE_STANDARD)
@@ -113,7 +118,18 @@ void CytonDaisy::read_thread ()
113118
accel_temp[1] = cast_16bit_to_int32 (b + 27);
114119
accel_temp[2] = cast_16bit_to_int32 (b + 29);
115120

116-
if ((b[0] % 2 == 0) && (first_sample))
121+
if (first_sample)
122+
{
123+
package[0] = (double)b[0];
124+
// accel
125+
if (accel_temp[0] != 0)
126+
{
127+
accel[0] = accel_scale * accel_temp[0];
128+
accel[1] = accel_scale * accel_temp[1];
129+
accel[2] = accel_scale * accel_temp[2];
130+
}
131+
}
132+
else
117133
{
118134
// need to average accel data
119135
if (accel_temp[0] != 0)
@@ -129,18 +145,6 @@ void CytonDaisy::read_thread ()
129145

130146
package[20] = (double)b[31];
131147
}
132-
else
133-
{
134-
first_sample = true;
135-
package[0] = (double)b[0];
136-
// accel
137-
if (accel_temp[0] != 0)
138-
{
139-
accel[0] = accel_scale * accel_temp[0];
140-
accel[1] = accel_scale * accel_temp[1];
141-
accel[2] = accel_scale * accel_temp[2];
142-
}
143-
}
144148

145149
package[17] = accel[0];
146150
package[18] = accel[1];
@@ -149,7 +153,15 @@ void CytonDaisy::read_thread ()
149153
// place processed analog data
150154
if (b[31] == END_BYTE_ANALOG)
151155
{
152-
if ((b[0] % 2 == 0) && (first_sample))
156+
if (first_sample)
157+
{
158+
package[0] = (double)b[0];
159+
// analog
160+
package[27] = cast_16bit_to_int32 (b + 25);
161+
package[28] = cast_16bit_to_int32 (b + 27);
162+
package[29] = cast_16bit_to_int32 (b + 29);
163+
}
164+
else
153165
{
154166
// need to average analog data
155167
package[27] += cast_16bit_to_int32 (b + 25);
@@ -160,18 +172,9 @@ void CytonDaisy::read_thread ()
160172
package[29] /= 2.0f;
161173
package[20] = (double)b[31]; // cyton end byte
162174
}
163-
else
164-
{
165-
first_sample = true;
166-
package[0] = (double)b[0];
167-
// analog
168-
package[27] = cast_16bit_to_int32 (b + 25);
169-
package[28] = cast_16bit_to_int32 (b + 27);
170-
package[29] = cast_16bit_to_int32 (b + 29);
171-
}
172175
}
173176
// commit package
174-
if ((b[0] % 2 == 0) && (first_sample))
177+
if (!first_sample)
175178
{
176179
double timestamp = get_timestamp ();
177180
db->add_data (timestamp, package);

src/board_controller/openbci/cyton_daisy_wifi.cpp

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ void CytonDaisyWifi::read_thread ()
4242
*/
4343
int res;
4444
unsigned char b[OpenBCIWifiShieldBoard::package_size];
45+
double package[30] = {0.};
46+
bool first_sample = true;
4547
double accel[3] = {0.};
48+
unsigned char last_sample_id = 0;
4649
while (keep_alive)
4750
{
4851
res = server_socket->recv (b, OpenBCIWifiShieldBoard::package_size);
@@ -60,11 +63,9 @@ void CytonDaisyWifi::read_thread ()
6063
continue;
6164
}
6265

63-
double package[30] = {0.};
64-
bool first_sample = false;
6566
if (b[0] != START_BYTE)
6667
{
67-
break; // drop entire transaction for daisy
68+
continue;
6869
}
6970
unsigned char *bytes = b + 1; // for better consistency between plain cyton and wifi, in
7071
// plain cyton index is shifted by 1
@@ -75,8 +76,34 @@ void CytonDaisyWifi::read_thread ()
7576
continue;
7677
}
7778

79+
80+
// For Cyton Daisy Wifi, sample IDs are repeated twice
81+
// (0, 0, 1, 1, 2, 2, 3, 3, ...) so when the sample id
82+
// changes, that's how we know it's the first sample
83+
if (last_sample_id != bytes[0])
84+
{
85+
first_sample = true;
86+
}
87+
last_sample_id = bytes[0];
88+
7889
// place unprocessed bytes to other_channels for all modes
79-
if ((bytes[0] % 2 == 0) && (first_sample))
90+
if (first_sample)
91+
{
92+
package[0] = (double)bytes[0];
93+
// eeg
94+
for (int i = 0; i < 8; i++)
95+
{
96+
package[i + 1] = eeg_scale * cast_24bit_to_int32 (bytes + 1 + 3 * i);
97+
}
98+
// other_channels
99+
package[21] = (double)bytes[25];
100+
package[22] = (double)bytes[26];
101+
package[23] = (double)bytes[27];
102+
package[24] = (double)bytes[28];
103+
package[25] = (double)bytes[29];
104+
package[26] = (double)bytes[30];
105+
}
106+
else
80107
{
81108
// eeg
82109
for (int i = 0; i < 8; i++)
@@ -98,23 +125,6 @@ void CytonDaisyWifi::read_thread ()
98125
package[26] /= 2.0;
99126
package[20] = (double)bytes[31];
100127
}
101-
else
102-
{
103-
first_sample = true;
104-
package[0] = (double)bytes[0];
105-
// eeg
106-
for (int i = 0; i < 8; i++)
107-
{
108-
package[i + 1] = eeg_scale * cast_24bit_to_int32 (bytes + 1 + 3 * i);
109-
}
110-
// other_channels
111-
package[21] = (double)bytes[25];
112-
package[22] = (double)bytes[26];
113-
package[23] = (double)bytes[27];
114-
package[24] = (double)bytes[28];
115-
package[25] = (double)bytes[29];
116-
package[26] = (double)bytes[30];
117-
}
118128

119129
// place processed accel data
120130
if (bytes[31] == END_BYTE_STANDARD)
@@ -124,7 +134,19 @@ void CytonDaisyWifi::read_thread ()
124134
accel_temp[1] = cast_16bit_to_int32 (bytes + 27);
125135
accel_temp[2] = cast_16bit_to_int32 (bytes + 29);
126136

127-
if ((bytes[0] % 2 == 0) && (first_sample))
137+
if (first_sample)
138+
{
139+
package[0] = (double)bytes[0];
140+
141+
// accel
142+
if (accel_temp[0] != 0)
143+
{
144+
accel[0] = accel_scale * accel_temp[0];
145+
accel[1] = accel_scale * accel_temp[1];
146+
accel[2] = accel_scale * accel_temp[2];
147+
}
148+
}
149+
else
128150
{
129151
// need to average accel data
130152
if (accel_temp[0] != 0)
@@ -140,19 +162,6 @@ void CytonDaisyWifi::read_thread ()
140162

141163
package[20] = (double)bytes[31];
142164
}
143-
else
144-
{
145-
first_sample = true;
146-
package[0] = (double)bytes[0];
147-
148-
// accel
149-
if (accel_temp[0] != 0)
150-
{
151-
accel[0] = accel_scale * accel_temp[0];
152-
accel[1] = accel_scale * accel_temp[1];
153-
accel[2] = accel_scale * accel_temp[2];
154-
}
155-
}
156165

157166
package[17] = accel[0];
158167
package[18] = accel[1];
@@ -161,7 +170,15 @@ void CytonDaisyWifi::read_thread ()
161170
// place processed analog data
162171
if (bytes[31] == END_BYTE_ANALOG)
163172
{
164-
if ((bytes[0] % 2 == 0) && (first_sample))
173+
if (first_sample)
174+
{
175+
package[0] = (double)bytes[0];
176+
// analog
177+
package[27] = cast_16bit_to_int32 (bytes + 25);
178+
package[28] = cast_16bit_to_int32 (bytes + 27);
179+
package[29] = cast_16bit_to_int32 (bytes + 29);
180+
}
181+
else
165182
{
166183
// need to average analog data
167184
package[27] += cast_16bit_to_int32 (bytes + 25);
@@ -172,22 +189,15 @@ void CytonDaisyWifi::read_thread ()
172189
package[29] /= 2.0f;
173190
package[20] = (double)bytes[31]; // cyton end byte
174191
}
175-
else
176-
{
177-
first_sample = true;
178-
package[0] = (double)bytes[0];
179-
// analog
180-
package[27] = cast_16bit_to_int32 (bytes + 25);
181-
package[28] = cast_16bit_to_int32 (bytes + 27);
182-
package[29] = cast_16bit_to_int32 (bytes + 29);
183-
}
184192
}
185193
// commit package
186-
if ((bytes[0] % 2 == 0) && (first_sample))
194+
if (!first_sample)
187195
{
188196
double timestamp = get_timestamp ();
189197
db->add_data (timestamp, package);
190198
streamer->stream_data (package, 30, timestamp);
191199
}
200+
201+
first_sample = false;
192202
}
193203
}

0 commit comments

Comments
 (0)