Exercise 6: Servo Code

Expected duration: 45 minutes

Introduction:

In this exercise, you will delve into the code for operating the Servo which you hooked up in the previous Hardware Exercise. Since we have not done any of the interconnecting network code yet, we'll just get the Servo working from the switches to show how servos work and then connect the sensor to the servo in the next exercise.

Servos are driven via pulse width modulation, and in the Sun SPOT SDK, we can simply set a value on the servo which corresponds to a specific pulse. The Servos that we are using have a defined 'midpoint' pulse of 1500 µSeconds. Sending a value of 1500 will move the servo to its mid-point. These servos also have defined minimum and maximum rotational values of 500 µSeconds and 2500 µSeconds, respectively. These values have been defined for easy reference:

 53     private static final int CENTER = 1500;
 54     private static final int MAX = 2500;
 55     private static final int MIN = 500;

Steps to follow:

  1. Open the ServoMover class in NetBeans
  2. Modify how the servo moves

Exercise

Open the J1HOL-Servoproject in NetBeans, and open the ServoMover class. This is the class that defines the servo, and handles moving the servo based on input it receives. Since the Bend Sensor is not yet connected via the network, we will be operating the servo manually during this part of the Lab.

In the Sun SPOT SDK, Servos are supported directly in the SDK, so we don't need to do any special handling aside from defining the Servo, and defining which pin we have connected the servo to. This is done in a single line of code as follows:

 68         serv = new Servo(EDemoBoard.getInstance().getOutputPins()[EDemoBoard.H0]);

This specifies that the Servo is connected to the High Current Pin H0 on the EDemoBoard. To ensure that we can actually control the servo as we expect, when the application starts, it will center the servo, then move it to its maximum and minimum rotations.

Since there is no connection to the Bend Sensor at this point, we can manually control the Servo via switch-presses on the 2 switches on the EDemoBoard. The movement of the Servo is controlled by how far the servo moves at each switch press.

124     /**
125      * Move a servo based on a value for direction
126      * @param dir 1 or 2, corresponds to switch 1 or 2 to move the servo
127      */
128     private void manualMove (int dir) {
129         final  int move = 25;
130         int servPos = serv.getValue();
131         if(dir == 1){
132             if(servPos - move > MIN){
133                 serv.setValue(servPos - move);
134             } else {
135                 serv.setValue(MIN);
136             }
137         } else {
138             if(servPos + move < MAX ){
139                 serv.setValue(servPos + move);
140             } else {
141                 serv.setValue(MAX);
142             }
143         }
144     }

Changing the value of the 'move' instance variable will effect how quickly, and how smoothly, the Servo moves. Try different values for move to see how the changes affect your servo's movement.

Once again build and deploy your application to your Sun SPOT to test the application. You must re-build and re-deploy your application to the Sun SPOT every time you make changes to your code. Again, to deploy your application using NetBeans, right-click on the Project Node and select 'Build Project + Deploy to Sun SPOT'. Choosing 'Run' will build, deploy and run your application at once.

Summary:

You have now completed all the end pieces of hardware and software development needed for the Sensor Network system we are biulding. All that is left is to enable the two Sun SPOTs to communicate via their radio network interfaces so that sensor data read from the Bend Sensor can be translated to servo movements on the other side. That will be covered in the next exercise.

View the solution

Go to next exercise