A demonstration of the Telemetry library that sends the contents of an array over a single topic 'sin'. The array is a shifting 16-samples window on a sine function
Dependencies: BufferedSerial mbed telemetry
main.cpp@1:22e495480269, 2016-03-09 (annotated)
- Committer:
- Overdrivr
- Date:
- Wed Mar 09 15:49:10 2016 +0000
- Revision:
- 1:22e495480269
- Parent:
- 0:2e15d72bfbcf
update headers include
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Overdrivr | 0:2e15d72bfbcf | 1 | #include "mbed.h" |
Overdrivr | 1:22e495480269 | 2 | #include "Telemetry.hpp" |
Overdrivr | 0:2e15d72bfbcf | 3 | |
Overdrivr | 0:2e15d72bfbcf | 4 | /* |
Overdrivr | 0:2e15d72bfbcf | 5 | Example of the 'Telemetry' library, a portable communication library for embedded devices. |
Overdrivr | 0:2e15d72bfbcf | 6 | |
Overdrivr | 0:2e15d72bfbcf | 7 | This code shows how you can use the powerful topic adressing of the library |
Overdrivr | 0:2e15d72bfbcf | 8 | to send complex, indexed and sparse data such as arrays, lists, etc. |
Overdrivr | 0:2e15d72bfbcf | 9 | |
Overdrivr | 0:2e15d72bfbcf | 10 | You can use the Pytelemetry Command Line Interface to open plots, visualize the received data, |
Overdrivr | 0:2e15d72bfbcf | 11 | and communicate with the car. |
Overdrivr | 0:2e15d72bfbcf | 12 | See https://github.com/Overdrivr/pytelemetrycli |
Overdrivr | 0:2e15d72bfbcf | 13 | */ |
Overdrivr | 0:2e15d72bfbcf | 14 | |
Overdrivr | 0:2e15d72bfbcf | 15 | int main() |
Overdrivr | 0:2e15d72bfbcf | 16 | { |
Overdrivr | 0:2e15d72bfbcf | 17 | Telemetry TM(115200); |
Overdrivr | 0:2e15d72bfbcf | 18 | |
Overdrivr | 0:2e15d72bfbcf | 19 | Timer refresh_timer; |
Overdrivr | 0:2e15d72bfbcf | 20 | refresh_timer.start(); |
Overdrivr | 0:2e15d72bfbcf | 21 | |
Overdrivr | 0:2e15d72bfbcf | 22 | // The topic under which the array is published |
Overdrivr | 0:2e15d72bfbcf | 23 | char topic [] = "sin:000"; |
Overdrivr | 0:2e15d72bfbcf | 24 | |
Overdrivr | 0:2e15d72bfbcf | 25 | // The sent array contains 16 elements |
Overdrivr | 0:2e15d72bfbcf | 26 | // Each element takes the value of a sine function, with a phase difference between each element |
Overdrivr | 0:2e15d72bfbcf | 27 | // In the command-line interface, the generated data will visualize as a moving sine wave |
Overdrivr | 0:2e15d72bfbcf | 28 | |
Overdrivr | 0:2e15d72bfbcf | 29 | // Time for simulating sine function |
Overdrivr | 0:2e15d72bfbcf | 30 | float t = 0; |
Overdrivr | 0:2e15d72bfbcf | 31 | |
Overdrivr | 0:2e15d72bfbcf | 32 | for(;;) |
Overdrivr | 0:2e15d72bfbcf | 33 | { |
Overdrivr | 0:2e15d72bfbcf | 34 | // 10 times per second |
Overdrivr | 0:2e15d72bfbcf | 35 | if(refresh_timer.read_ms() > 100) |
Overdrivr | 0:2e15d72bfbcf | 36 | { |
Overdrivr | 0:2e15d72bfbcf | 37 | refresh_timer.reset(); |
Overdrivr | 0:2e15d72bfbcf | 38 | |
Overdrivr | 0:2e15d72bfbcf | 39 | // Compute a 16 points window on a sine function that shifts in time |
Overdrivr | 0:2e15d72bfbcf | 40 | for(uint16_t i = 0 ; i < 16 ; i++) |
Overdrivr | 0:2e15d72bfbcf | 41 | { |
Overdrivr | 0:2e15d72bfbcf | 42 | // Compute sinus of point i |
Overdrivr | 0:2e15d72bfbcf | 43 | float s = sin(2 * 3.14159 * t + i/10.0); |
Overdrivr | 0:2e15d72bfbcf | 44 | |
Overdrivr | 0:2e15d72bfbcf | 45 | // Build the topic |
Overdrivr | 0:2e15d72bfbcf | 46 | // First topic will be 'sin:0' |
Overdrivr | 0:2e15d72bfbcf | 47 | // Second topic will be 'sin:1', etc |
Overdrivr | 0:2e15d72bfbcf | 48 | sprintf(topic,"sin:%u",i); |
Overdrivr | 0:2e15d72bfbcf | 49 | |
Overdrivr | 0:2e15d72bfbcf | 50 | // Publish sine value at index 'i' to topic 'sin:i' |
Overdrivr | 0:2e15d72bfbcf | 51 | TM.pub_f32(topic,s); |
Overdrivr | 0:2e15d72bfbcf | 52 | } |
Overdrivr | 0:2e15d72bfbcf | 53 | |
Overdrivr | 0:2e15d72bfbcf | 54 | // Move time forward |
Overdrivr | 0:2e15d72bfbcf | 55 | t += 0.005; |
Overdrivr | 0:2e15d72bfbcf | 56 | } |
Overdrivr | 0:2e15d72bfbcf | 57 | } |
Overdrivr | 0:2e15d72bfbcf | 58 | } |