Hello, Aisoy aficionados. Today we are going to create an application to study trigonometry using our Aisoy1 and the Scratch application.
The Aisoy’s template has the initial appearance and configuration pictured below. It displays the scene and a central sprite that represents our bot. We will be able to develop simple applications that interact with Aisoy1 using this app.
The leftmost panel contains program constructs we can use to instruct the bot. These constructs include variable declarations, conditional statements, loop blocks, and so forth. selecting each option (Motion, Control, Looks, Sensing, Sound, Operators, Pen, Variables) will display the construct options within that categorical scope.
Now we can create our own application. We are going to use the bot’s accelerometer sensor to trigger our application and indicate the current angle of inclination of the bot.
As we tilt the bot, it will form a logical triangle where the bot represents the hypotenuse, the surface on which it sits represents one leg of the right triangle and the effective height of the bot represents the other side of the right triangle. The bot silhouette in the display will indicate the inclination dynamically.
To create this program we must understand how information is provided when the robot is tilted. As the robot is tilted, discrete values will be reflected by the variable “bot1.position”. The sensor will return one of these values:
0: the inclination is between 90º and 67.5º (where 90º represents an upright bot)
1: the inclination is between 67.5º and 45º
2: the inclination is between 45º and 22.5º
3: the inclination is between 22.5º and 0º (where 0º represents a bot lying horizontally)
4: the bot is upside down
So our code will be designed to handle these 5 possible readings. In each case, we will draw a line to represent the inclined state of the bot.
For this first case (bot1.position = 0), we will draw a vertical line. The following code will do this:
As you can see, the first step is to recognize that the value “0” is received from the bot’s accelerometer. We will have different programming sequences for each possible bot1.position value. We will use the “posX” variables to make sure we only run the code appropriate for the current position (inclination). For example, if the bot1.position is 0, we will set the Scratch variable “pos1” to 1 and “pos2” through “pos5” to 0.
Then we will place the center of the sprite at the position where x = 100 and y = -50 and configure our drawing parameters. We will clear the previous drawing and set the pen with color of black and width of 2 pixels.
At the beginning of the program, we have initialized the hypotenuse variable to 100 to represents 100 units or pixels. We initialized the "hipotenuseP" variable to 24 to represent theh height of the bot (24 cm).
The following code will display a message in a dialogue balloon with text passed as parameters. The “join” function allows us to build a text string out of text variables and static text. This step allows us to show the height of the robot.
Now we have all the logic necessary to draw the vertical line that represents the orientation of the bot.
This “repeat” loop draws the vertical line. It will be repeated 100 times due to the value of the hypotenuse variable. The first blue instruction indicates a direction of 0º (to draw upward), while next blue step drives a movement of 1 unit (or pixel).
The “if” conditional block located inside the loop check the case that “bot1.position” is something other than 0. If it is not 0, we abort this script because program should draw a different triangle. It is very possible that the position of the robot changes during the execution of this drawing routine and this conditional checks for that situation. If we didn’t exit this routine, we could have multiple routines drawing triangles simultaneously which would be inaccurate. So we would exit the current routine if it no longer corresponds to the bot’s current orientation.
We should change the current sprite image to another to take care of this job. We will choose a very simple sprite: a brightly colored square made with the Scratch sprite editor.
If you launch the program and the bot is standing upright on its base, this result will be displayed in the Scratch screen:
Now let's add another sprite. This sprite will be the bot silhouette to indicate what we are drawing.
We can see that now we have two sprites, one of them made the dynamic triangle drawing as well as the bot silhouette.
Now we will establish the structures that display the inclination of the bot according to the accelerometer readings received.
Notice that we place the center of the sprite at (-100, 0) to avoid overlapping the triangle and lines we are drawing. The other instructions will rotate the sprite to the correct angle.
So the result will display like this:
Let's see how we can draw the resulting triangle when the bot is not completely upright ( "bot1.position" has the value 1, 2, 3 or 4). We will describe the procedure for the case when the value of the variable is 1. We will focus on the “Trigon” sprite:
The first portion of code is the same as when “bot1.position” has the value 0.
However, we introduce the value of the angle we want to draw here (57° in this case; the average between 67.5° and 45°) in the variable "angle", which allow us to perform trigonometric calculations for different measures of the two cathetus (the two sides of triangle adjacent to the right angle).
Now we will draw the triangle. In this case we begin by drawing the hypotenuse (remember that "hypotenuse Bis", "next cathetus Bis" and "opposite cathetus Bis" variables are used to display messages indicating centimeters). The other variables are used for internal calculations.
In the next step we display a message indicating the hypotenuse in centimeters while the program is drawing it.
Then we create a loop to draw the hypotenuse. The procedure is the same as drawing the line, except that the angle is different. In this case,we have done a subtraction between the angle and 90º, because in Scratch, the 0° and 180° endpoints are in positioned vertically rather than horizontally.
Once we have drawn the hypotenuse, is the time to draw the opposite cathetus (the side opposite the angle of inclination):
The height of the opposite side is calculated using the sine function. Knowing that "sin a = height of opposite cathetus / hypotenuse", we can solve for "opposite cathetus” using “hypotenuse * sin a". We store the relevant values in variables for the internal calculation and displaying messages.
The next step is to display the message with the value of the opposite cathetus height and draw it following the previous processes. But in this case, the direction of the "virtual pen" will be 180 (down).
Time to next cathetus:
The side of the right triangle adjacent to the angle of inclination is the “next cathetus”. The next cathetus is proportional to the cosine of the angle. So the procedure is the similar to the previous but uses the cosine function. Well, the code for the value 1 of “bot1.position” ends here.
Let's see the result of our work:
The procedure for programming the other cases provided by the bot position would be similar, changing the angle appropriately in each particular case.
You can see the source code in the attached file.