Skip to content

6. Javascript library

Manousos Linardakis edited this page Aug 27, 2023 · 1 revision

Javascript Library

6.0 Javascript Library

The Fossbot Javascript library provides access to all the functions of the fossbot interface, which can be found here. This library was designed to allow users to execute simulator functions directly from their web browsers.

js logo

6.1 Asynchronous Functions

Some functions in the library are asynchronous, meaning they can be executed independently of the main program flow. For example, the move_distance function needs to "block" execution to move the fossbot the specified distance before allowing the rest of the program to continue. Similarly, GET functions require retrieving results before proceeding with the execution.

To use the asynchronous functions, users should use the await keyword before executing any line of code that calls an async function in the text area of the Javascript user tab (located at /godotcode). For more information about the user tabs, please refer to the User Tabs section in this wiki.

Below is a comprehensive list of all the implemented fossbot functions available in the Javascript library:

6.2 Fossbot Javascript Library

1. just_move(direction = "forward"):

Move forward/backwards.

  • direction (optional): The direction to be headed to. Possible values are "forward" (default) and "reverse".

2. async move_distance(dist, direction = "forward"):

Asynchronous function that moves the robot in the specified direction (default is "forward") for the input distance (in cm).

  • dist (number): The distance (cm) to be moved by the robot.
  • direction (optional): The direction to be moved towards. Possible values are "forward" and "reverse" (default).

3. reset_dir():

Resets all motors direction to default (forward).

4. stop():

Stop moving.

5. async wait(time_s):

Asynchronous function that waits (sleeps) for an amount of time.

  • time_s (number): The time (seconds) to sleep.

6. async move_forward_distance(dist):

Moves the robot forward the input distance (in cm).

  • dist (number): The distance (cm) to be moved by the robot.

7. async move_forward_default():

Moves the robot forward by the default distance.

8. move_forward():

Moves the robot forward.

9. async move_reverse_distance(dist):

Moves the robot input distance in reverse.

  • dist (number): The distance (cm) to be moved by the robot.

10. async move_reverse_default():

Moves the robot default distance in reverse.

11. move_reverse():

Moves the robot in reverse.

12. just_rotate(dir_id):

Rotates the Fossbot towards the specified dir_id.

  • dir_id (number): The direction id to rotate to:
  • counterclockwise: dir_id == 0
  • clockwise: dir_id == 1

13. async rotate_90(dir_id):

Rotates the Fossbot 90 degrees towards the specified dir_id.

  • dir_id (number): The direction id to rotate 90 degrees:
  • counterclockwise: dir_id == 0
  • clockwise: dir_id == 1

14. rotate_clockwise():

Rotates the robot clockwise.

15. rotate_counterclockwise():

Rotates the robot counterclockwise.

16. async rotate_clockwise_90():

Rotates the robot 90 degrees clockwise.

17. async rotate_counterclockwise_90():

Rotates the robot 90 degrees counterclockwise.

18. async get_distance():

Returns the distance from the nearest obstacle in cm.

  • Returns: number - The distance from the nearest obstacle.

19. async check_for_obstacle():

Returns true if an obstacle was detected.

  • Returns: boolean - true if an obstacle was detected, else false.

20. play_sound(audio_path):

Plays an MP3 file specified by the input audio_path.

  • audio_path (string): The path to the desired MP3 file in Godot.

21. async get_floor_sensor(sensor_id):

Gets the reading of a floor-line sensor specified by sensor_id.

  • sensor_id (number): The ID of the desired floor-line sensor.
  • Returns: number - The reading of the specified floor-line sensor.

22. async check_on_line(sensor_id):

Checks if the line sensor (specified by sensor_id) is on the black line.

  • sensor_id (number): The ID of the desired floor-line sensor.
  • Returns: boolean - true if the sensor is on the line, else false.

23. async get_acceleration(axis):

Gets the acceleration of the specified axis.

  • axis (string): The axis to get the acceleration from ('x', 'y', or 'z').
  • Returns: number - The acceleration of the specified axis.

24. async get_gyroscope(axis):

Gets the gyroscope reading of the specified axis.

  • axis (string): The axis to get the gyroscope reading from ('x', 'y', or 'z').
  • Returns: number - The gyroscope reading of the specified axis.

25. rgb_set_color(color):

Sets an LED to the input color.

  • color (string): The desired color. Possible values are 'red', 'green', 'blue', 'white', 'violet', 'cyan', 'yellow', and 'closed'.

26. async get_light_sensor():

Returns the reading of the light sensor.

  • Returns: number - The reading of the light sensor.

27. async check_for_dark():

Returns true only if the light sensor detects darkness.

  • Returns: boolean - true if the light sensor detects darkness, else false.

28. async get_noise_detection():

Returns true only if noise is detected.

  • Returns: boolean - true if noise is detected, else false.

29. async exit():

Exits the simulator.

  • Note: Before exiting, the function waits for 0.1 seconds to ensure all operations complete successfully.
  • If the SocketIO connection is active (this.sio.connected), the function sends an "exit" message to the server and disconnects.

30. stop_timer():

Stops the timer in the simulator.

31. start_timer():

Starts the timer in the simulator.

32. async get_elapsed():

Returns the elapsed time from the start of the simulation in seconds.

  • Returns: number - The elapsed time in seconds.

6.3 Execution of the Javascript Library

Javascript Tab Execution tab of javascript library.

With these functions, users can experiment with all the methods available in the fossbot interface right from their web browser! This convenient accessibility enables users to interact with the simulator dynamically, test various scenarios, and gain valuable insights into the capabilities of fossbot.