Skip to content

Commit b0f4252

Browse files
authored
Merge pull request #22 from thodob/ultrasound_wifi
Added UltraSound WiFi example
2 parents 4f2c8ec + c88f7ee commit b0f4252

File tree

8 files changed

+465
-2
lines changed

8 files changed

+465
-2
lines changed

docs/Library/Others.md

Lines changed: 216 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ These examples illustrate more complex, yet fundamental, applications of multipl
1717

1818
## Basic Ultrasound Examples
1919

20-
These examples demonstrate how to transmit a 32kHz ultrasonic wave at a constant sampling rate. The reflected wave is then captured and sent to a PC via Serial communication. Using MATLAB, you can visualize and potentially process the wave. By placing an object on top of the SensEdu, you can observe wave reflections.
20+
These examples demonstrate how to transmit a 32kHz ultrasonic wave at a constant sampling rate. The reflected wave is then captured and sent to a PC via Serial or WiFi communication. Using MATLAB, you can visualize and potentially process the wave. By placing an object on top of the SensEdu, you can observe wave reflections.
2121

2222
{: .IMPORTANT}
2323
To run these examples, you need to install [MATLAB].
@@ -163,7 +163,7 @@ DATA_LENGTH = 16*128; % ensure this value matches `mic_data_size` in firmware
163163
arduino = serialport(ARDUINO_PORT, ARDUINO_BAUDRATE);
164164
```
165165

166-
**Step 11**{: .text-blue-000} : Write a function in MATLAB to receive data from Arduino. The function should handle 16-bit samples sent in bytes, so each sample consisting of 2 bytes. In the firmware, data is sent in 32-byte chunks, and the MATLAB function should match this to avoid synchronization issues.
166+
**Step 11**{: .text-blue-000} : Write a function in MATLAB to receive the data from Arduino. The function should handle 16-bit samples sent in bytes, so each sample consisting of 2 bytes. In the firmware, data is sent in 32-byte chunks, and the MATLAB function should match this to avoid synchronization issues.
167167

168168
```matlab
169169
function data = read_data(arduino, data_length)
@@ -303,6 +303,220 @@ Second figure shows measurements with a flat object placed near the board. Here,
303303

304304
<img src="{{site.baseurl}}/assets/images/basic_ultrasound_4ch_object.gif" alt="drawing"/>
305305

306+
### Basic_UltraSound_WiFi
307+
308+
Utilizes only one microphone. The example is similar to [Basic_UltraSound]({% link Library/Others.md %}#basic_ultrasound), but uses WiFi as communication medium instead of Serial.
309+
310+
**Step 1**{: .text-blue-000} : Include SensEdu and WiFi libraries.
311+
312+
```c
313+
#include "SensEdu.h"
314+
#include <WiFi.h>
315+
```
316+
317+
**Step 2**{: .text-blue-000} : Create a lookup table (LUT) that contains a sine wave.
318+
319+
```c
320+
const uint16_t dac_cycle_num = 10; // sine cycles
321+
322+
const uint16_t sine_lut_size = 64; // sine wave size
323+
const SENSEDU_DAC_BUFFER(sine_lut, sine_lut_size) = {
324+
0x0000,0x000a,0x0027,0x0058,0x009c,0x00f2,0x0159,0x01d1,
325+
0x0258,0x02ed,0x038e,0x043a,0x04f0,0x05ad,0x0670,0x0737,
326+
0x0800,0x08c8,0x098f,0x0a52,0x0b0f,0x0bc5,0x0c71,0x0d12,
327+
0x0da7,0x0e2e,0x0ea6,0x0f0d,0x0f63,0x0fa7,0x0fd8,0x0ff5,
328+
0x0fff,0x0ff5,0x0fd8,0x0fa7,0x0f63,0x0f0d,0x0ea6,0x0e2e,
329+
0x0da7,0x0d12,0x0c71,0x0bc5,0x0b0f,0x0a52,0x098f,0x08c8,
330+
0x0800,0x0737,0x0670,0x05ad,0x04f0,0x043a,0x038e,0x02ed,
331+
0x0258,0x01d1,0x0159,0x00f2,0x009c,0x0058,0x0027,0x000a
332+
};
333+
```
334+
335+
**Step 3**{: .text-blue-000} : Initialize the `SensEdu_DAC_Settings` struct with DAC parameters. Refer to [DAC page]({% link Library/DAC.md %}#sensedu_dac_settings) for details.
336+
337+
```c
338+
#define DAC_SINE_FREQ 32000 // 32kHz
339+
#define DAC_SAMPLE_RATE DAC_SINE_FREQ * sine_lut_size // 64 samples per one sine cycle
340+
341+
DAC_Channel* dac_ch = DAC_CH1;
342+
SensEdu_DAC_Settings dac_settings = {
343+
.dac_channel = dac_ch,
344+
.sampling_freq = DAC_SAMPLE_RATE,
345+
.mem_address = (uint16_t*)sine_lut,
346+
.mem_size = sine_lut_size,
347+
.wave_mode = SENSEDU_DAC_MODE_BURST_WAVE,
348+
.burst_num = dac_cycle_num
349+
};
350+
```
351+
352+
**Step 4**{: .text-blue-000} : Initialize the `SensEdu_ADC_Settings` struct with ADC parameters. Refer to [ADC page]({% link Library/ADC.md %}#sensedu_adc_settings) for details. Array for ADC must have the proper size and be cache aligned, you can read more about this [here]({% link Library/ADC.md %}#cache-coherence).
353+
354+
```c
355+
const uint16_t mic_data_size = 16*128; // must be multiple of 16 for 16bit
356+
__attribute__((aligned(__SCB_DCACHE_LINE_SIZE))) uint16_t mic_data[mic_data_size];
357+
358+
ADC_TypeDef* adc = ADC1;
359+
const uint8_t mic_num = 1;
360+
uint8_t mic_pins[mic_num] = {A1};
361+
SensEdu_ADC_Settings adc_settings = {
362+
.adc = adc,
363+
.pins = mic_pins,
364+
.pin_num = mic_num,
365+
366+
.conv_mode = SENSEDU_ADC_MODE_CONT_TIM_TRIGGERED,
367+
.sampling_freq = 250000,
368+
369+
.dma_mode = SENSEDU_ADC_DMA_CONNECT,
370+
.mem_address = (uint16_t*)mic_data,
371+
.mem_size = mic_data_size
372+
};
373+
```
374+
375+
**Step 5**{: .text-blue-000} : Set SSID and password for your WiFi network, define a WiFi server on port 80 and a variable for its status.
376+
377+
```c
378+
char *ssid = "TestWifi";
379+
char *pass = "test1234";
380+
uint16_t port = 80;
381+
382+
int status = WL_IDLE_STATUS;
383+
384+
WiFiServer server(port);
385+
```
386+
387+
**Step 6**{: .text-blue-000} : Initialize DAC and ADC with created structs. Enable Serial for communication with PC (for retrieving the boards IP later). Attempt connection with the previously defined WiFi network.
388+
389+
```c
390+
void setup() {
391+
Serial.begin(115200);
392+
393+
SensEdu_DAC_Init(&dac_settings);
394+
SensEdu_ADC_Init(&adc_settings);
395+
SensEdu_ADC_Enable(adc);
396+
397+
// attempt connection to WiFi network
398+
while (status != WL_CONNECTED) {
399+
Serial.println(ssid);
400+
// connect to WPA/WPA2 network (change this if youre using open / WEP network)
401+
status = WiFi.begin(ssid, pass);
402+
403+
// wait 10 seconds for connection
404+
delay(10000);
405+
}
406+
server.begin();
407+
}
408+
```
409+
410+
**Step 6**{: .text-blue-000} : Create a buffer that waits for a trigger command (symbol "t") from MATLAB.
411+
Print the board's IP-address in a serial monitor, as it will be needed later.
412+
413+
```c
414+
void loop() {
415+
416+
WiFiClient client = server.available();
417+
Serial.println("IP Address: ");
418+
Serial.print(WiFi.localIP());
419+
420+
if (client) {
421+
Serial.println("Client connected!");
422+
static char buf = 0;
423+
424+
while(client.connected()){
425+
if (client.available()) {
426+
buf = client.read();
427+
if(buf == 't') {
428+
// trigger detected -> initiate measurements
429+
// do stuff
430+
// transmit data to PC
431+
}
432+
}
433+
}
434+
}
435+
436+
}
437+
```
438+
439+
**Step 7**{: .text-blue-000} : Start the transmission of the sine wave and wait until it is completed.
440+
441+
```c
442+
SensEdu_DAC_Enable(dac_ch);
443+
while(!SensEdu_DAC_GetBurstCompleteFlag(dac_ch));
444+
SensEdu_DAC_ClearBurstCompleteFlag(dac_ch);
445+
```
446+
447+
**Step 8**{: .text-blue-000} : Start the data acquisition and wait until it is completed.
448+
449+
```c
450+
while(!SensEdu_ADC_GetTransferStatus(adc));
451+
SensEdu_ADC_ClearTransferStatus(adc);
452+
```
453+
454+
**Step 9**{: .text-blue-000} : Send the received wave data to MATLAB via Serial.
455+
456+
```markdown
457+
wifi_send_array((const uint8_t *) & mic_data, mic_data_size << 1, client);
458+
} // close if(buf == 't')
459+
```
460+
```c
461+
// send data over WiFi in 32 byte chunks
462+
void wifi_send_array(const uint8_t* data, size_t size, WiFiClient client) {
463+
const size_t chunk_size = 32;
464+
for (uint32_t i = 0; i < size/chunk_size; i++) {
465+
client.write(data + chunk_size * i, chunk_size);
466+
}
467+
}
468+
```
469+
470+
**Step 10**{: .text-blue-000} : Open `matlab\Basic_UltraSound_WiFi_ReadData.m`. Specify the required parameters and start the connection with Arduino.
471+
472+
```matlab
473+
%% Settings
474+
ARDUINO_IP = 'XXX.XXX.XXX.XXX'; % match to Arduino IP
475+
ARDUINO_PORT = 80; % match to port of Arduino server
476+
ITERATIONS = 10000; % script stops after this number of measurements
477+
DATA_LENGTH = 16*128; % ensure this value matches `mic_data_size` in firmware
478+
479+
%% Arduino Setup
480+
arduino_server = tcpclient(ARDUINO_IP, ARDUINO_PORT);
481+
```
482+
483+
**Step 11**{: .text-blue-000} : Write a function in MATLAB to receive the data from Arduino. The function should handle 16-bit samples sent in bytes, so each sample consisting of 2 bytes. In the firmware, data is sent in 32-byte chunks, and the MATLAB function should match this to avoid synchronization issues.
484+
485+
```matlab
486+
function data = read_data(arduino_server, data_length)
487+
total_byte_length = data_length * 2; % 2 bytes per sample
488+
serial_rx_data = zeros(1, total_byte_length);
489+
490+
for i = 1:(total_byte_length/32) % 32 bytes chunk size
491+
serial_rx_data((32*i - 31):(32*i)) = read(arduino_server, 32, 'uint8');
492+
end
493+
494+
data = double(typecast(uint8(serial_rx_data), 'uint16'));
495+
end
496+
```
497+
498+
**Step 12**{: .text-blue-000} : Create the main loop in MATLAB to send the trigger symbol "t" to the Arduino, read the data, and plot it.
499+
500+
```matlab
501+
%% Readings Loop
502+
data = zeros(1,ITERATIONS);
503+
504+
for it = 1:ITERATIONS
505+
write(arduino_server, 't', "char"); % trigger arduino measurement
506+
data = read_data(arduino_server, DATA_LENGTH);
507+
plot(data);
508+
end
509+
```
510+
511+
**Step 13**{: .text-blue-000} : Connect your PC to the same WiFi network and run the script.
512+
513+
![]({{site.baseurl}}/assets/images/PCWiFiConnection.png)
514+
515+
**Results**{: .text-blue-000} : Below is a figure showing the measurement results.
516+
517+
Notice that with WiFi, your Serial is freed up, allowing to use it for convenient debugging purposes on Arduino or to send data in parallel to the WiFi connection.
518+
519+
![]({{site.baseurl}}/assets/images/WiFiConnection.png)
306520

307521
[STM32H747 Reference Manual]: https://www.st.com/resource/en/reference_manual/rm0399-stm32h745755-and-stm32h747757-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
308522

30 KB
Loading

docs/assets/images/WiFiConnection.png

32.5 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.tabSize": 4
3+
}

0 commit comments

Comments
 (0)