1 31 package org.objectweb.proactive.examples.cruisecontrol; 32 33 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 43 int state = INACTIVE; 44 45 double desiredSpeed = 0; 46 49 double acc = 0; 50 51 Interface father; 52 53 54 55 public CruiseControl() { 56 57 } 58 59 60 61 public CruiseControl(Interface m_father) { 62 father = m_father; 63 } 64 65 66 67 public void controlOn(double m_desiredSpeed, double m_acc) { 68 70 if (this.state == INACTIVE) { 71 desiredSpeed = m_desiredSpeed; 72 this.acc = m_acc; 73 father.setDesiredSpeed(desiredSpeed); 74 76 78 this.state = ACTIVE; 79 } 80 } 81 82 83 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 99 public void engineOff() { 100 state = INACTIVE; 101 desiredSpeed = 0; 102 acc = 0; 103 father.setDesiredSpeed(0); 104 105 } 106 107 108 110 public double getDesiredSpeed() { 111 return desiredSpeed; 112 } 113 114 115 116 public void setDesiredSpeed(double m_speed) { 117 if (this.state == ACTIVE) 118 this.desiredSpeed = m_speed; 119 } 120 121 122 126 public void accelerate() { 127 if (this.state == ACTIVE) { 128 this.desiredSpeed += 1; 129 father.setDesiredSpeed(this.desiredSpeed); 130 } 131 132 } 133 134 135 138 public void decelerate() { 139 if (this.state == ACTIVE) { 140 this.desiredSpeed -= 1; 141 father.setDesiredSpeed(this.desiredSpeed); 142 } 143 } 144 145 146 150 public void calculateAcceleration() { 151 double currentSpeed; 152 currentSpeed = father.getSpeed().doubleValue(); 153 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 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 } else 183 father.setAcceleration(this.acc); 184 } 185 } 186 } 187 188 189 190 public void setAcceleration(double m_acc) { 191 if (this.state == ACTIVE) 192 father.setAcceleration(m_acc); 193 } 194 195 196 197 public void runActivity(org.objectweb.proactive.Body body) { 198 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 199 while (body.isActive()) { 201 try { 202 Thread.sleep(1800); 203 if (this.state == ACTIVE) { 206 this.calculateAcceleration(); 207 } 208 service.flushingServeYoungest(); 210 } catch (InterruptedException e) { 211 } 212 } 213 } 214 } 215 216 | Popular Tags |