KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > cruisecontrol > CruiseControl


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.cruisecontrol;
32
33 /** The active object for the controller,
34  * checks the speed of the car when it is on
35  */

36 public class CruiseControl implements org.objectweb.proactive.RunActive {
37
38   /** */
39   final static int ACTIVE = 1;
40   /** */
41   final static int INACTIVE = 0;
42   /** State of the controller */
43   int state = INACTIVE;
44   /** The desired speed of the controller */
45   double desiredSpeed = 0;
46   /** The current acceleration to applies to the car
47    * when the controller is on
48    */

49   double acc = 0;
50   /** Reference onto the dispatcher of the Applet */
51   Interface father;
52
53
54   /** No-arg constructor for tha ProActive */
55   public CruiseControl() {
56
57   }
58
59
60   /** Constructor which intializes the father field */
61   public CruiseControl(Interface m_father) {
62     father = m_father;
63   }
64
65
66   /** Tuns on the controller, sets the new desired speed and sets the acceleration to the current acceleration of the car */
67   public void controlOn(double m_desiredSpeed, double m_acc) {
68     //System.out.println("control On : Cruise Control : "+ this.state);
69

70     if (this.state == INACTIVE) {
71       desiredSpeed = m_desiredSpeed;
72       this.acc = m_acc;
73       father.setDesiredSpeed(desiredSpeed);
74       //System.out.println("control On : Cruise Control : Father "+father.acc);
75

76       //System.out.println("control On : Cruise Control"+acc);
77

78       this.state = ACTIVE;
79     }
80   }
81
82
83   /** Turns off the controller,
84    * sets the desired speed to null
85    * and calls the dispatcher to repaint the cruisePane */

86   public void controlOff() {
87     if (state == ACTIVE) {
88       state = INACTIVE;
89       desiredSpeed = 0;
90       father.setDesiredSpeed(0);
91       father.brake();
92     }
93   }
94
95
96
97   ////////////////////////////////////////////////////////////
98
/*** Turn off the controller when the car is stopping */
99   public void engineOff() {
100     state = INACTIVE;
101     desiredSpeed = 0;
102     acc = 0;
103     father.setDesiredSpeed(0);
104
105   }
106
107
108   //////////////////////////////////////////////////////////////
109
/** Asks the dispatcher the current speed of the car */
110   public double getDesiredSpeed() {
111     return desiredSpeed;
112   }
113
114
115   /** Changes the desired speed to the new one */
116   public void setDesiredSpeed(double m_speed) {
117     if (this.state == ACTIVE)
118       this.desiredSpeed = m_speed;
119   }
120
121
122   //////////////////////////////////////////////////////////////
123
/** Increases the desired speed with one
124    * and asks the dispatcher to display the new value
125    */

126   public void accelerate() {
127     if (this.state == ACTIVE) {
128       this.desiredSpeed += 1;
129       father.setDesiredSpeed(this.desiredSpeed);
130     }
131
132   }
133
134
135   /** Decreases the desired speed with one
136    * and asks the dispatcher to display the new value
137    */

138   public void decelerate() {
139     if (this.state == ACTIVE) {
140       this.desiredSpeed -= 1;
141       father.setDesiredSpeed(this.desiredSpeed);
142     }
143   }
144
145
146   ///////////////////////////////////////////////////////////////
147
/** Computes the new acceleration to apply to the car
148    * when the controller is on
149    */

150   public void calculateAcceleration() {
151     double currentSpeed;
152     currentSpeed = father.getSpeed().doubleValue();
153     //System.out.println("Cruise Control : calculateAcceleration >>"+ currentSpeed);
154
//System.out.println("Cruise Control : calculateAcceleration >>"+ desiredSpeed);
155
//System.out.println("Cruise Control : calculateAcceleration : State >>"+ this.state);
156
double delta = Math.abs(currentSpeed - desiredSpeed);
157     if (this.state == ACTIVE) {
158       if (delta < 0.4)
159         ;
160       else if (currentSpeed < desiredSpeed) {
161         if (delta < 2)
162           this.acc += 0.5;
163         else if (delta < 8)
164           this.acc += 1;
165         else
166           this.acc += 2.5;
167         //System.out.println("CruiseControl : calculateAcceleration >> this.acc : "+ this.acc);
168
father.setAcceleration(this.acc);
169       } else {
170         if (delta < 2)
171           this.acc -= 0.5;
172         else if (delta < 8)
173           this.acc -= 1;
174         else
175           this.acc -= 1.8;
176         if (this.acc < 0) {
177           father.setAcceleration(0);
178           this.acc = 0;
179           this.state = INACTIVE;
180           father.deactiveControl();
181           //System.out.println("Cruise Control Off : Speed too high >>>");
182
} else
183           father.setAcceleration(this.acc);
184       }
185     }
186   }
187
188
189   /** Sets the car acceleration to the new one */
190   public void setAcceleration(double m_acc) {
191     if (this.state == ACTIVE)
192       father.setAcceleration(m_acc);
193   }
194
195   
196   /** the routine Live which runs only the most recent call */
197   public void runActivity(org.objectweb.proactive.Body body) {
198     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
199     //System.out.println ("Starts live in object ActiveCruise");
200
while (body.isActive()) {
201       try {
202         Thread.sleep(1800);
203         //System.out.println("CruiseControl : Method live >>"+desiredSpeed);
204
// serve Requests before calculating Acceleration
205
if (this.state == ACTIVE) {
206           this.calculateAcceleration();
207         }
208         //System.out.println("CruiseControl : Cruise Control Live Routine : End");
209
service.flushingServeYoungest();
210       } catch (InterruptedException JavaDoc e) {
211       }
212     }
213   }
214 }
215
216 // The END
217
Popular Tags