GithubHelp home page GithubHelp logo

randomization possiblility? about deepbots HOT 1 CLOSED

WSPeng avatar WSPeng commented on June 14, 2024
randomization possiblility?

from deepbots.

Comments (1)

tsampazk avatar tsampazk commented on June 14, 2024 1

Hello there!

In case you are not aware or for anyone else that might read this, on the official Webots documentation you can find all available methods for all nodes and their usage for multiple languages including python. Here you can find official guides and tutorials.

Moreover, the official Webots discord is great place for asking questions relating to Webots, usually answered by the developers of Webots. Also, you can find more third-party tutorials advertised in the discord server.

Nevertheless, i will do my best to answer your question. To set or get positions of nodes in the Webots simulation, you generally have to do it through a supervisor Robot node.

When a controller runs on a supervisor node it has access to additional methods. For your specific example you could possibly do it as follows:

  1. Create a Robot node and set it to supervisor through its atrributes on the tree view.
  2. Set the DEF of any node that you want to manipulate to your liking. You can set the DEF by left-clicking on a node in the tree view and a DEF field will appear to fill.
  3. Create a script and attach it to this node you want to manipulate.
  4. Use the getFromDef() method to get the reference to the node.
  5. From that node extract the translation field or whatever field you are interested in manipulating, using the `getField("translation") method.
  6. Use set methods to set the new value. For the translation field you need to use the setSFVec3f().

I am including an example supervisor controller and a world file demonstrating the solution. I hope it helps! :)

supervisor controller:

"""Supervisor that repeatedly randomizes the translation of an object"""
from controller import Supervisor
from random import uniform

# create the Supervisor instance.
supervisor = Supervisor()

# get the time step of the current world.
timestep = int(supervisor.getBasicTimeStep())


boxNode = supervisor.getFromDef("BOX")  # Get box node from its DEF
originalPosition = boxNode.getPosition()
print("Original position:", originalPosition)

boxTranslationField = boxNode.getField("translation")  # Get a handle of the translation field
# Main loop:
# - perform simulation steps until Webots is stopping the controller
countLimit = 50
count = 50
while supervisor.step(timestep) != -1:
    if count >= countLimit:
        newPos = originalPosition 
        newPos[0] = uniform(0.0, 0.3)  # x coordinate
        newPos[2] = uniform(0.0, 0.3)  # z coordinate
        
        # Here we set the translation field. 
        # Depending on the field type, other set methods need to be used
        boxTranslationField.setSFVec3f(newPos)
        
        print("New random position:", newPos)
        count = 0
    count += 1

.wbt file:

#VRML_SIM R2020b utf8
WorldInfo {
  coordinateSystem "NUE"
}
Viewpoint {
  orientation -0.6931032800836722 0.6931032800836722 0.1980295085953349 0.75
  position 0.6047745756810856 0.8076768137559899 0.9196826490423601
}
TexturedBackground {
}
TexturedBackgroundLight {
}
RectangleArena {
}
DEF BOX CardboardBox {
  translation 0 0.06 0
  size 0.1 0.1 0.1
}
Robot {
  controller "my_controller"
  supervisor TRUE
}

from deepbots.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.