plotter
plotter.cpp@0:ed208b547ef9, 2016-10-30 (annotated)
- Committer:
- dicarloj
- Date:
- Sun Oct 30 19:37:33 2016 +0000
- Revision:
- 0:ed208b547ef9
- Child:
- 1:761371998f16
working, no safety
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dicarloj | 0:ed208b547ef9 | 1 | #include "mbed.h" |
dicarloj | 0:ed208b547ef9 | 2 | #include "plotter.h" |
dicarloj | 0:ed208b547ef9 | 3 | |
dicarloj | 0:ed208b547ef9 | 4 | volatile float* data_1; |
dicarloj | 0:ed208b547ef9 | 5 | volatile float* data_2; |
dicarloj | 0:ed208b547ef9 | 6 | size_t buffer_size; |
dicarloj | 0:ed208b547ef9 | 7 | volatile bool send_now; |
dicarloj | 0:ed208b547ef9 | 8 | volatile float* plot_ptr; |
dicarloj | 0:ed208b547ef9 | 9 | |
dicarloj | 0:ed208b547ef9 | 10 | Serial pc(USBTX, USBRX); |
dicarloj | 0:ed208b547ef9 | 11 | // |
dicarloj | 0:ed208b547ef9 | 12 | int samples_per_plot = -1; |
dicarloj | 0:ed208b547ef9 | 13 | int count = 0; |
dicarloj | 0:ed208b547ef9 | 14 | int plot_count = 0; |
dicarloj | 0:ed208b547ef9 | 15 | bool initialized = false; |
dicarloj | 0:ed208b547ef9 | 16 | void send_message() |
dicarloj | 0:ed208b547ef9 | 17 | { |
dicarloj | 0:ed208b547ef9 | 18 | count++; |
dicarloj | 0:ed208b547ef9 | 19 | //pc.printf("in send message...\n\r"); |
dicarloj | 0:ed208b547ef9 | 20 | if(!send_now) return; |
dicarloj | 0:ed208b547ef9 | 21 | //pc.printf("aaaaa...\n\r"); |
dicarloj | 0:ed208b547ef9 | 22 | |
dicarloj | 0:ed208b547ef9 | 23 | count = 0; |
dicarloj | 0:ed208b547ef9 | 24 | char start = 236; |
dicarloj | 0:ed208b547ef9 | 25 | //float data_6[] = {12.3456, 2.2, 33.4, 45.34, -52.34, 6, 734.234, 84.3, 99}; |
dicarloj | 0:ed208b547ef9 | 26 | ser_send(&pc, &start, 1); |
dicarloj | 0:ed208b547ef9 | 27 | ser_send(&pc, &start, 1); |
dicarloj | 0:ed208b547ef9 | 28 | ser_send(&pc, (void*)plot_ptr, buffer_size); |
dicarloj | 0:ed208b547ef9 | 29 | memset((void*)plot_ptr, 0, buffer_size); |
dicarloj | 0:ed208b547ef9 | 30 | send_now = false; |
dicarloj | 0:ed208b547ef9 | 31 | } |
dicarloj | 0:ed208b547ef9 | 32 | |
dicarloj | 0:ed208b547ef9 | 33 | void init_plotter(int num_vars, int loop_freq, int plot_freq) |
dicarloj | 0:ed208b547ef9 | 34 | { |
dicarloj | 0:ed208b547ef9 | 35 | pc.baud(115200); |
dicarloj | 0:ed208b547ef9 | 36 | |
dicarloj | 0:ed208b547ef9 | 37 | buffer_size = 3 * num_vars * sizeof(float); |
dicarloj | 0:ed208b547ef9 | 38 | send_now = false; |
dicarloj | 0:ed208b547ef9 | 39 | data_1 = (volatile float*)malloc(buffer_size); |
dicarloj | 0:ed208b547ef9 | 40 | data_2 = (volatile float*)malloc(buffer_size); |
dicarloj | 0:ed208b547ef9 | 41 | memset((void*)data_1, 0, 3 * num_vars * sizeof(float)); |
dicarloj | 0:ed208b547ef9 | 42 | memset((void*)data_2, 0, 3 * num_vars * sizeof(float)); |
dicarloj | 0:ed208b547ef9 | 43 | samples_per_plot = loop_freq/plot_freq; |
dicarloj | 0:ed208b547ef9 | 44 | buffer_size = 3 * num_vars * sizeof(float); |
dicarloj | 0:ed208b547ef9 | 45 | if(samples_per_plot < 1) |
dicarloj | 0:ed208b547ef9 | 46 | printf("Bad inputs for plooter. try again next time.\n\r"); |
dicarloj | 0:ed208b547ef9 | 47 | else |
dicarloj | 0:ed208b547ef9 | 48 | initialized = true; |
dicarloj | 0:ed208b547ef9 | 49 | } |
dicarloj | 0:ed208b547ef9 | 50 | |
dicarloj | 0:ed208b547ef9 | 51 | void plot(int index_v, float value) |
dicarloj | 0:ed208b547ef9 | 52 | { |
dicarloj | 0:ed208b547ef9 | 53 | volatile float* data_ptr = (plot_count % 2) == 0 ? data_1 : data_2; |
dicarloj | 0:ed208b547ef9 | 54 | if(!initialized) |
dicarloj | 0:ed208b547ef9 | 55 | { |
dicarloj | 0:ed208b547ef9 | 56 | //printf("plotter not initialized, not plotting!\n\r"); |
dicarloj | 0:ed208b547ef9 | 57 | return; |
dicarloj | 0:ed208b547ef9 | 58 | } |
dicarloj | 0:ed208b547ef9 | 59 | data_ptr[3*index_v] += value/(samples_per_plot + 1); |
dicarloj | 0:ed208b547ef9 | 60 | data_ptr[3*index_v + 1] = 987;//fmaxf(data_ptr[3*index_v + 1], value); |
dicarloj | 0:ed208b547ef9 | 61 | data_ptr[3*index_v + 2] = 236;//fminf(data_ptr[3*index_v + 2], value); |
dicarloj | 0:ed208b547ef9 | 62 | //printf("count %d, samples per plot %d \n\r", count, samples_per_plot); |
dicarloj | 0:ed208b547ef9 | 63 | |
dicarloj | 0:ed208b547ef9 | 64 | if(count > samples_per_plot) |
dicarloj | 0:ed208b547ef9 | 65 | { |
dicarloj | 0:ed208b547ef9 | 66 | plot_ptr = data_ptr; |
dicarloj | 0:ed208b547ef9 | 67 | send_now = true; |
dicarloj | 0:ed208b547ef9 | 68 | plot_count++; |
dicarloj | 0:ed208b547ef9 | 69 | } |
dicarloj | 0:ed208b547ef9 | 70 | |
dicarloj | 0:ed208b547ef9 | 71 | } |