Finished the sensor report thread.
This commit is contained in:
@@ -16,7 +16,8 @@
|
||||
package ve.ucv.ciens.ccg.nxtcam;
|
||||
|
||||
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
|
||||
import ve.ucv.ciens.ccg.nxtcam.network.LCPThread;
|
||||
import ve.ucv.ciens.ccg.nxtcam.network.NxtBTCommThread;
|
||||
import ve.ucv.ciens.ccg.nxtcam.network.SensorReportThread;
|
||||
import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread;
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
||||
@@ -40,7 +41,8 @@ public class CamActivity extends Activity{
|
||||
private CameraPreview cPreview;
|
||||
private CameraSetupTask camSetupTask;
|
||||
private VideoStreamingThread imThread;
|
||||
private LCPThread botThread;
|
||||
private NxtBTCommThread botThread;
|
||||
private SensorReportThread sensorThread;
|
||||
private String serverIp;
|
||||
|
||||
/*******************
|
||||
@@ -57,8 +59,11 @@ public class CamActivity extends Activity{
|
||||
imThread = new VideoStreamingThread(serverIp);
|
||||
imThread.start();
|
||||
|
||||
botThread = new LCPThread(serverIp);
|
||||
botThread = new NxtBTCommThread(serverIp);
|
||||
botThread.start();
|
||||
|
||||
sensorThread = new SensorReportThread(serverIp);
|
||||
sensorThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,14 +97,14 @@ public class CamActivity extends Activity{
|
||||
camSetupTask = new CameraSetupTask();
|
||||
camSetupTask.execute();
|
||||
|
||||
// imThread.start();
|
||||
// TODO: resumethe imThread, botThread and sensorThread objects.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(){
|
||||
super.onPause();
|
||||
|
||||
// TODO: pause the imThread and botThread objects.
|
||||
// TODO: pause the imThread, botThread and sensorThread objects.
|
||||
|
||||
if(cPreview != null){
|
||||
cPreview.removePreviewCallback();
|
||||
@@ -113,9 +118,12 @@ public class CamActivity extends Activity{
|
||||
super.onDestroy();
|
||||
imThread.finish();
|
||||
imThread = null;
|
||||
|
||||
|
||||
botThread.finish();
|
||||
botThread = null;
|
||||
|
||||
sensorThread.finish();
|
||||
sensorThread = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,24 +23,21 @@ import ve.ucv.ciens.ccg.nxtcam.network.protocols.MotorMasks;
|
||||
import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue;
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||
|
||||
public class LCPThread extends Thread{
|
||||
public class NxtBTCommThread extends Thread{
|
||||
private static final String TAG = "LCP_THREAD";
|
||||
private static final String CLASS_NAME = LCPThread.class.getSimpleName();
|
||||
private static final String CLASS_NAME = NxtBTCommThread.class.getSimpleName();
|
||||
|
||||
private boolean done;
|
||||
private boolean reportSensors;
|
||||
private BTCommunicator btComm;
|
||||
private MotorControlThread motorControl;
|
||||
private SensorReportThread sensorReport;
|
||||
|
||||
private MotorEventQueue queue;
|
||||
|
||||
public LCPThread(String serverIp){
|
||||
public NxtBTCommThread(String serverIp){
|
||||
super("Robot Control Main Thread");
|
||||
btComm = BTCommunicator.getInstance();
|
||||
done = false;
|
||||
motorControl = new MotorControlThread(serverIp);
|
||||
sensorReport = new SensorReportThread(serverIp);
|
||||
queue = MotorEventQueue.getInstance();
|
||||
}
|
||||
|
||||
@@ -49,7 +46,6 @@ public class LCPThread extends Thread{
|
||||
MotorEvent event;
|
||||
byte[] msg = new byte[2];
|
||||
|
||||
sensorReport.start();
|
||||
motorControl.start();
|
||||
|
||||
then = System.currentTimeMillis();
|
||||
@@ -63,21 +59,18 @@ public class LCPThread extends Thread{
|
||||
}
|
||||
}
|
||||
|
||||
if((reportSensors = sensorReport.isConnected())){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".run() :: Sensor data can be reported.");
|
||||
}else{
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: Thread sensorReport could not connect to the server.");
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: Sensor data will not be reported to server app.");
|
||||
}
|
||||
|
||||
while(!done){
|
||||
if(btComm.isBTEnabled() && btComm.isConnected()){
|
||||
msg[0] = 0x00;
|
||||
msg[1] = 0x00;
|
||||
|
||||
event = queue.getNextEvent();
|
||||
|
||||
try{
|
||||
// Set the motor bit.
|
||||
msg[0] = (event.getMotor() == motor_t.MOTOR_A) ? MotorMasks.MOTOR_A: ((event.getMotor() == motor_t.MOTOR_B) ? MotorMasks.MOTOR_B: MotorMasks.MOTOR_C);
|
||||
msg[0] |= (event.getMotor() == motor_t.MOTOR_A) ? MotorMasks.MOTOR_A : 0;
|
||||
msg[0] |= (event.getMotor() == motor_t.MOTOR_B) ? MotorMasks.MOTOR_B : 0;
|
||||
msg[0] |= (event.getMotor() == motor_t.MOTOR_C) ? MotorMasks.MOTOR_C : 0;
|
||||
// Set the direction bit.
|
||||
if(event.getPower() > 0) msg[0] |= MotorMasks.DIRECTION;
|
||||
// TODO: Set the recenter bits.
|
||||
@@ -94,10 +87,6 @@ public class LCPThread extends Thread{
|
||||
}catch(IOException io){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException sending message to the robot: " + io.getMessage());
|
||||
}
|
||||
|
||||
if(reportSensors){
|
||||
|
||||
}
|
||||
}else{
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available.");
|
||||
break;
|
||||
@@ -1,7 +1,7 @@
|
||||
package ve.ucv.ciens.ccg.nxtcam.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||
@@ -14,21 +14,36 @@ public class SensorReportThread extends Thread{
|
||||
private Socket socket;
|
||||
private String serverIp;
|
||||
private boolean done;
|
||||
private ObjectOutputStream writer;
|
||||
private OutputStream writer;
|
||||
private boolean connected;
|
||||
private BTCommunicator btComm;
|
||||
|
||||
public SensorReportThread(String serverIp){
|
||||
super("Sensor Report Thread");
|
||||
this.serverIp = serverIp;
|
||||
done = false;
|
||||
connected = false;
|
||||
btComm = BTCommunicator.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
byte[] lightReading;
|
||||
|
||||
if(connectToServer()){
|
||||
while(!done){
|
||||
|
||||
if(btComm.isBTEnabled() && btComm.isConnected()){
|
||||
try{
|
||||
lightReading = btComm.readMessage(1);
|
||||
writer.write(lightReading);
|
||||
}catch(IOException io){
|
||||
Logger.log_e(TAG, CLASS_NAME + "run(): IOException: " + io.getMessage());
|
||||
done = true;
|
||||
}
|
||||
}else{
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: Could not connect to the server.");
|
||||
@@ -43,7 +58,7 @@ public class SensorReportThread extends Thread{
|
||||
boolean connected;
|
||||
try{
|
||||
socket = new Socket(serverIp, ProjectConstants.SENSOR_REPORT_PORT);
|
||||
writer = new ObjectOutputStream(socket.getOutputStream());
|
||||
writer = socket.getOutputStream();
|
||||
connected = true;
|
||||
}catch(IOException io){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: IOException caught: " + io.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user