MotorControlThread receives and sends data.
This commit is contained in:
32
src/ve/ucv/ciens/ccg/networkdata/MotorEventACK.java
Normal file
32
src/ve/ucv/ciens/ccg/networkdata/MotorEventACK.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 Miguel Angel Astor Romero
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package ve.ucv.ciens.ccg.networkdata;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class MotorEventACK implements Serializable {
|
||||||
|
private static final long serialVersionUID = 9989L;
|
||||||
|
|
||||||
|
private boolean clientQueueIsFull;
|
||||||
|
|
||||||
|
public MotorEventACK(boolean isQueueFull){
|
||||||
|
this.clientQueueIsFull = isQueueFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClientQueueFull(){
|
||||||
|
return this.clientQueueIsFull;
|
||||||
|
}
|
||||||
|
}
|
@@ -16,6 +16,7 @@
|
|||||||
package ve.ucv.ciens.ccg.nxtcam;
|
package ve.ucv.ciens.ccg.nxtcam;
|
||||||
|
|
||||||
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
|
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
|
||||||
|
import ve.ucv.ciens.ccg.nxtcam.network.MotorControlThread;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread;
|
import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.network.LCPThread;
|
import ve.ucv.ciens.ccg.nxtcam.network.LCPThread;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||||
@@ -41,6 +42,7 @@ public class CamActivity extends Activity{
|
|||||||
private CameraSetupTask camSetupTask;
|
private CameraSetupTask camSetupTask;
|
||||||
private VideoStreamingThread imThread;
|
private VideoStreamingThread imThread;
|
||||||
private LCPThread botThread;
|
private LCPThread botThread;
|
||||||
|
private MotorControlThread motorThread;
|
||||||
private String serverIp;
|
private String serverIp;
|
||||||
|
|
||||||
/*******************
|
/*******************
|
||||||
@@ -56,6 +58,9 @@ public class CamActivity extends Activity{
|
|||||||
serverIp = intent.getStringExtra("address");
|
serverIp = intent.getStringExtra("address");
|
||||||
imThread = new VideoStreamingThread(serverIp);
|
imThread = new VideoStreamingThread(serverIp);
|
||||||
imThread.start();
|
imThread.start();
|
||||||
|
|
||||||
|
motorThread = new MotorControlThread(serverIp);
|
||||||
|
motorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -104,14 +109,17 @@ public class CamActivity extends Activity{
|
|||||||
releaseCamera();
|
releaseCamera();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy(){
|
public void onDestroy(){
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
// TODO: Destroy the network threads.
|
imThread.finish();
|
||||||
imThread = null;
|
imThread = null;
|
||||||
|
|
||||||
|
motorThread.finish();
|
||||||
|
motorThread = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed(){
|
public void onBackPressed(){
|
||||||
Intent result = new Intent();
|
Intent result = new Intent();
|
||||||
|
@@ -323,7 +323,7 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn
|
|||||||
public ServiceDiscoveryTask(){
|
public ServiceDiscoveryTask(){
|
||||||
// Open a multicast socket and join the project's multicast group.
|
// Open a multicast socket and join the project's multicast group.
|
||||||
try{
|
try{
|
||||||
udpSocket = new MulticastSocket(ProjectConstants.SERVER_UDP_PORT);
|
udpSocket = new MulticastSocket(ProjectConstants.SERVICE_DISCOVERY_PORT);
|
||||||
InetAddress group = InetAddress.getByName(ProjectConstants.MULTICAST_ADDRESS);
|
InetAddress group = InetAddress.getByName(ProjectConstants.MULTICAST_ADDRESS);
|
||||||
udpSocket.joinGroup(group);
|
udpSocket.joinGroup(group);
|
||||||
}catch(IOException io){
|
}catch(IOException io){
|
||||||
|
@@ -2,9 +2,12 @@ package ve.ucv.ciens.ccg.nxtcam.network;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
import ve.ucv.ciens.ccg.networkdata.MotorEvent;
|
import ve.ucv.ciens.ccg.networkdata.MotorEvent;
|
||||||
|
import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t;
|
||||||
|
import ve.ucv.ciens.ccg.networkdata.MotorEventACK;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue;
|
import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
||||||
@@ -18,6 +21,7 @@ public class MotorControlThread extends Thread {
|
|||||||
private MotorEventQueue queue;
|
private MotorEventQueue queue;
|
||||||
private boolean done;
|
private boolean done;
|
||||||
private ObjectInputStream reader;
|
private ObjectInputStream reader;
|
||||||
|
private ObjectOutputStream writer;
|
||||||
private boolean connected;
|
private boolean connected;
|
||||||
|
|
||||||
public MotorControlThread(String serverIp){
|
public MotorControlThread(String serverIp){
|
||||||
@@ -30,15 +34,59 @@ public class MotorControlThread extends Thread {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
if(!connected){
|
Object msg;
|
||||||
|
MotorEvent event;
|
||||||
|
MotorEventACK ack;
|
||||||
|
|
||||||
|
if(!connectToServer()){
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: The thread is not connected to a server. Finishing.");
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: The thread is not connected to a server. Finishing.");
|
||||||
return;
|
return;
|
||||||
}else{
|
}else{
|
||||||
while(!done){
|
while(!done){
|
||||||
Object msg = readMessage();
|
// Receive a message and enqueue it;
|
||||||
MotorEvent event = verifyMessage(msg);
|
msg = readMessage();
|
||||||
if(event != null)
|
event = verifyMessage(msg);
|
||||||
|
if(event != null){
|
||||||
queue.addEvent(event);
|
queue.addEvent(event);
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor control message enqueued.");
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor ID: " + (event.getMotor() == motor_t.MOTOR_A ? "MOTOR_A" : "MOTOR_C"));
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor power: " + Byte.toString(event.getPower()));
|
||||||
|
}else{
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: Message could not be verified;");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send corresponding ack;
|
||||||
|
ack = new MotorEventACK(queue.getSize() >= 10);
|
||||||
|
try{
|
||||||
|
writer.writeObject(ack);
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: First ACK sent.");
|
||||||
|
}catch(Exception ex){
|
||||||
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: Exception while sending first ACK: " + ex.getMessage());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ack.isClientQueueFull()){
|
||||||
|
while(queue.getSize() >= 10){ }
|
||||||
|
|
||||||
|
ack = new MotorEventACK(false);
|
||||||
|
|
||||||
|
try{
|
||||||
|
writer.writeObject(ack);
|
||||||
|
}catch(Exception ex){
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".run() :: Second ACK sent.");
|
||||||
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: Exception while sending second ACK: " + ex.getMessage());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
event = null;
|
||||||
|
ack = null;
|
||||||
|
msg = null;
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
socket.close();
|
||||||
|
}catch(IOException io){
|
||||||
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException while closing socket: " + io.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,8 +97,9 @@ public class MotorControlThread extends Thread {
|
|||||||
|
|
||||||
public boolean connectToServer(){
|
public boolean connectToServer(){
|
||||||
try{
|
try{
|
||||||
socket = new Socket(serverIp, ProjectConstants.SERVER_TCP_PORT_3);
|
socket = new Socket(serverIp, ProjectConstants.MOTOR_CONTROL_PORT);
|
||||||
reader = new ObjectInputStream(socket.getInputStream());
|
reader = new ObjectInputStream(socket.getInputStream());
|
||||||
|
writer = new ObjectOutputStream(socket.getOutputStream());
|
||||||
connected = true;
|
connected = true;
|
||||||
}catch(IOException io){
|
}catch(IOException io){
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: IOException caught: " + io.getMessage());
|
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: IOException caught: " + io.getMessage());
|
||||||
@@ -63,6 +112,7 @@ public class MotorControlThread extends Thread {
|
|||||||
Object message;
|
Object message;
|
||||||
try{
|
try{
|
||||||
message = reader.readObject();
|
message = reader.readObject();
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".readMessage() :: Motor control message received.");
|
||||||
}catch(ClassNotFoundException cn){
|
}catch(ClassNotFoundException cn){
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".readMessage() :: ClassNotFoundException caught: " + cn.getMessage());
|
Logger.log_e(TAG, CLASS_NAME + ".readMessage() :: ClassNotFoundException caught: " + cn.getMessage());
|
||||||
message = null;
|
message = null;
|
||||||
@@ -75,6 +125,7 @@ public class MotorControlThread extends Thread {
|
|||||||
|
|
||||||
private MotorEvent verifyMessage(Object message){
|
private MotorEvent verifyMessage(Object message){
|
||||||
if(message != null && message instanceof MotorEvent){
|
if(message != null && message instanceof MotorEvent){
|
||||||
|
Logger.log_i(TAG, CLASS_NAME + ".verifyMessage() :: Valid motor control message received.");
|
||||||
return (MotorEvent)message;
|
return (MotorEvent)message;
|
||||||
}else{
|
}else{
|
||||||
return null;
|
return null;
|
||||||
|
@@ -36,7 +36,7 @@ public class SensorReportThread extends Thread{
|
|||||||
public boolean connectToServer(){
|
public boolean connectToServer(){
|
||||||
boolean connected;
|
boolean connected;
|
||||||
try{
|
try{
|
||||||
socket = new Socket(serverIp, ProjectConstants.SERVER_TCP_PORT_3);
|
socket = new Socket(serverIp, ProjectConstants.SENSOR_REPORT_PORT);
|
||||||
writer = new ObjectOutputStream(socket.getOutputStream());
|
writer = new ObjectOutputStream(socket.getOutputStream());
|
||||||
connected = true;
|
connected = true;
|
||||||
}catch(IOException io){
|
}catch(IOException io){
|
||||||
|
@@ -260,10 +260,10 @@ public class VideoStreamingThread extends Thread{
|
|||||||
size = int2ByteArray(bufferSize);
|
size = int2ByteArray(bufferSize);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
packet = new DatagramPacket(size, 4, InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
|
packet = new DatagramPacket(size, 4, InetAddress.getByName(serverIp), ProjectConstants.VIDEO_STREAMING_PORT);
|
||||||
udpSocket.send(packet);
|
udpSocket.send(packet);
|
||||||
|
|
||||||
packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
|
packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(serverIp), ProjectConstants.VIDEO_STREAMING_PORT);
|
||||||
udpSocket.send(packet);
|
udpSocket.send(packet);
|
||||||
|
|
||||||
}catch(UnknownHostException uo){
|
}catch(UnknownHostException uo){
|
||||||
@@ -323,7 +323,7 @@ public class VideoStreamingThread extends Thread{
|
|||||||
private void connectToServer(){
|
private void connectToServer(){
|
||||||
try{
|
try{
|
||||||
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
|
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
|
||||||
socket = new Socket(InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
|
socket = new Socket(InetAddress.getByName(serverIp), ProjectConstants.VIDEO_STREAMING_PORT);
|
||||||
/*writer = new ObjectOutputStream(socket.getOutputStream());
|
/*writer = new ObjectOutputStream(socket.getOutputStream());
|
||||||
reader = new ObjectInputStream(socket.getInputStream());*/
|
reader = new ObjectInputStream(socket.getInputStream());*/
|
||||||
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
|
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
|
||||||
|
@@ -54,4 +54,8 @@ public class MotorEventQueue {
|
|||||||
motorEvents.add(event);
|
motorEvents.add(event);
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized int getSize(){
|
||||||
|
return motorEvents.size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,10 +21,12 @@ import android.app.Activity;
|
|||||||
|
|
||||||
public abstract class ProjectConstants {
|
public abstract class ProjectConstants {
|
||||||
// Network related constants.
|
// Network related constants.
|
||||||
public static final int SERVER_UDP_PORT = 8889;
|
public static final int SERVICE_DISCOVERY_PORT = 9988;
|
||||||
public static final int SERVER_TCP_PORT_1 = 9989;
|
public static final int VIDEO_STREAMING_PORT = 9989;
|
||||||
public static final int SERVER_TCP_PORT_2 = 9990;
|
public static final int MOTOR_CONTROL_PORT = 9990;
|
||||||
public static final int SERVER_TCP_PORT_3 = 9991;
|
public static final int SENSOR_REPORT_PORT = 9991;
|
||||||
|
public static final int APP_CONTROL_PORT = 9992;
|
||||||
|
|
||||||
public static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
public static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||||||
public static final String OUI_LEGO = "00:16:53";
|
public static final String OUI_LEGO = "00:16:53";
|
||||||
public static final String MULTICAST_ADDRESS = "230.0.0.1";
|
public static final String MULTICAST_ADDRESS = "230.0.0.1";
|
||||||
|
Reference in New Issue
Block a user