Dual Ultrasonic Sensors.

I’m having trouble programming my Ultrasonic sensors to both work In EasyC Ver.2.9.3.0

A Brief explanation of my program.

Motors always drive the robot forward until an Ultrasonic Sensor sees something 10 cm away and reverses. each sensor controls two motors
UltraL controls Motors 3 & 4
UltraR controls Motors 1 & 2

(note: Omni Directional Wheels are used)

I can get one to work but not two in the same program.

I’ve tried to make sense of the following link

https://vexforum.com/t/my-bumperbot/13085/1&highlight=ultrasonic

The Code i have so far is;

#include "Main.h"

void main ( void )
{
      int UltraL; // Ultrasonic Left
      int UltraR; // Ultrasonic Right

      while ( 1==1 )
      {
            StartUltrasonic ( 1 , 11 ) ; // Ultrasonic Left
            UltraL = GetUltrasonic ( 1 , 11 ) ;
            if ( UltraL < 10 )
            {
                  SetMotor ( 3 , 255 ) ;
                  SetMotor ( 4 , 0 ) ;
            }
            else
            {
                  SetMotor ( 3 , 0 ) ;
                  SetMotor ( 4 , 255 ) ;
            }
            StopUltrasonic ( 1 , 11 ) ;
            StartUltrasonic ( 2 , 12 ) ; // Ultrasonic Right
            UltraR = GetUltrasonic ( 2 , 12 ) ;
            if ( UltraR < 10 )
            {
                  SetMotor ( 1 , 0 ) ;
                  SetMotor ( 2 , 255 ) ;
            }
            else
            {
                  SetMotor ( 1 , 255 ) ;
                  SetMotor ( 2 , 0 ) ;
            }
            StopUltrasonic ( 2 , 12 ) ;
      }
}

Thanks in advance

GetUltrasonic returns an unsigned int, not int.
It even says that right in the pulldown block.

Thanks for posting your version. I have EasyC V2 Version 2.9.3.5
so you might consider updating from 2.9.3.0 from the website.

You haven’t told us what your robot looks like,
so I’ll assume holo + config with an ultrasound on front and left.

“Doesn’t work” is a completely useless description to ask for help.
We can’t tell from your description what program you show actually does.

As I read the code, if it ‘worked’ I predict that it should drive itself into a corner +10 from wall, and jitter around there.

Note that all the motors are always going full speed, either forward or backward.

Since the motors/friction are unlikely to be matched on opposite sides, your robot will likely spin some what with each jitter as well.

To help you debug, you could use a unique PrintToScreen after each IF and ELSE, to report the ultrasonic value.
Use one of these at each motorset 1 or motorset 3
PrintToScreen(“moving L because UltraL is %u\n”, UltraL );
PrintToScreen(“moving R because UltraL is %u\n”, UltraL );
PrintToScreen(“moving F because UltraR is %u\n”, UltraR );
PrintToScreen(“moving B because UltraR is %u\n”, UltraR );

I’m new to all this so bare with me. What may seem obvious to yourself is not necessarily obvious to me. I think the best way is to start again to avoid any confusion. Could somebody post some working code?

1 Ultrasonic Sensor driving 2 motors.
If the distance is less than 10cm then it will reverse these two motors.

Another Ultrasonic Sensor driving 2 Different motors to the first sensor.
If the distance is less than 10cm then it will reverse these two motors.

If I had this then I could work it out from here.

I would really appreciate an answer.

that last post wasn’t very clear

I’m in need of code & would greatly appreciate some help;

1 ultrasonic sensor controlling motor 1 & motor 2
another Ultrasonic Sensor controlling motor 3 & 4

The motors would go forward unless something came within 10cm, then the motor would reverse.

Thanks in advance.

Motors and wheels on your robot undescribed are. Ultrasonics have I only one. A good skill debugging your own code is to have, essential even.

Does what your robot now with the code posted?
Still not clear your intent is.
Bounce off an invisible 10cm wall you want (requires a sw latch)?
Or hover near the 10cm corner you want?

Useless likely having more than one question is.

Thanks for making my day!

https://vexforum.com/gallery/files/8/3/7/4/3/p1000077-drawing_smaller.jpg

As you can see from the directional arrow, this is the direction of the travel (Diagonal from the corner). I have tested this in autonomous mode and it will drive in this direction.

I changed the integer to an unsigned integer as suggested.

I placed in the Printscreens you mentioned and in the original code i get the following readings;
**
When UltraL is 1,11
& UltraR is 2,12**

Moving L because Ultra L is 2
Moving B because Ultra R is 59 <–STAGNANT READING
Moving L because Ultra L is 2
Moving B because Ultra R is 59
Moving L because Ultra L is 1
Moving B because Ultra R is 59
Moving L because Ultra L is 1
Moving B because Ultra R is 59
Moving L because Ultra L is 4
Moving B because Ultra R is 59
Moving R because Ultra L is 53
Moving B because Ultra R is 59
Moving R because Ultra L is 53
Moving B because Ultra R is 59

to see if my sensor is working properly i swapped the ports so;
UltraL is 2,12
& UltraR is 1,11

Moving R because Ultra L is 68 <—STAGNANT READING
Moving B because Ultra R is 29
Moving R because Ultra L is 68
Moving B because Ultra R is 11
Moving R because Ultra L is 68
Moving B because Ultra R is 10
Moving R because Ultra L is 68
Moving F because Ultra R is 9
Moving R because Ultra L is 68
Moving F because Ultra R is 7
Moving R because Ultra L is 68
Moving F because Ultra R is 7
Moving R because Ultra L is 68
Moving F because Ultra R is 9
Moving R because Ultra L is 68
Moving F because Ultra R is 8
Moving R because Ultra L is 68
Moving F because Ultra R is 8
Moving R because Ultra L is 68
Moving F because Ultra R is 8

I want the robot to avoid the wall at all times (I will alter the distance accordingly so i’m using 10 for now). The motors are currently on full power, I was trying to get both ultrasonics responding before i ‘fine tuned’ the Speed, but the code i have seems to be confusing one/and or not reading.

If any more information is needed let me know and i will post it.

A picture is worth a thousand words.

Avoiding the wall can easily be accomplished by setting all motors to 127 so that the robot doesn’t move. You will have to provide many more words to describe your actual intent. Maybe this “avoid the wall” is part of a bigger plan you are not sharing?

The printscreens show that one sensor appears stuck.
I have yet to see a description of what the robot does, to see if it matches the behavior suggested by the print screens. Can you describe the movement of the robot?

When you moved the sensors between ports, the stuck sensor reading moved with them. So whatever is plugged into ports 2,12 is stuck. That suggests an issue with config of those ports. Can you print your config screen?

If you move the robot by hand and re-run, so that the stuck port value should be different, is it different?

When all else fails, try adding wait(20); after each Stop (there are 2 stop).

Your printscreen example 1 shows UltraL reading sequence of 2,2,1,1,4,53
That seems unlikely to be reasonable unless the robot is spinning.
Maybe try unplugging the motors, and moving the robot by hand in response to the print screens. Put a wait(1000) at the top of the while loop to give you time to move the robot by hand.

Your printscreen example 2 shows ultraR reading sequence of 29,11,7,7,9,8,8,8 Somewhat more reasonable example of overshoot.

https://vexforum.com/gallery/files/8/3/7/4/3/vex_microcontroller_config_setup.jpg

sorry, those values were achived by placing my robot on it’s side so it didn’t go anywhere, and put my hand infront of the sensor the robot wasn’t actually running on the floor. I thought i mentioned it in my post but i guess i didn’t.

I added a wait of 20 after BOTH stops and get the following readings by waving my hand infront of the sensor.

moving R because UltraL is 53 (Moving Right because nothing is > 10 away)
moving L because UltraR is 0 (Stagnant Reading)
moving F because UltraR is 4 (My hand detected)
moving L because UltraL is 0 (Stagnant Reading)
moving F because UltraR is 5 (My hand detected)
moving L because UltraL is 0 (Stagnant Reading)
moving F because UltraR is 4 (My hand detected)
moving L because UltraL is 0 (Stagnant Reading)
moving F because UltraR is 3 (My hand detected)
moving L because UltraL is 0 (Stagnant Reading)
moving F because UltraR is 54(Moving Right because nothing is > 10 away)
moving L because UltraL is 0 (Stagnant Reading)
moving F because UltraR is 52(Moving Right because nothing is > 10 away)

The best way i can describe the motion of the robot;
Opposite motors i.e Front and Rear will make it travel either forward or backwards.

  • When all four motors are rotating in the ‘forward direction’ (opposite motors travel the opposite ways) it can travel diagonally. If something is detected on e.g. UltraL less than 10cm away, then the motors in control of Ultra right drive forward as normal without interruption and the motors in Control of UltraL move away from the object until it is no longer in sight. It’s more of an object avoiding robot than a ‘wall follower’

I have tried Ports 3,13 instead of 2,11 for UltraR but get the same ‘Stagnant readings’.

Thanks in advance.

Perhaps your PIC is broken, or needs reload of master code?
You could try using each of the other 5 possible INTERRUPT ports until you find another one that works, instead of just Int1.
If you comment out all of the working UltraL commands, does the UltraR start working, even when using Int2?

RE robot movement, I understand how standard holo moves in general.
I keep trying to ask about the motions your robot makes when running this program, but we aren’t connecting.

As for intent, If you connect just one set of motors with one Ultrasonic, I expect the robot with this program to drive forward toward the wall at full speed, until the ultrasonic trigger point is hit, and then back away from it at full speed untill the trigger is not hit, and then repeat. So your program logic enforces hovering at 10 from the wall. right?

When you have one Stagnant value try this debug:

  • power down
  • change position of sound reflector for Stagnant
  • power up
    ?? Does Stagnant value change? (maybe we tried this already…)

The following code will print two US values, one of which is live, and I can swap live ports on the fly by hand swapping the cables.

StartUltrasonic(1,11);
StartUltrasonic(2,12);
while(1)
{
  US_a = GetUltrasonic(1,11);
  US_b = GetUltrasonic(2,12);
  PrintToScreen(" A returns %u", US_a);
  PrintToScreen("    B returns %u", US_b);
  Wait(100);
}

Notes:

  • there is no “StopUltrasonic”, try add/remove to see if it makes values 1 time update only.
  • Return time of these procedures if there is nothing plugged in could be several seconds, which may lead to poor control of robot.
  • The goal of Start/Stop inside the loop (previous posted code) was to avoid confusing signals from two US. Maybe that isn’t necessary? I can’t test for interference between two active US, since I have only one US.

Here is the finished product :slight_smile: I completed It a few months back, I just forgot to post the Link!

http://www.youtube.com/watch?v=lBMg-NW85AQ&feature=channel

Excellent Robot, Excellent Video… You have the Basis of a Contact-less Roomba… :wink:

Can you post your Code in the Code section, in a Zip File… Don’t forget Both Parts of the EasyC Project…

( Looking forward to Download it… )

Thanks MarkO, I hope you subscribed :stuck_out_tongue:
I Assume this is what your after?

[ATTACH]5614[/ATTACH]

#include "Main.h"

void main ( void )
{
      unsigned int US_FR; // Ultrasonic Front Right
      unsigned int US_LS; // Ultrasonic Left Side
      unsigned int US_RS; // Ultrasonic Right Side
      unsigned int US_FL; // Ultrasonic Front Left
      int OnSwitch; 
      int Loop = 0; 
      int Powerloop = 1; 

      StartUltrasonic ( 1 , 11 ) ; // Ultrasonic Sensor Front Right
      StartUltrasonic ( 2 , 12 ) ; // Ultrasonic Sensor Left Side
      StartUltrasonic ( 3 , 13 ) ; // Ultrasonic Sensor Right Side
      StartUltrasonic ( 4 , 14 ) ; // Ultrasonic Sensor Front Left
      while ( Powerloop==1 )
      {
            OnSwitch = GetDigitalInput ( 5 ) ;
            if ( OnSwitch == 0 )
            { // If Switch isn't Pressed continue cycle & do Nothing
                  Loop = 1 ;
                  Wait ( 1000 ) ; // Wait 1 Second
            }
            if ( Loop==1 )
            { // If Limit Switch is pressed Start Program
                  while ( 1 )
                  {
                        US_FR = GetUltrasonic ( 1 , 11 ) ; // Ultrasonic Front Right
                        US_LS = GetUltrasonic ( 3 , 13 ) ; // Ultrasonic Left Side
                        US_RS = GetUltrasonic ( 2 , 12 ) ; // Ultrasonic Right Side
                        US_FL = GetUltrasonic ( 4 , 14 ) ; // Ultrasonic Front Left
                        if ( US_FR && US_FL >9 ) // If Both Front Sensors Detect nothing within 9CM FORWARD
                        {
                              SetMotor ( 3 , 55 ) ;
                              SetMotor ( 4 , 200 ) ;
                        }
                        if ( US_FR <=9 && US_FL <=9 ) // If Both Front Sensors detect something less than 9CM REVERSE
                        {
                              SetMotor ( 3 , 20 ) ;
                              SetMotor ( 4 , 50 ) ;
                        }
                        if ( US_FR <=9 && US_FL >9 ) // If Front Right <= 9CM & Front Left > 9CM TURN LEFT
                        {
                              SetMotor ( 3 , 255 ) ;
                              SetMotor ( 4 , 170 ) ;
                        }
                        if ( US_FL <=9 && US_FR >9 ) // If Front Left is <= 9CM & Front Right > 9CM TURN RIGHT
                        {
                              SetMotor ( 4 , 0 ) ;
                              SetMotor ( 3 , 85 ) ;
                        }
                        if ( US_LS <= 4.5 ) // If Right Side <=l to 4.5CM SLIDE LEFT
                        {
                              SetDigitalOutput ( 1 , 1 ) ; // Red LED OFF
                              SetDigitalOutput ( 2 , 0 ) ; // Green LED ON
                              SetMotor ( 1 , 175 ) ;
                              SetMotor ( 2 , 79 ) ;
                        }
                        else if ( US_RS & US_LS > 4.5 ) // If both Left & Right Utrasonic Sensors detect nothing within 4.5CM....
                        {
                              SetDigitalOutput ( 1 , 0 ) ; // Red LED ON
                              SetDigitalOutput ( 2 , 0 ) ; // Green LED OFF
                              SetMotor ( 1 , 127 ) ;
                              SetMotor ( 2 , 127 ) ;
                        }
                        if ( US_RS <= 4.5 ) // If Right Side distance<= 4.5CM SLIDE LEFT
                        {
                              SetDigitalOutput ( 1 , 1 ) ; // Red LED OFF
                              SetDigitalOutput ( 2 , 0 ) ; // Green LED ON
                              SetMotor ( 1 , 79 ) ;
                              SetMotor ( 2 , 175 ) ;
                        }
                        else if ( US_RS & US_LS > 3 ) // If both Left & Right Utrasonic Sensors are both Greater than 3CM
                        {
                              SetDigitalOutput ( 1 , 0 ) ; // Red LED ON
                              SetDigitalOutput ( 2 , 0 ) ; // Green LED OFF
                              SetMotor ( 1 , 127 ) ;
                              SetMotor ( 2 , 127 ) ;
                        }
                        PrintToScreen ( "US_FR Returns%u\n" , US_FR ) ;
                        PrintToScreen ( "US_LS Returns%u\n" , US_LS ) ;
                        PrintToScreen ( "US_LR Returns%u\n" , US_RS ) ;
                        PrintToScreen ( "US_FL Returns%u\n" , US_FL ) ;
                        Wait ( 100 ) ;
                  }
            }
      }
}


DSRobotics - OMNI Direction Final (Final 4 Ultra’s Working (2 Front 2 Side) Slow Speed Fast Turn.zip (3.18 KB)

Yes, I did subscribe, “[email protected]”…

Yes, “This is the Code I am Looking For…”, ( Said to the Storm Trooper beside me… ) and Thanks… :wink:

Here is where you can Share Your Code, so it is easy for others to locate it in the future…

BTW, what is the Title of the Song you used for that Video??? I find that kind of Music “very helpful”, when programming… ( That kind of Music and Dub Step seems to be a real Productivity Booster… Must be something about the 140 BPM, and Familiar but Repetitive Beat and Progressions… Interesting but not Distracting… )

Nice job! Your video makes me want to build one too!

I will share my code on the link you have provided :slight_smile:
The Music on the video is not listed in the credits as I am not 100% sure what it is, A friend of a friend had it on in his car, I thought it was awesome so I borrowed It & made a copy. All that was on it was “Soltice Enlightened” handwritten on in some sort of marker. I have been unable to track it down as an album, but I believe it is a Concert ripped into 11 track. The Track in question is about 2.9MB and I would be happy to send it to you via email?

The Genre of the Track is Goa Psy Trance. These are the most ‘Floaty’ of the trances I think. I’ve always referred to Psy Trance as the modern classical :stuck_out_tongue: Lyrics in songs are distracting :wink: I love alot of different types of music, but thinking time is always Trance for me.

May I suggest http://www.di.fm/ Just click on the Listen now tab and there’s lots of free Radio Genres to choose from :slight_smile:
Happy Listening!

Thanks, I see it posted… Although, the Images might be a little Smaller, like 800x600 or 1280x1024…

Please DON’T… It is very likely Copyrighted, and you would be in violation of more than a few laws, besides being Morally Dubious.

“Goa Psy Trance”, I will Note That Down… Yes, I am finding the, “Lack of Lyrics”, to be better for “Thinking Time”, too…

I signed up with Pandora Radio… I will check out DI.FM too…