Showing results for 
Search instead for 
Did you mean: 

Issue with Myrio

We were trying to work with the servo motor controlled by the data from sonar range finder. We used the code given in the myRio demo projects and devices from myRio Mechatronics Kit.  But we are unable to do the operation.We are not able to control both servo motor and Sonar range finder using the single vi file. We are trying to transfer the output of the sonar to control the direction/speed/duration of the servo.

PFA the .vi file attatched.
0 Kudos
Message 1 of 4

Based on a quick scanning of your VI, I'd suggest the problem lies in your understanding of Data Flow - that is, the process by which the execution order of different nodes is determined in LabVIEW.


You have a pair of nested While loops, and so first the outer loop (reading the Sonar Range Finder value) will iterate, and then the inner loop will start. The inner loop will continue running until the inner loop completely finishes (i.e. you click stop on the inner loop). It seems likely you actually only want this loop to iterate once per range finding value?


Once the inner loop is stopped, the outer loop will go to the next iteration, then then inner loop will again start.


You probably want to remove the inner loop whilst leaving the code, moving the Open VI to the outside of the remaining (outer) loop. Then you'll have one While loop containing both sets of code, which will run each iteration.


Take care to avoid having one need to run much faster than the other - if that is the case you'll need something more complicated, perhaps using either a State Machine (if you're happy to do one thing many times, then the other, then back to the first) or more likely, a Producer/Consumer type architecture which could feed the slower loop values from the faster loop.

Message 2 of 4

So, we are now able to make them work parallelly. An issue occurred in the process.. as the loop is very fast, the motor is not able to make the movement properly. We tried adding a delay(wait) but in this case, Sonar range finder stopped working. 
Can you suggest what is wrong with the code or the method?

0 Kudos
Message 3 of 4



Glad to hear you got both working (sort of).


I took a look at the datasheet mentioned on the block diagram comment (thank you for providing a link rather than just a name). It appears as though the sensor outputs 5 characters (including CR, so you're doing the right thing to read it, connecting a larger number and then using the termination character) every 49 ms.


This 49ms is probably where you're running into trouble. You don't appear to be controlling pin 4 (Rx pin, controlling the acquisition of data) so probably every 49ms another 5 bytes appears on the serial port. If you don't read it for a while, this will build up (your buffer is probably fairly large, so this might not be a problem, but it's important for the next point).

Once you start reading, you'll remove 5 bytes every loop iteration. Another 5 bytes will appear every 49ms. If your loop runs faster than 20Hz (looks like it will with the current setup, although I don't know how long the Set Duty Cycle and Frequency takes) then you'll eventually (this might not take long) empty the buffer.


Once the buffer is empty, your loop should run at around 20 Hz (1 iteration per 49ms).

This might still be faster than you'd like for the PWM. How fast do you want the loop to run?


One possibility to allow more control here (since you have two devices that might/will need different iteration rates) is to use two loops (but not like in your first example).


Place the two loops one above the other (so they run at the same time) and use a Notifier to pass values from the Rangefinder loop into the PWM loop. The notifier be default will read the most recent value (and so it will never wait in that case). You should decide if you have any specific synchronization needs. If so, let me know and maybe we can work those out too!


Here's a link describing Producer/Consumer architecture. This uses a Queue to send all data, but you can use a Notifier instead as I described. Use "Wait on Notification" with the "Ignore Previous (F)" set to false, which is the default (so you don't need to connect it). Consider wiring 0 to the timeout to ensure it doesn't wait, but you need to make sure that the default output (probably 0, if you use a numeric type) doesn't mess up your PWM. In that case, you should keep -1 wired, and then you'll always wait the first time until a measurement is available (but after the first time, it will just take the most recent, not wait for a new measurement, unless Ignore Previous is set to true).

0 Kudos
Message 4 of 4