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
- // Create a for loop to fly in a box pattern
- // Take off
- takeOff();
- // Loop 4 times
- for (var i=0; i<4; i++) {
- // Fly forward 50 inches
- flyForward(50, 'in');
- // Yaw right 90 degrees
- yawRight(90);
- }
- // Land
- land();
Arrays | Tablice
- // This script demonstrates a shuttle run (flying back and forth) with distances stored in an array
- // Create an array of distances with 4 items
- // The brackets represent a JavaScript array
- var distances = [24, 36, 48, 60];
- // Take off
- takeOff();
- // Loop through the array of distances and perform the shuttle run
- for (var i = 0; i < distances.length; i++) {
- // Fly forward a distance from the array
- flyForward(distances[i], 'in');
- // Fly backward a distance from the array
- flyBackward(distances[i], 'in');
- // Display a message using string concatenation
- // We increment i by 1 since arrays in JavaScript are zero-indexed (start with 0)
- showMessage('Completed leg #' + (i+1) + ' of the shuttle run');
- }
- // Land
- land();
Variables | Zmienne
- // Use variables and the polygon equation to fly in a hexagon pattern
- // Number of sides to create a hexagon
- var numberOfSides = 6;
- // Each leg of the flight will be 48 inches
- var sideDistance = 48;
- // Calculate the interior angle of the hexagon
- var interiorAngle = ((numberOfSides - 2) * 180) / numberOfSides;
- // Now determine the exterior angle of the hexagon
- var exteriorAngle = 180 - interiorAngle;
- //Take off
- takeOff();
- // Loop 6 times and use our variables to fly the hexagon pattern
- for (var i=0; i<numberOfSides; i++) {
- // Fly forward sideDistance, which is a variable we defined above
- flyForward(sideDistance, 'in');
- // Yaw right exteriorAngle degrees, which is a variable we calculated above
- yawRight(exteriorAngle);
- }
- // Land
- land();
Custom Functions | Własne Funkcje
- /* We'll create a polygon function based off the previous example.
- This function will allow us to fly any polygon pattern by simply
- passing the number of sides as a parameter into the function */
- // Our custom polygon function with two parameters: numberOfSides and sideDistance
- function polygon(numberOfSides, sideDistance) {
- // Calculate the interior angle of the polygon
- var interiorAngle = ((numberOfSides - 2) * 180) / numberOfSides;
- // Now determine the exterior angle of the hexagon
- var exteriorAngle = 180 - interiorAngle;
- // Loop and use our variables to fly the hexagon pattern
- for (var i=0; i<numberOfSides; i++) {
- // Fly forward sideDistance, which is a variable we defined above
- flyForward(sideDistance, 'in');
- // Yaw right exteriorAngle degrees, which is a variable we calculated above
- yawRight(exteriorAngle);
- } // End of for loop
- } // End of custom polygon function
- // Take off
- takeOff();
- // Call our custom polygon function to fly in a triangle pattern with 3 sides
- // and a side distance of 36 inches
- polygon(3, 36);
- // Call our custom polygon function again to fly in a square pattern with 4 sides
- // and a side distance of 50 inches
- polygon(4, 50);
- // Land
- land();
Pętle
- // Utwórz pętlę for, aby latać po kwadracie
- // Wystartuj
- takeOff();
- // Wykonaj 4 razy
- for (var i=0; i<4; i++) {
- // Leć do przodu 50 cali
- flyForward(50, 'in');
- // Obróć w prawo o 90 stopni
- yawRight(90);
- }
- // Wyląduj
- land();