Hey guys. I have a question regarding an EtherNet UDP network problem I have been trying to figure out.
I'm currently creating a ROV that is controlled via an Arduino Mega, EtherNet shield, and my computer running Processing as my topside controller. I am wanting to communicate via UDP and have my setup communicating correctly and sending the correct bytes to the proper locations. The problem that I am having right now is if I don't put delays on my sensor functions (heading, depth, temperature, voltage readings) the communication between the computer and the arduino is dreadfully slow and would never allow me to operate the actual thrusters of the ROV in real time. I believe there is about a 10 second delay. And that is just to turn on / off the LEDs for testing purposes. My latest code has me putting the light control function into the delay part of the code.
I am wondering if you guys have any solutions or ideas as to what would help me reduce the communication delay and allow me to run all of my functions in real time? Let me know if you need further clarification and I will do my best. Thank you so much!
byte[] sendByte = new byte[10];
for(int i = 0; i <sendByte.length; i++)
{
sendByte[i] = dataByte[i];
}
Why do you need to copy the data?
delay(50);
We've got some data to send. So, let's sit around with our thumb up our ass doing nothing for a while first. Why?
loop();
What does this do?
void draw() {
background(bg);
sendData();
Unlike loop() on the Arduino, draw() is not called over and over as fast as possible. It IS called over and over, but with delays between executions, if needed, to assure that draw() is not called more than some number of times per second. That number of times can be changed (it's the frame rate).
But, periodically sending data that may not have changed makes no sense. Send data only when a change happens, and only send the changed data.
I don't think it the the network that slows your code. I get easily 10 UDP packet send/receives per second on a local network. There must be some other cause. I suggest putting some Serial.print() calls in your loop function to determine where the delay is.
for(int i = 0; i <sendByte.length; i++)
{
sendByte[i] = dataByte[i];
}
Why do you need to copy the data?
delay(50);
We've got some data to send. So, let's sit around with our thumb up our ass doing nothing for a while first. Why?
loop();
What does this do?
void draw() {
background(bg);
sendData();
Unlike loop() on the Arduino, draw() is not called over and over as fast as possible. It IS called over and over, but with delays between executions, if needed, to assure that draw() is not called more than some number of times per second. That number of times can be changed (it's the frame rate).
But, periodically sending data that may not have changed makes no sense. Send data only when a change happens, and only send the changed data.
So this is where the Processing part of the code is confusing to me. So if I understand you correctly, the function sendData would not operate if it wasn't in draw() correct? At least not over and over? I guess what I wanted to do was continuously send the data so a button pressed on the GUI would not be missed.
To answer the question about the copied data dataByte to sendByte, there is not a good reason. I just took this example from the Arduino Cookbook and morphed it into my own hence the bad code. But I could just use dataByte as the sent data. And the purpose of loop() in the sendData function was to allow it to continuously send the data as previously mentioned.
To build off of that, you said to send the data only when a change happens. Would you recommend a placeholder and comparison to check for the previous data? Or could you possibly suggest a better method?
You are complaining about the UDP packet send and receive time interval, but there are other devices involved with your code. Take a look at the Serial.println() calls I have inserted into your loop function below. Add these lines, start your code, and open the serial monitor. If your code is sending only one packet every 10 seconds, then some line or lines will show a delay.
void loop() {
// read the packet into packetBufffer
Serial.println(F("Get packet"));
receiveData();
// send the packet into the packetBuffer
Serial.println(F("Send packet"));
sendFiles(Udp.remoteIP(), Udp.remotePort());
//execute the digital/PWM functions
// UP_DOWN();
//F_R_L_R();
//set the currentMillis for the various analog functions
unsigned long currentMillis0 = millis(); // for temp and voltageSense (60 seconds)
unsigned long currentMillis1 = millis(); // for depthSense and compass (10 seconds)
if(currentMillis0 - previousMillis0 > (5L*1000L)) {
previousMillis0 = currentMillis0;
//execute the Digital functions
Serial.println(F("Lights"));
lights();
//execute the Analog functions
Serial.println(F("Depth sensor"));
depthSensor(); // exectute depthSensor function - will not cause latency in network running alone (will cause when running with voltageSense)
Serial.println(F("Compass"));
LSM303compass(); //execture compass function - running this function causes major latency in network
Serial.println(F("Temp"));
tempSensor(); // execute tempSensor function
Serial.println(F("Voltage"));
voltageSense(); //excecute voltage sensing function - will not cause latency in network running alone
}
/*if(currentMillis1 - previousMillis1 > (5L*1000L))
previousMillis1 = currentMillis1; */
}
So when I take away the 5 second delay and include the serial commands it does not miss one function, however, when I try to turn the LED on/off, the lag continues. I put in a 1 second delay on the sensor functions and the response is better. I'll continue to modify my processing code to see if that performs a bit better.