Project

General

Profile

Revision 625

Added by Greg Tress about 16 years ago

Fixed battery bars

View differences:

trunk/code/projects/colonet/client/BatteryIcon.java
92 92
	* @param level The battery level as returned by the robot.
93 93
	* @returns The representable battery percentage.
94 94
	*/
95
	private int convert (int level) {
96
		// TODO: make this a forreals conversion.
97
		return (int) (100.0 * level / 170);
95
	public int convert (int x) {
96
	    final int DEAD = 110;
97
	    final int FULL = 160;
98
	    if (x < DEAD)
99
	        return 1;
100
		return (int) ((x+DEAD) * 100.0 / (FULL-DEAD));
98 101
	}
99 102
}
trunk/code/projects/colonet/client/Colonet.java
515 515
	* @see ColonetServerInterface#sendBatteryRequest(int)
516 516
	*/
517 517
	public void parseBattery (String line) {
518
		String [] str = line.split(" ");
519
		int botNum = Integer.parseInt(str[2]);
520
		int level = Integer.parseInt(str[3]);
518
	    int botNum, level;
519
	    System.out.println("Got battery update:" + line);
520
	    try {
521
		    String [] str = line.split(" ");
522
		    botNum = Integer.parseInt(str[2]);
523
		    level = Integer.parseInt(str[3]);
524
		} catch (NumberFormatException e) {
525
		    System.out.println("Could not parse battery update");
526
		    return;
527
		}
521 528

  
522
		System.out.println("Got battery update:" + line);
523 529
		RobotIcon r = robotIcons.get(botNum);
524 530
		if (r != null) {
525
		    r.battery = level;
531
		    r.battery = batteryIcon.convert(level);  // set contextual battery meter
532
		    batteryIcon.setLevel(level); // set solo battery meter
533
		    System.out.println("Set " + r.id + " battery value to " + level);
526 534
		}
527 535
		repaint();
528 536
	}
......
540 548
			int x = Integer.parseInt(str[i+1]);
541 549
			int y = Integer.parseInt(str[i+2]);
542 550
			RobotIcon newIcon = new RobotIcon(id, x, y);
551
			// Save previous battery value
552
			if (robotIcons.get(id) != null)
553
			    newIcon.battery = robotIcons.get(id).battery;
543 554
			if (newIcon.id >= 0) {
544 555
				newIcon.color = Color.GREEN;
545 556
			}
......
805 816
	*
806 817
	*/
807 818
	class DataUpdater extends Thread {
808
		final int DATAUPDATER_DELAY = 500;
819
		final int DATAUPDATER_DELAY = 3000;
809 820
		public DataUpdater () {
810 821
			super("Colonet DataUpdater");
811 822
		}
812 823

  
813
		public void run () {
824
		public synchronized void run () {
814 825
			String line;
815 826
			while (true) {
816 827
				try {
......
830 841
					Thread.sleep(DATAUPDATER_DELAY);
831 842
				} catch (InterruptedException e) {
832 843
					return;
844
				} catch (NullPointerException e) {
845
				    // This probably indicates that an object was destroyed by shutdown.
846
				    return;
833 847
				}
834 848
			}
835 849
		}
......
932 946
					bufferedGraphics.drawOval(px-RADIUS, py-RADIUS, 2*r.RADIUS, 2*r.RADIUS);
933 947
					// Battery
934 948
					if (r.battery >= 0) {
935
					    int pixels = r.battery * BATTERY_WIDTH / 160;
936
						bufferedGraphics.setColor(Color.YELLOW);
949
					    int pixels = r.battery * BATTERY_WIDTH / 160;  // 160 should be fully charged
950
						if (r.battery > 50)
951
						    bufferedGraphics.setColor(Color.GREEN);
952
						else if (r.battery > 25)
953
						    bufferedGraphics.setColor(Color.YELLOW);
954
						else
955
						    bufferedGraphics.setColor(Color.RED);
937 956
						bufferedGraphics.fillRect(px+20, py+20, pixels, BATTERY_HEIGHT);
938 957
						bufferedGraphics.setColor(Color.BLACK);
939 958
						bufferedGraphics.drawRect(px+20, py+20, BATTERY_WIDTH, BATTERY_HEIGHT);
......
1177 1196
				val = dy * 1024 / SIDE + 255;
1178 1197
			else
1179 1198
				val = 255;
1199
		    // Normalize to 0-255
1180 1200
			return val * getSpeed() / 255;
1181 1201
		}
1182 1202
		
......
1195 1215
				val = -255;
1196 1216
			else
1197 1217
				val = dy * 1024 / SIDE - 255;
1218
			// Normalize to 0-255
1198 1219
			return val * getSpeed() / 255;
1199 1220
		}
1221
		
1222
		private int eliminateDeadZone (int x) {
1223
		    final int START = 150;
1224
		    int val;
1225
		    if (x == 0)
1226
		        return 0;
1227
		    if (x > 0)
1228
			    val = (int) ((1 - 1.0 * START / 255) * x + START);
1229
			else
1230
			    val = (int) ((1 - 1.0 * START / 255) * x - START);
1231
			return val;
1232
		
1233
		}
1200 1234

  
1201 1235
		public void paint (Graphics g) {
1202 1236
			// Clear image
......
1266 1300

  
1267 1301
			String motor1_string;
1268 1302
			String motor2_string;
1269
			int motor1 = getMotorL();
1270
			int motor2 = getMotorR();
1303
			int motor1 = eliminateDeadZone(getMotorL());
1304
			int motor2 = eliminateDeadZone(getMotorR());
1271 1305
			
1272 1306
			if (motor1 > 0) {
1273 1307
			    motor1_string = " 1 " + motor1;

Also available in: Unified diff