A demás de que me lea un libro de programación en C++, que ya lo estoy haciendo.
Hola asesorplaza1.
Finalmente he podido descomprimir los códigos fuente en Windows y los he revisado.
En mi opinión, no hay una sola forma buena de programar, de hecho cada persona tiene un estilo propio que no tiene que ser ni mejor ni peor. Por lo que intentaré explicar cómo haría yo aquellas cosa que me llamen la atención. El código es bastante legible y veo que has seguido el estilo que recomienda Arduino así que es un buen punto de partida.
Una de las cosas que he visto y que por lo que comentas en post anteriores no te funciona bien es la comunicación.
Voy a dar un poco de contexto:
El bucle loop() se está ejecutando continuamente una vez arrancado el micro controlador, empieza por la primera línea y va realizando las tareas que le manda el código que hemos escrito hasta llegar al final y comienza de nuevo. Dependiendo del tamaño del código y de la velocidad del micro controlador esto se repite miles de veces por segundo.
La función delay() interrumpe la ejecución del código por un determinado periodo de tiempo (es como si le damos al pause).
La función while() Interrumpe la ejecución del código hasta que se cumple la condición que hemos puesto entre paréntesis (otro pause).
Los puertos serial tienen un buffer donde se van acumulando los datos que reciben, si la cantidad de datos es mayor que el tamaño del buffer se bloquean.
Este es el código relevante dentro de loop():
Maestro
474 ETout.sendData();
476 delay (200);
// Recibe el mensaje al puerto serie para el Arduino del ROV
479 while (!ETin.receiveData());
619 delay (1000);
Esclavo
334 while (!ETin.receiveData());
711 ETout.sendData // Esta dentro de la función loop_SKU237545_E()
460 delay(200);
La secuencia del código seria:
El maestro envía un mensaje (L 474). Se queda en pause 200ms (L 476). Pause hasta recibir datos del esclavo y pause de 1000ms. El loop() se ejecuta completo una vez cada 1.2 segundos y algunas milésimas.
El esclavo esta en pause hasta que recibe un mensaje (L334). Envía un mensaje y se queda en pause 200ms (L 460). Como el maestro envía un mensaje cada 1.2s, el loop() del esclavo se ejecuta completo cada 1.4s mas algunas milésimas.
Como resultado tanto el esclavo como el maestro están en pause el 99.99% del tiempo.
Yo utilizaría la función millis() en vez de delay() para aquellas partes del código que quiero que se ejecuten cada cierto tiempo y no bloquear la ejecución del resto del código.
También sustituiría
while (!ETin.receiveData()); por if(ETin.receiveData()) { código para procesar el mensaje ; }
De esta forma el código solo se ejecuta cuando hay un mensaje y se comprueba si hay menaje cada pocos milisegundos de forma que evitamos que el buffer se llene.
Tsanshon
¿Qué te parece?
Un saludo
A colleague from another forum has told me all this.
Other than reading a programming book in C++, I'm already doing it
Hello advisorplaza1.
I've finally been able to unzip the source codes in Windows and reviewed them.
In my opinion, there is not a single good way to program, in fact every person has a style of their own that doesn't have to be better or worse. So I'll try to explain how I'd do those things that catch my eye. The code is quite readable and I see that you have followed the style recommended by Arduino so it is a good starting point.
One of the things I've seen and that from what you review in previous posts doesn't work well for you is communication.
I'm going to give you some context:
The loop() is running continuously once the micro controller is booted, starts with the first line and goes performing the tasks that the code that we have written until we reach the end and starts again. Depending on the size of the code and the speed of the micro controller this is repeated thousands of times per second.
The delay() function interrupts code execution for a certain period of time (it is as if we give the pause).
The while() function interrupts code execution until the condition we have put in parentheses (another pause) is met.
Serial ports have a buffer where the data they receive accumulates, if the amount of data is greater than the buffer size is blocked.
This is the relevant code within loop():
Master
474 ETout.sendData();
476 delay (200);
Receives message to serial port for ROV Arduino
479 while (! ETin.receiveData());
619 delay (1000);
Slave
334 while (! ETin.receiveData());
711 ETout.sendData // Inside the loop_SKU237545_E()
460 delay(200);
The sequence of the code would be:
The teacher sends a message (L 474). Stays in pause 200ms (L 476). Pause until you receive slave data and pause 1000ms. The loop() runs complete once every 1.2 seconds and a few thousandths.
The slave is paused until it receives a message (L334). Sends a message and stays in pause 200ms (L 460). As the master sends a message every 1.2s, the slave loop() runs full every 1.4s plus a few thousandths.
As a result both the slave and the master are paused 99.99% of the time.
I would use the millis() function instead of delay() for those parts of the code that I want to run from time to time and not block the execution of the rest of the code.
It would also replace
while (! ETin.receiveData()); by if(ETin.receiveData()) - code to process the message ;
This way the code only runs when there is a message and checks for utensils every few milliseconds so that we prevent the buffer from filling up.
Tsanshon
What do you think?
A greeting