git.haldean.org droidcopter / 383309b
Fix hypothetically-possible divide by zero problems, that would probably never have come up ever. Benjamin Bardin 11 years ago
5 changed file(s) with 35 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
4848 myVel = Math.sqrt(myVel);
4949 if (myVel > MAX_VEL) {
5050 Log.v(TAG, "guid, Reducing requested velocity");
51 double adjustment = MAX_VEL / myVel;
51 double adjustment;
52 if (myVel != 0) {
53 adjustment = MAX_VEL / myVel;
54 } else {
55 adjustment = 0;
56 }
5257 for (int i = 0; i < 3; i++) {
5358 mRelativeTarget[i] *= adjustment;
5459 }
44 import java.io.FileWriter;
55 import java.io.IOException;
66 import java.util.LinkedList;
7 import java.util.List;
87 import java.util.ListIterator;
98 import java.util.concurrent.atomic.AtomicInteger;
109
144143 * @see org.haldean.chopper.pilot.ChopperStatus#getBatteryLevel()
145144 */
146145 public float getBatteryLevel() {
147 return (float) mCurrBatt.get() / (float) mMaxBatt.get();
146 if (mMaxBatt.get() != 0) {
147 return (float) mCurrBatt.get() / (float) mMaxBatt.get();
148 } else {
149 return 0.0F;
150 }
148151 }
149152
150153 /* (non-Javadoc)
241244 /* int read/writes are uninterruptible, no lock needed */
242245 mCurrBatt.set(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
243246 mMaxBatt.set(intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
244 float batteryPercent = (float) mCurrBatt.get() * 100F / (float) mMaxBatt.get();
247 float batteryPercent;
248 if (mMaxBatt.get() != 0) {
249 batteryPercent = (float) mCurrBatt.get() * 100F / (float) mMaxBatt.get();
250 } else {
251 batteryPercent = 0F;
252 }
245253 if (batteryPercent <= LOW_BATT) {
246254 updateReceivers("CSYS:LOWPOWER");
247255 }
251259
252260 /* Gets a sensor manager */
253261 final SensorManager sensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
254 List<Sensor> sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
262 //List<Sensor> sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
255263 /*for (Sensor sensor : sensorList) {
256264 try {
257265 if (logwriter != null) {
326334 synchronized (mGpsExtrasLock) {
327335 timeElapsed = mGpsTimeStamp - loc.getTime();
328336 }
329 double newdalt = ((newalt - oldAlt) / (double) timeElapsed) * 1000.0;
330 setGpsField(dALT, newdalt);
331 Log.i(TAG, "new dalt: " + newdalt);
337 if (timeElapsed != 0) {
338 double newdalt = ((newalt - oldAlt) / (double) timeElapsed) * 1000.0;
339 setGpsField(dALT, newdalt);
340 Log.i(TAG, "new dalt: " + newdalt);
341 }
332342 setGpsField(ALTITUDE, newalt);
333343 }
334344 setGpsField(BEARING, loc.getBearing());
336336 //Calculate derivative errors.
337337 long timeInterval = starttime - mLastUpdate;
338338 if (timeInterval != 0) {
339 mErrors[i][2] = (err - mErrors[i][0]) * 1000.0 / (starttime - mLastUpdate);
339 mErrors[i][2] = (err - mErrors[i][0]) * 1000.0 / timeInterval;
340340 } else {
341341 mErrors[i][2] = 0.0;
342342 }
196196 double bearingRad = bearingDeg / 180.0 * Math.PI;
197197 target[0] = Math.sin(bearingRad);
198198 target[1] = Math.cos(bearingRad);
199 target[2] = verDistance / horDistance;
199 if (horDistance != 0) {
200 target[2] = verDistance / horDistance;
201 } else {
202 target[2] = verDistance;
203 }
200204
201205 //Determine magnitude, "normalize" to myVelocity
202206 double mag = Math.sqrt(Math.pow(target[0], 2) +
203207 Math.pow(target[1], 2) +
204208 Math.pow(target[2], 2));
205 double adjustment = lastDest.getVelocity() / mag;
209 double adjustment;
210 if (mag != 0) {
211 adjustment = lastDest.getVelocity() / mag;
212 } else {
213 adjustment = 0.0;
214 }
206215
207216 for (int i = 0; i < 3; i++)
208217 target[i] *= adjustment;