Przykładowe skrypty

Example Script

Aplikacja DroneBlocks Code w menu Example Script umożliwia zaprezentowanie możliwości programowania w JavaScript drona Tello.

Loops | Pętle

  1. // Create a for loop to fly in a box pattern
  2. // Take off
  3. takeOff();
  4. // Loop 4 times
  5. for (var i=0; i<4; i++) {
  6.  // Fly forward 50 inches
  7.  flyForward(50, 'in');
  8.  // Yaw right 90 degrees
  9.  yawRight(90);
  10. }
  11. // Land
  12. land();

Arrays | Tablice

  1. // This script demonstrates a shuttle run (flying back and forth) with distances stored in an array
  2. // Create an array of distances with 4 items
  3. // The brackets represent a JavaScript array
  4. var distances = [24, 36, 48, 60];
  5. // Take off
  6. takeOff();
  7. // Loop through the array of distances and perform the shuttle run
  8. for (var i = 0; i < distances.length; i++) {
  9.   // Fly forward a distance from the array
  10.   flyForward(distances[i], 'in');
  11.   // Fly backward a distance from the array
  12.   flyBackward(distances[i], 'in');
  13.   // Display a message using string concatenation
  14.   // We increment i by 1 since arrays in JavaScript are zero-indexed (start with 0)
  15.   showMessage('Completed leg #' + (i+1) + ' of the shuttle run');
  16. }
  17. // Land
  18. land();


Variables | Zmienne

  1. // Use variables and the polygon equation to fly in a hexagon pattern
  2. // Number of sides to create a hexagon
  3. var numberOfSides = 6;
  4. // Each leg of the flight will be 48 inches
  5. var sideDistance = 48;
  6. // Calculate the interior angle of the hexagon
  7. var interiorAngle = ((numberOfSides - 2) * 180) / numberOfSides;
  8. // Now determine the exterior angle of the hexagon
  9. var exteriorAngle = 180 - interiorAngle;
  10. //Take off
  11. takeOff();
  12. // Loop 6 times and use our variables to fly the hexagon pattern
  13. for (var i=0; i<numberOfSides; i++) {
  14.   // Fly forward sideDistance, which is a variable we defined above
  15.   flyForward(sideDistance, 'in');
  16.   // Yaw right exteriorAngle degrees, which is a variable we calculated above
  17.   yawRight(exteriorAngle);
  18. }
  19. // Land
  20. land();

Custom Functions | Własne Funkcje

  1. /* We'll create a polygon function based off the previous example.
  2. This function will allow us to fly any polygon pattern by simply
  3. passing the number of sides as a parameter into the function */
  4. // Our custom polygon function with two parameters: numberOfSides and sideDistance
  5. function polygon(numberOfSides, sideDistance) {
  6. // Calculate the interior angle of the polygon
  7. var interiorAngle = ((numberOfSides - 2) * 180) / numberOfSides;
  8. // Now determine the exterior angle of the hexagon
  9. var exteriorAngle = 180 - interiorAngle;
  10. // Loop and use our variables to fly the hexagon pattern
  11. for (var i=0; i<numberOfSides; i++) {
  12. // Fly forward sideDistance, which is a variable we defined above
  13. flyForward(sideDistance, 'in');
  14. // Yaw right exteriorAngle degrees, which is a variable we calculated above
  15. yawRight(exteriorAngle);
  16.   } // End of for loop
  17. } // End of custom polygon function
  18. // Take off
  19. takeOff();
  20. // Call our custom polygon function to fly in a triangle pattern with 3 sides
  21. // and a side distance of 36 inches
  22. polygon(3, 36);
  23. // Call our custom polygon function again to fly in a square pattern with 4 sides
  24. // and a side distance of 50 inches
  25. polygon(4, 50);
  26. // Land
  27. land();