]> rtime.felk.cvut.cz Git - orte.git/commitdiff
ROBOT_DEMO: implement voltage monitor
authorMartin Vajnar <martin.vajnar@gmail.com>
Fri, 23 Aug 2013 11:49:46 +0000 (13:49 +0200)
committerMartin Vajnar <martin.vajnar@gmail.com>
Fri, 23 Aug 2013 11:49:46 +0000 (13:49 +0200)
orte/Robot_Demo/src/org/ocera/orte/demo/HokuyoScanType.java
orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java
orte/Robot_Demo/src/org/ocera/orte/demo/PwrVoltageSubscribe.java
orte/Robot_Demo/src/org/ocera/orte/demo/PwrVoltageType.java
orte/Robot_Demo/src/org/ocera/orte/demo/SpeedMotionType.java

index be16593eb9859758a58b9a4ee516048220f6f286..c5e99316da0da3d431d0dddc0e5f8825281e40c0 100644 (file)
@@ -31,13 +31,4 @@ public class HokuyoScanType extends MessageData {
        public int getMaxDataLength() {
                return 681 * ORTEConstant.SHORT_FIELD_SIZE;
        }
-
-       public String toString() {
-         String data = new String();
-         for(int i = 0; i < hokuyo.length; i++) {
-           data += i + ": " + hokuyo[i] + ", ";
-         }
-         data = data.substring(0, data.length()-2);
-         return data;    
-       }
 }
index 61d4627521d5bcb833437286cc8107f8d32c4aa7..36f38c090670934e15c92c70b89bd1b6a81a7f63 100644 (file)
@@ -12,6 +12,8 @@ import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
 import android.view.Menu;
@@ -19,15 +21,41 @@ import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
+import android.widget.EditText;
  
 public class MainActivity extends Activity {
        private Dialog voltageDialog = null;
+       static EditText voltage33 = null;
+       static EditText voltage50 = null;
+       static EditText voltage80 = null;
+       static EditText voltageBAT = null;
+       static Handler dialogHandler = new Handler() {
+               @Override
+               public void handleMessage(Message msg) {
+                       double[] values = msg.getData().getDoubleArray("voltages");
+                       if (values != null) {
+                               if (voltage33 != null) {
+                                       voltage33.setText(Double.toString(values[0]));
+                               }
+                               if (voltage50 != null) {
+                                       voltage50.setText(Double.toString(values[1]));
+                               }
+                               if (voltage80 != null) {
+                                       voltage80.setText(Double.toString(values[2]));
+                               }
+                               if (voltageBAT != null) {
+                                       voltageBAT.setText(Double.toString(values[3]));
+                               }
+                       }
+               }
+    };
        
        private Manager manager = null;
     private String[] mgrs = {"192.168.1.5","192.168.1.8","192.168.1.29","10.1.1.1"};
        private MotionSpeedPublish motion_speed_publ = null;
        private MotionSpeedSubscribe motion_speed_subs = null;
        private HokuyoScanSubscribe hokuyo_scan = null;
+       private PwrVoltageSubscribe pwr_voltage = null;
     private SensorManager mSensorManager = null;
     private Sensor mGravity = null;
     private SensorEventListener accel = null;
@@ -74,6 +102,12 @@ public class MainActivity extends Activity {
                        hokuyo_view.invalidate();
                        hokuyo_item.setTitle("Start LRF");
         }
+
+        if (pwr_voltage != null && !pwr_voltage.isCancelled()) {
+               if (voltageDialog.isShowing())
+                       voltageDialog.dismiss();
+                       pwr_voltage.cancel();
+        }
         
         mWakeLock.release();
     }
@@ -100,6 +134,20 @@ public class MainActivity extends Activity {
         
         voltageDialog = new Dialog(this,R.style.voltage_dialog);
         voltageDialog.setCancelable(false);
+               voltageDialog.setContentView(R.layout.status_dialog);
+               Button okButton = (Button)voltageDialog.findViewById(R.id.button1);
+               okButton.setOnClickListener(new OnClickListener() {
+                       @Override
+                       public void onClick(View arg0) {
+                               voltageDialog.dismiss();
+                               pwr_voltage.cancel();
+                       }
+               });
+               voltage33 = (EditText)voltageDialog.findViewById(R.id.editText1);
+               voltage50 = (EditText)voltageDialog.findViewById(R.id.editText2);
+               voltage80 = (EditText)voltageDialog.findViewById(R.id.editText3);
+               voltageBAT = (EditText)voltageDialog.findViewById(R.id.editText4);
+
         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
         mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
         
@@ -183,14 +231,9 @@ public class MainActivity extends Activity {
                        
                }
                else if (item.getTitle().equals("Voltage monitor")) {
-                       voltageDialog.setContentView(R.layout.status_dialog);
-                       Button okButton = (Button)voltageDialog.findViewById(R.id.button1);
-                       okButton.setOnClickListener(new OnClickListener() {
-                               @Override
-                               public void onClick(View arg0) {
-                                       voltageDialog.dismiss();
-                               }
-                       });
+                       if (pwr_voltage == null)
+                               pwr_voltage = new PwrVoltageSubscribe(appDomain, dialogHandler);
+                       pwr_voltage.start();
                        voltageDialog.show();
                }
                else if (item.getTitle().equals("Exit")) {
index bebef861ad37d7eec5f7686f694f8acc14f28779..98529882c3bb71d7786df42b4aa8834afcb94bdd 100644 (file)
@@ -1,7 +1,91 @@
 package org.ocera.orte.demo;
 
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
+
+import org.ocera.orte.DomainApp;
+import org.ocera.orte.Subscription;
 import org.ocera.orte.SubscriptionCallback;
+import org.ocera.orte.types.MessageData;
+import org.ocera.orte.types.NtpTime;
+import org.ocera.orte.types.RecvInfo;
+import org.ocera.orte.types.SubsProp;
+
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+
+public class PwrVoltageSubscribe extends SubscriptionCallback{
 
-public class PwrVoltageSubscribe extends SubscriptionCallback {
+    public final static int IMMEDIATE    = 0x02;
+    public final static int BEST_EFFORTS = 0x01;
 
-}
+    static Handler handler;
+    
+       private Subscription sub;
+       private PwrVoltageType voltagemsg;
+       private DomainApp domainApp;
+       private SubsProp subProps;
+       
+       private boolean isCancelled = true;
+       private final ReentrantReadWriteLock controlRrwl = new ReentrantReadWriteLock(true);
+       private final ReadLock rcLock = controlRrwl.readLock();
+       private final WriteLock wcLock = controlRrwl.writeLock();
+       
+       public PwrVoltageSubscribe(DomainApp domainApp, Handler dialogHandler) {
+               handler = dialogHandler;
+               this.domainApp = domainApp;
+               
+           NtpTime deadline      = new NtpTime(0,1288490189);
+           NtpTime minSeparation = new NtpTime(1);
+           
+           voltagemsg = new PwrVoltageType(domainApp, "pwr_voltage");
+               
+           subProps = new SubsProp(voltagemsg.getTopic(),
+                                                       "pwr_voltage",                                  
+                                                       minSeparation,  
+                                                       deadline,
+                                                       IMMEDIATE,
+                                                       BEST_EFFORTS,
+                                                       0);
+       }
+       
+       public void start() {
+               wcLock.lock();
+               try {
+                       isCancelled = false;
+                       sub = domainApp.createSubscription(subProps, voltagemsg, this);
+               } finally {
+                       wcLock.unlock();
+               }
+       }
+       
+       public void cancel() {
+               wcLock.lock();
+               try {
+                       isCancelled = true;
+                       sub.destroy();
+               } finally {
+                       wcLock.unlock();
+               }
+       }
+       
+       public boolean isCancelled() {
+               rcLock.lock();
+               try {
+                       return isCancelled;
+               } finally {
+                       rcLock.unlock();
+               }
+       }
+       
+    public void callback(RecvInfo info, MessageData msg) {
+       Message message = handler.obtainMessage();
+       Bundle bundle = new Bundle();
+       
+       bundle.putDoubleArray("voltages", ((PwrVoltageType)msg).voltage.clone());
+       message.setData(bundle);
+       handler.sendMessage(message);
+    }
+}
\ No newline at end of file
index 2cbc65f6310269d38603d3868b5883028a653db8..575ae4c24e83a55fe75e25e708838a2653114690 100644 (file)
@@ -1,25 +1,34 @@
 package org.ocera.orte.demo;
 
-import org.ocera.orte.types.MessageData;
+import org.ocera.orte.*;
+import org.ocera.orte.types.*;
 
-public class PwrVoltageType extends MessageData {
 
-       @Override
-       public void read() {
-               // TODO Auto-generated method stub
-               
-       }
+public class PwrVoltageType extends MessageData
+{
+   public  double[]    voltage = new double[4];
+   
+  public PwrVoltageType(DomainApp domainApp, String newTopic) {
+    super();
+    this.setTopic(newTopic);
+    if (!domainApp.regNewDataType("pwr_voltage",getMaxDataLength())) {
+      System.out.println(":j!: cannot register data type \"pwr_voltage\"!");    
+    }
+  }
+      
+  @Override
+  public void write() {}
+  @Override
+  public void read() {
+         buffer.rewind();
+         for (int i = 0; i < 4; i++) {
+                 voltage[i] = buffer.getDouble();
+         }
+  }
 
-       @Override
-       public void write() {
-               // TODO Auto-generated method stub
-               
-       }
-
-       @Override
-       public int getMaxDataLength() {
-               // TODO Auto-generated method stub
-               return 0;
-       }
-
-}
+  @Override
+  public int getMaxDataLength() {
+    return 4 * ORTEConstant.DOUBLE_FIELD_SIZE;
+  }
+}  
index 184280f4105ade5fcf3ecf8a5ee985a9c3bbccde..b524f9aecf610e384566da51efdda7b55c2b7fcc 100644 (file)
@@ -36,14 +36,4 @@ public class SpeedMotionType extends MessageData
   {
     return 2 * ORTEConstant.SHORT_FIELD_SIZE;
   }
-
-  public String toString()
-  {
-    String data = "";
-    
-    data += (" left = " + speed[0]); 
-    data += (" right = " + speed[1]);  
-       return data;      
-  }
-  
 }