1 31 package org.objectweb.proactive.examples.cruisecontrol; 32 33 36 public class CarModel implements org.objectweb.proactive.RunActive { 37 38 39 final static int ACTIVE = 1; 40 41 final static int INACTIVE = 0; 42 43 final static double deltaT = 1; 44 45 final static double g = 9.8; 46 47 final static int m = 1000; 48 49 public int state = INACTIVE; 50 51 double acc = 0; 52 53 double speed = 0; 54 55 double distance = 0; 56 57 private static double coeff = 0.31; 58 59 private double alpha = 0; 60 61 Interface father; 62 63 64 65 public CarModel() { 66 67 } 68 69 70 71 public CarModel(Interface m_father) { 72 father = m_father; 73 } 74 75 76 78 79 public void engineOn() { 80 if (state == INACTIVE) { 82 state = ACTIVE; 83 } 85 } 86 87 88 91 public void engineOff() { 92 state = INACTIVE; 93 father.setSpeed(0); 94 speed = 0; 95 acc = 0; 96 } 97 98 99 100 101 103 104 public void setSpeed(double m_speed) { 105 this.speed = m_speed; 106 } 107 108 109 110 public Double getSpeed() { 111 return new Double (this.speed); 112 } 113 114 115 117 120 public void calculateSpeed(double m_newAcc) { 121 if (this.state == ACTIVE) { 122 if ((m_newAcc <= 50) && (m_newAcc >= 0)) { 124 speed = speed + m_newAcc * deltaT - coeff * speed * deltaT - Math.sin(alpha) * 4 * g * deltaT; 125 if (this.speed < 0.005) 126 this.speed = 0; 127 128 } 129 distance += speed * deltaT / 3600; 130 father.setSpeed(speed); 131 father.setDistance(distance); 132 135 } 136 139 } 140 141 142 144 145 public void incAcceleration(double m_acc) { 146 if (this.state == ACTIVE) { 147 if (((this.acc < 50) && (m_acc > 0)) || ((this.acc > 0) && (m_acc < 0))) 148 this.acc += m_acc; 149 } 151 154 } 155 156 157 158 public void setAcceleration(double m_acc) { 159 this.acc = m_acc; 161 } 164 165 166 167 public void brake() { 168 acc = 0; 169 if (speed > 0.01) { 170 } else { 171 speed = 0; 172 } 173 } 174 175 176 177 179 public void setAlpha(double m_alpha) { 180 alpha = m_alpha; 181 } 182 183 184 185 186 public void runActivity(org.objectweb.proactive.Body body) { 187 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 189 while (body.isActive()) { 190 try { 191 Thread.sleep(500); 192 } catch (InterruptedException e) {} 193 if (this.state == ACTIVE) 194 this.calculateSpeed(acc); 195 service.flushingServeYoungest("brake"); 196 service.flushingServeYoungest(); 197 } 198 } 199 } | Popular Tags |