• No se han encontrado resultados

An´ alisis estad´ıstico

3. Material y m´ etodos

3.4. An´ alisis estad´ıstico

//In Javascript var loop_ctr;

loop_ctr = 0;

while(loop_ctr < 10) {

//loop body loop_ctr++;

}

Frequently, within a processor application, the program begins with system initialization actions. Once initialization activities are complete, the processor enters a continuous loop. This may be accomplished using the following code fragment.

while(1) {

}

In JavaScript, this is sort of synchronous programming is typically avoided and often replaced with use of the JavaScript timers. For example, it is possible to use a JavaScript timer to call a function once every second.

var mytimer = setInterval(myfunction, 1000);

Here “myfunction” is passed to setInterval as the first argument and the second argument is the number of milliseconds between when the timer goes off to call the function. “myfunction” will be called without any arguments provided to it. “mytimer” contains a reference to the timer that can be used to disable the timer using clearInterval. We’ll discuss JavaScript timers in a bit more detail later in this chapter.

2.4.3 DECISION PROCESSING

There are a variety of constructs that allow decision making. These include the following:

• the if statement,

• the if–else construct,

• the if–else if–else construct, and the

start

finish

Figure 2.7: Autonomous robot within maze.

• switch statement.

The if statement will execute the code between an open and close bracket set should the condition within the if statement be logically true.

Example: We use autonomous, maze navigating robots several times throughout the book as electronic systems examples. An autonomous, maze navigating robot is equipped with sensors to detect the presence of maze walls and navigate about the maze. The robot has no prior knowledge about the maze configuration. It uses the sensors and an onboard algorithm to determine the robot’s next move. The overall goal is to navigate from the starting point of the maze to the end point as quickly as possible without bumping into maze walls as shown in Figure2.7. Maze walls are usually painted white to provide a good, light reflective surface; whereas, the maze floor is painted matte black to minimize light reflections.

In several examples we use the Graymark Blinky 602A robot. This robot platform is quite popular. There are several videos of this robot available online. We refer to it as “Blinky.” Suppose our goal is to have Blinky find a path through a maze as shown in Figure2.7. Blinky is equipped with two wheels driven by DC motors. When moving through the maze, Blinky must safeguard itself from bumping against the walls. For this purpose, we equip Blinky with three IR sensors. The IR sensors provide a voltage output which depends on the distance of the sensor from the reflecting surface.

Later in the chapter we develop the algorithm to allow Blinky to navigate the maze. To help develop the algorithm, a light emitting diode (LED) is connected to a digital input/output pin on BeagleBone. The robot’s center infrared (IR) sensor is connected to an analog–to–digital

2.4. FUNDAMENTAL PROGRAMMING CONCEPTS 35 converter (ADC) input pin on BeagleBone. The IR sensor provides a voltage output that is inversely proportional to distance of the sensor from the maze wall. It is desired to illuminate the LED if the robot is within 10 cm of the maze wall. The sensor’s output is too large to be directly applied to BeagleBone. We place a voltage divided network between the sensor and BeagleBone to remedy this situation. With the voltage divider network, the sensor provides an output voltage of 1.25 VDC at the 10 cm range. The Bonescript library’s analogRead function provides a normalized analog voltage reading from 0 to 1. A reading of 1 corresponds to the maximum ADC system voltage of 1.8 VDC.

Therefore, the ADC will report a reading of 0.694 (1.25 V DC/1.80 V DC) when the maze wall is 10 cm from the robot.

The following if statement construct will implement this LED indicator. We use pseudocode to illustrate the concept. We provide the actual code to do this later in the chapter.

if (center_sensor_output > 0.694) //Center IR sensor voltage //greater than 1.25 VDC {

led_pin = logic_high; //illuminate LED connected //to led_pin

}

In the example provided, there is no method to turn off the LED once it is turned on. This will require the else portion of the construct as shown in the next code fragment.

if (center_sensor_output > 0.694) //Center IR sensor voltage //greater than 1.25 VDC {

led_pin = logic_high; //illuminate LED connected //to led_pin

} else {

led_pin = logic_low; //turn LED off

}

The if–else if–else construct may be used to implement a three LED system. In this example, the left, center, and right IR sensors are connected to three different analog–to–digital converter pins on BeagleBone. Also, three different LEDs are connected to digital output pins on BeagleBone.

The following pseudocode fragment implements this LED system.

if (left_sensor_output > 0.694) //Left IR sensor voltage //greater than 1.25 VDC

{

left_led_pin = logic_high; //illuminate LED connected //to left_led_pin

}

else if (center_sensor_output > 0.694) //Center IR sensor voltage //greater than 1.25 VDC {

center_led_pin = logic_high; //illuminate LED connected //to center_led_pin

}

else if (right_sensor_output > 0.694) //Right IR sensor voltage //greater than 1.25 VDC {

right_led_pin = logic_high; //illuminate LED connected //to right_led_pin

}

else {

left_led_pin = logic_low; //turn LEDs off center_led_pin = logic_low;

right_led_pin = logic_low;

}

The switch statement is used when multiple if–else conditions exist. Each possible condition is specified by a case statement. When a match is found between the switch variable and a specific case entry, the statements associated with the case are executed until a break statement is encountered. In general, the break terminates the execution of the nearest enclosing do, switch or while statement and program control passes to the next program statement following the break. In the switch statement, this ensures only a single case of the switch statement is executed.

Example: Suppose an eight bit variable “robot_status” is periodically updated to reflect the current status of the robot (e.g., low battery power, maze walls in the robot’s path, etc.). Each bit in the register represents a different robot status item. In response to a change the status the robot must complete status related items. A switch statement may be used to process the multiple possible actions in an orderly manner.

2.4. FUNDAMENTAL PROGRAMMING CONCEPTS 37

if(new_robot_status != old_robot_status) //check for status change switch(new_robot_status)

{ //process change in status

case 0x01: //new_robot_status bit 0

//related actions break;

case 0x02: //new_robot_status bit 1

//related actions break;

case 0x04: //new_robot_status bit 2

//related actions break;

case 0x08: //new_robot_status bit 3

//related actions break;

case 0x10: //new_robot_status bit 4

//related actions break;

case 0x20: //new_robot_status bit 5

//related actions break;

case 0x40: //new_robot_status bit 6

//related actions break;

case 0x80: //new_robot_status bit 7

//related actions break;

default:; //all other cases

} //end switch(new_robot_status)

} //end if new_robot_status old_robot_status = new_robot_status; //update old_robot_status

That completes our brief overview of the JavaScript and C programming languages. In the next section, we provide a bit more detail on the Bonescript library and the environment in which we will be using it. You will see how this development environment provides a user–friendly method of quickly developing code applications for BeagleBone. But first, let’s discuss some of the basics of programming our chosen JavaScript interpreter.

2.5 PROGRAMMING IN JAVASCRIPT USING NODE.JS

In this section we provide an admittedly brief introduction to Node.js. However, we provide pointers to several excellent sources on these topics at the end of the chapter. Node.js is an important topics since Bonescript is written specifically for it. As you will recall, Bonescript is the user–friendly

“website” interface to write application programs for BeagleBone. Since JavaScript was specifically developed to quickly implement websites, it was a natural choice to implement Bonescript. JavaScript is an interpreted language. This means the code is not compiled. Instead a JavaScript program is a script or listing of pre–built functions to quickly implement dynamic user interactions within a website.

Node.js is an implementation of a JavaScript interpreter for running on the web host, rather than within your web browser. It was developed to implement event driven programming techniques. In our discussion of Node.js we use a restaurant example to gain a general understanding of event driven programming. Having a fundamental understanding of JavaScript and Node.js will allow you to extend the features and capabilities of Bonescript. It is important to emphasize that Bonescript is an open source library. We are counting on the user community to expand the features of Bonescript. If there is a feature you need, please develop it and share it with the BeagleBoard.org community.

2.5.1 JAVASCRIPT

As previously illustrated, JavaScript is very similar in syntax to the C programming language. It consists of two basic parts: the syntax and the object model. The syntax can be divided into six basic areas:

• comments

• conditionals

• loops

• operators

• functions

2.5. PROGRAMMING IN JAVASCRIPT USING NODE.JS 39

• variables

The first four items are virtually identical to the C programming language previously discussed.

We discussed function writing and the declaration of variables in the section on Bonescript. It should be noted that strong typed variables are available in JavaScript 2.0 [Vander Veer, Pollock, Kiessling, Hughes–Crocher].

The object model for JavaScript within web browsers is the document object module (DOM).

The components of a webpage are referred to as self–contained or encapsulated modules. Module encapsulation includes the object’s data, properties, methods and event handlers. Predefined modules are available in HTML, web browsers and JavaScript. Also, custom modules may be implemented using JavaScript[Vander Veer].