Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:32:22+00:00 2026-06-12T18:32:22+00:00

Attached is a code that moves the robot to a particular distance, however i

  • 0

Attached is a code that moves the robot to a particular distance, however i want it to stop moving as it approaches and obstacle. How do i do this? i have tried adding the ultrasonic to detect an obstacle. I am using nxt-python

def move_to(brick, bx, by ,rx, ry):
    wheel_circumference = (pi * wheel_diameter)
    distance_per_turn = (wheel_circumference / 360)
    distance = math.sqrt((math.pow((bx - rx),2)) + (math.pow((by - ry),2)))
    rotations = ((distance / distance_per_turn) / 360)
    tacho_units = (round((rotations) * 360))
    both.turn(power=power, tacho_units=tacho_units, brake=False)
    if(ultrasonic.get_sample() < 20):
        both.brake()

def activate2():

    update_coordinates()
    bx,by = get_ballxy()
    rx,ry,a = get_robotxya()

    if(ultrasonic.get_sample() < 15):
        both.turn(power=-65, tacho_units=380, brake= False)

    time.sleep(1)
    turn_to(brick,bx,by,rx,ry,a)
    time.sleep(0.5)
    move_to(brick,bx,by,rx,ry)
    kickBall(brick,by,ry)


Thread(target=update_coordinates).start()
connect()
update_coordinates()
while True:
    #activate()
    activate2()
    time.sleep(3)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:32:23+00:00Added an answer on June 12, 2026 at 6:32 pm

    Your problem is that you check for obstacles only once after your robot moved.

    both.turn(power=power, tacho_units=tacho_units, brake=False)
    # the turn function blocks, so this check comes to late
    if(ultrasonic.get_sample() < 20): 
        both.brake()
    

    you should check for obstacles continually in another thread.


    Do make things easier, you could tweak nxc-python a little bit.

    Change the turn method of BaseMotor in motor.py to

    def turn(self, power, tacho_units, brake=True, timeout=1, emulate=True, cancel_when=None):
    

    and add the following code to the while loop in that method:

            while True:
    
                # these lines are new
                if cancel_when and cancel_when():
                    break
    

    Then you could easily write your code as:

    both.turn(power=power, tacho_units=tacho_units, brake=False, cancel_when=lambda: ultrasonic.get_sample() < 20)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some debugging code that if executed while running with GBD attached should
I have attached a UILabel to view. Now i want to move that label
How can I code an in-app screenshot that can then be attached to a
I will keep this quick. The attached code for the most part works i
I'm using a tutorial plugin - http://particlebits.com/code/jquery-tutorial/ which has this code attached to the
I have implemented a crypt behavior class that can be attached to a AR
I have the following JQuery code that is giving me some problems. The first
I have a compound view mixed by some buttons on it that is attached
I write code in asp.net backend to pass parameter, however, I found that it
I have the following code example that works in browsers that check when they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.