|
1 | 1 | /**************************************************************************** |
2 | 2 | * examples/telemetry/telemetry_main.c |
3 | 3 | * |
4 | | - * Copyright (C) 2018 Erle Robotics (Juan Flores Muñoz). All rights reserved. |
5 | | - * Author: Erle Robotics (Juan Flores Muñoz) <juan@erlerobotics.com> |
| 4 | + * Copyright (C) 2018 Acutronics Link Robotics. All rights reserved. |
| 5 | + * Author: Acutronics Link Robotics <juan@erlerobotics.com> |
6 | 6 | * |
7 | 7 | * Redistribution and use in source and binary forms, with or without |
8 | 8 | * modification, are permitted provided that the following conditions |
|
33 | 33 | * |
34 | 34 | ****************************************************************************/ |
35 | 35 |
|
36 | | - /**************************************************************************** |
37 | | - * Included Files |
38 | | - ****************************************************************************/ |
39 | | - |
40 | | - #include <nuttx/config.h> |
41 | | - #include <stdio.h> |
42 | | - #include <stdlib.h> |
43 | | - #include <fcntl.h> |
44 | | - #include <unistd.h> |
45 | | - #include <nuttx/sensors/ina219.h> |
46 | | - #include <string.h> |
47 | | - |
48 | | - /**************************************************************************** |
49 | | - * Public Functions |
50 | | - ****************************************************************************/ |
51 | | - |
52 | | - /**************************************************************************** |
53 | | - * Variables |
54 | | - ****************************************************************************/ |
55 | | - struct ina219_s sample; |
56 | | - |
57 | | - /**************************************************************************** |
58 | | - * hello_main |
59 | | - ****************************************************************************/ |
60 | | - |
61 | | - #ifdef CONFIG_BUILD_KERNEL |
62 | | - int main(int argc, FAR char *argv[]) |
63 | | - #else |
64 | | - int telemetry_main(int argc, char *argv[]) |
65 | | - #endif |
66 | | - { |
67 | | - //File descriptors |
68 | | - FILE *fd_save; |
69 | | - FILE *fd_sensor; |
70 | | - FILE *fd_cpu; |
71 | | - FILE *fd_mem; |
72 | | - |
73 | | - //Buffers |
74 | | - char cpuload_buf[6]; |
75 | | - char meminfo_buf[256]; |
76 | | - char buffer[256]; |
77 | | - char buffer_2[256]; |
78 | | - |
79 | | - //Auxilary Variables |
80 | | - int i=0; |
81 | | - char aux_c; |
82 | | - int n=0; |
83 | | - char aux[10]; |
84 | | - int iterations=0; |
85 | | - int iterations_counter=0; |
86 | | - |
87 | | - //Checking the arguments of the function. |
88 | | - if(argc<4){ |
89 | | - //It's neccesary almost the name of the file |
90 | | - printf("Correct: telemetry file.txt number_measures show_option\n"); |
91 | | - printf("If you want a continue measure, write i as argument \n"); |
92 | | - printf("The available options are:\n s (to save in the sd card)\n"); |
93 | | - printf("c (to see the data in the console) \n b (both modes)\n"); |
94 | | - return -1; |
95 | | - } |
96 | | - else if(argc==4){ |
97 | | - //This is to check the name of the file |
98 | | - sprintf(buffer,"/mnt/%s",argv[1]); |
99 | | - sprintf(buffer_2,"%s",argv[2]); |
100 | | - |
101 | | - int num=atoi(argv[2]); |
102 | | - //To set the number of iterations |
103 | | - if(num>1 && num<=500){ |
104 | | - iterations=num; |
105 | | - } |
106 | | - else if(strcmp(buffer_2,"i")==0){ |
107 | | - iterations=-1; |
108 | | - } |
109 | | - else{ |
110 | | - printf("Error, must be 1 to 500 iterations or i to continue measurement\n"); |
111 | | - return -1; |
112 | | - } |
113 | | - |
114 | | - } |
115 | | - else{ |
116 | | - printf("Too much arguments\n"); |
117 | | - printf("Correct: telemetry file.txt number_measures show_option\n"); |
118 | | - printf("If you want a continue measure, write i as argument \n"); |
119 | | - printf("The available options are:\n s (to save in the sd card)\n"); |
120 | | - printf("c (to see the data in the console) \n b (both modes)\n"); |
121 | | - return -1; |
122 | | - } |
123 | | - |
124 | | - |
125 | | - |
126 | | - //Commands to mount the file system of the SD card and the data of the board |
127 | | - system("mount -t vfat /dev/mmcsd0 /mnt"); |
128 | | - system("mount -t procfs /proc"); |
129 | | - |
130 | | - //Opening the Files |
131 | | - fd_save = fopen( buffer , "w" ); |
132 | | - fd_cpu = fopen("/proc/cpuload", "r"); |
133 | | - fd_mem = fopen("/proc/meminfo", "r"); |
134 | | - fd_sensor = open("/dev/ina219", O_RDWR); |
135 | | - |
136 | | - if(fd_save < 0 || fd_cpu < 0 || fd_mem < 0|| fd_sensor < 0){ |
137 | | - printf("Error opening file\n"); |
138 | | - return -1; |
139 | | - } |
140 | | - |
141 | | - //This loop gets the data from the sensor and from the files, then it |
142 | | - //construct the message, and finally it save in the file |
143 | | - printf("Starting telemetry SD\n"); |
144 | | - /*buffer="New Measure\n";//This is just in case we use the same file |
145 | | - fwrite(aux_buffer , 1 , n , fd_save );*/ |
146 | | - |
147 | | - while(iterations!=iterations_counter){ |
148 | | - //Reading the value of sensor |
149 | | - read(fd_sensor, &sample, sizeof(sample)); |
150 | | - //getting the data from the CPU |
151 | | - while(1){ |
152 | | - aux_c = fgetc(fd_cpu); |
153 | | - if( feof(fd_cpu) ) break; |
154 | | - cpuload_buf[i]=aux_c; |
155 | | - i++; |
156 | | - } |
157 | | - i=0; |
158 | | - //getting the data from the CPU |
159 | | - while(1){ |
160 | | - aux_c = fgetc(fd_mem); |
161 | | - if( feof(fd_mem) ) break; |
162 | | - meminfo_buf[i]=aux_c; |
163 | | - i++; |
164 | | - } |
165 | | - //With this loop, we look for the value of used SRAM |
166 | | - for(i=0;i<10;i++){ |
167 | | - aux[i]=meminfo_buf[76+i]; |
168 | | - } |
169 | | - i=127072 - atoi(aux); |
170 | | - //Creating the message |
171 | | - n=sprintf(buffer,"V: %4u mV I: %4u mA CPU: %s Free SRAM: %d Bytes\n", |
172 | | - sample.voltage,sample.current,cpuload_buf,i); |
173 | | - //Writing to the file |
174 | | - sprintf(buffer_2,"%s",argv[3]); |
175 | | - if(strcmp(buffer_2,"s")==0){ |
176 | | - //Save in the SD |
177 | | - fwrite(buffer , 1 , n , fd_save ); |
178 | | - } |
179 | | - else if(strcmp(buffer_2,"c")==0){ |
180 | | - //Show in the console |
181 | | - printf(buffer); |
182 | | - } |
183 | | - else{ |
184 | | - fwrite(buffer , 1 , n , fd_save ); |
185 | | - printf(buffer); |
186 | | - } |
187 | | - |
188 | | - usleep(200); |
189 | | - iterations_counter++; |
190 | | - } |
191 | | - |
192 | | - //Closing the files |
193 | | - fclose(fd_save); |
194 | | - fclose(fd_cpu); |
195 | | - fclose(fd_mem); |
196 | | - close(fd_sensor); |
197 | | - return 0; |
198 | | - } |
| 36 | +/**************************************************************************** |
| 37 | + * Included Files |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +#include <fcntl.h> |
| 41 | +#include <math.h> |
| 42 | +#include <nuttx/config.h> |
| 43 | +#include <stdio.h> |
| 44 | +#include <stdio.h> |
| 45 | +#include <stdlib.h> |
| 46 | +#include <string.h> |
| 47 | +#include <unistd.h> |
| 48 | + |
| 49 | +/**************************************************************************** |
| 50 | + * Public Functions |
| 51 | + ****************************************************************************/ |
| 52 | + |
| 53 | +/**************************************************************************** |
| 54 | + * Variables |
| 55 | + ****************************************************************************/ |
| 56 | + |
| 57 | +int a = 0; |
| 58 | +float e = 0; |
| 59 | + |
| 60 | +char cpuload_buf[256]; |
| 61 | +int aux_c; |
| 62 | +int lSize; |
| 63 | +char *buffer; |
| 64 | + |
| 65 | +/**************************************************************************** |
| 66 | + * hello_main |
| 67 | + ****************************************************************************/ |
| 68 | + |
| 69 | +#ifdef CONFIG_BUILD_KERNEL |
| 70 | +int main(int argc, FAR char *argv[]) |
| 71 | +#else |
| 72 | +int telemetry_main(int argc, char *argv[]) |
| 73 | +#endif |
| 74 | +{ |
| 75 | + int i = 0; |
| 76 | + printf("Mounting file system \n\n"); |
| 77 | + system("mount -t procfs /proc"); |
| 78 | + |
| 79 | + FILE *fd_cpu; |
| 80 | + fd_cpu = fopen("/proc/cpuload", "r"); |
| 81 | + if (fd_cpu < 0) return -1; |
| 82 | + sleep(1); |
| 83 | + |
| 84 | + printf("Pushing the CPU to the limits\n"); |
| 85 | + printf("We will do the next floating operation: \n"); |
| 86 | + printf("e=3.141592653589793\n"); |
| 87 | + printf("e=e*e\n"); |
| 88 | + printf("This operation 100000 times\n"); |
| 89 | + // sleep(5); |
| 90 | + e = 3.141592653589793; |
| 91 | + |
| 92 | + while (i < 100000) { |
| 93 | + e = e * e; |
| 94 | + printf("="); |
| 95 | + i++; |
| 96 | + } |
| 97 | + printf("\n"); |
| 98 | + printf("\nMax CPU load achieve: \n"); |
| 99 | + system("cat /proc/cpuload"); |
| 100 | + |
| 101 | + printf("\nCPU performace test finis!\n"); |
| 102 | + |
| 103 | + printf("Showing interruptions\n\n"); |
| 104 | + system("cat /proc/irqs"); |
| 105 | + sleep(2); |
| 106 | + |
| 107 | + printf("Memory ussage\n\n"); |
| 108 | + system("cat /proc/meminfo"); |
| 109 | + sleep(2); |
| 110 | + |
| 111 | + printf("Stack of the application\n\n"); |
| 112 | + system("cat /proc/self/stack"); |
| 113 | + sleep(2); |
| 114 | + |
| 115 | + printf("Status of the running task\n\n"); |
| 116 | + system("cat /proc/self/status"); |
| 117 | + sleep(2); |
| 118 | + |
| 119 | + printf("Type cat /proc/self/status\n"); |
| 120 | + printf("To see the context change after running this app\n"); |
| 121 | + return 0; |
| 122 | + |
| 123 | + fclose(fd_cpu); |
| 124 | +} |
0 commit comments