Skip to content
This repository has been archived by the owner on Feb 28, 2019. It is now read-only.

Commit

Permalink
add test codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonZell committed May 16, 2018
1 parent ad7090c commit 2c95399
Showing 1 changed file with 126 additions and 45 deletions.
171 changes: 126 additions & 45 deletions backend/CommProtocolBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ using namespace std;

bool initSocket(int& hsocket, char* host_name, int host_port);
bool sendPacket(int& socket, char* packet, int pktSize, ProtoPackets::Packet& payload);// , CodedOutputStream *coded_output);
void linkCallbacks(comnet::Comms& comm1);
int hsock;

// Test against an xbee on another machine.
void xbeeTest()
void singlexbeeTest()
{
//const char* destMac = "0013A20040A54318";
const char* destMac = "0013A2004105C6AF";

const char* portName = "COM8";
// test date
std::cout << "Test: 5/11/2018" << std::endl;
//Disables Pinging to make reading output easier
comnet::constate::ConnectionStateManager::ConStateEnabled = false;

std::condition_variable cond;
std::cout << sizeof(comnet::Header) << std::endl;
// CommNode 1
Expand All @@ -45,33 +44,16 @@ void xbeeTest()
// CommNode 1 init and add Connection.
std::cout << "Init connection succeeded: "
<< std::boolalpha
<< comm1.InitConnection(ZIGBEE_LINK, "COM8", "", 57600)
<< comm1.InitConnection(ZIGBEE_LINK, portName, "", 57600)
<< std::endl;
std::cout << "Connected to address: "
<< std::boolalpha
<< comm1.AddAddress(2, destMac)
<< std::endl;

// link to callback function.
comm1.LinkCallback(new ngcp::AirVehicleGroundRelativeState(), new comnet::Callback((comnet::callback_t)AirVehicleGroundRelativeStateCallback));
comm1.LinkCallback(new ngcp::ArmCommand(), new comnet::Callback((comnet::callback_t)ArmCommandCallback));
comm1.LinkCallback(new ngcp::ArmPosition(1, 1, 1, 1), new comnet::Callback((comnet::callback_t)ArmPositionCallback));
comm1.LinkCallback(new ngcp::Battery(), new comnet::Callback((comnet::callback_t)BatteryCallback));
comm1.LinkCallback(new ngcp::TargetAcknowledgement(), new comnet::Callback((comnet::callback_t)TargetAcknowledgementCallback));
comm1.LinkCallback(new ngcp::TargetDesignationCommand(), new comnet::Callback((comnet::callback_t)TargetDesignationCommandCallback));
comm1.LinkCallback(new ngcp::TargetStatus(), new comnet::Callback((comnet::callback_t)TargetStatusCallback));
comm1.LinkCallback(new ngcp::VehicleAttitude(), new comnet::Callback((comnet::callback_t)VehicleAttitudeCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationReply(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationReplyCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationRequest(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationRequestCallback));
comm1.LinkCallback(new ngcp::VehicleBodySensedState(), new comnet::Callback((comnet::callback_t)VehicleBodySensedStateCallback));
comm1.LinkCallback(new ngcp::VehicleGlobalPosition(), new comnet::Callback((comnet::callback_t)VehicleGlobalPositionCallback));
comm1.LinkCallback(new ngcp::VehicleIdentification(), new comnet::Callback((comnet::callback_t)VehicleIdentificationCallback));
comm1.LinkCallback(new ngcp::VehicleInertialState(), new comnet::Callback((comnet::callback_t)VehicleInertialStateCallback));
comm1.LinkCallback(new ngcp::VehicleModeCommand(), new comnet::Callback((comnet::callback_t)VehicleModeCommandCallback));
comm1.LinkCallback(new ngcp::VehicleSystemStatus(), new comnet::Callback((comnet::callback_t)VehicleSystemStatusCallback));
comm1.LinkCallback(new ngcp::VehicleTelemetryCommand(), new comnet::Callback((comnet::callback_t)VehicleTelemetryCommandCallback));
comm1.LinkCallback(new ngcp::VehicleTerminationCommand(), new comnet::Callback((comnet::callback_t)VehicleTerminationCommandCallback));
comm1.LinkCallback(new ngcp::VehicleWaypointCommand(), new comnet::Callback((comnet::callback_t)VehicleWaypointCommandCallback));
linkCallbacks(comm1);


// Test packet.

Expand Down Expand Up @@ -105,6 +87,100 @@ void xbeeTest()
}
}

void doublexbeeTest()
{
//const char* destMac = "0013A20040A54318";
const char* UGVMAC = "0013A2004105C6AF";
const char* GCSMAC = "0013A2004105C6AF";
const char* GCSPortName = "COM8";
const char* UGVPortName = "COM9";
const int GCSID = 1;
const int UGVID = 2;
// test date
//Disables Pinging to make reading output easier
comnet::constate::ConnectionStateManager::ConStateEnabled = false;
std::condition_variable cond;
//std::cout << sizeof(comnet::Header) << std::endl;
// CommNode 1
comnet::Comms GCSComm(GCSID);
comnet::Comms UGVComm(UGVID);
//comm1.LoadKey("01234567890ABCDEF");

comnet::architecture::os::CommMutex mut;
comnet::architecture::os::CommLock commlock(mut);
// This will cause the thread to wait for a few milliseconds, causing any other thread to wait.
comnet::architecture::os::WaitForMilliseconds(commlock, cond, 1000);

std::cout << "Test complete!" << std::endl;
// CommNode 1 init and add Connection.
std::cout << "Init connection succeeded: "
<< std::boolalpha
<< GCSComm.InitConnection(ZIGBEE_LINK, GCSPortName, "", 57600)
<< std::endl;
std::cout << "Connected to address: "
<< std::boolalpha
<< GCSComm.AddAddress(UGVID, UGVMAC)
<< std::endl;

// CommNode 1 init and add Connection.
std::cout << "Init connection succeeded: "
<< std::boolalpha
<< UGVComm.InitConnection(ZIGBEE_LINK, UGVPortName, "", 57600)
<< std::endl;
std::cout << "Connected to address: "
<< std::boolalpha
<< UGVComm.AddAddress(GCSID, GCSMAC)
<< std::endl;

// link to callback function.
linkCallbacks(GCSComm);
linkCallbacks(UGVComm);



// Test packet.

// NOTE(All): Be sure to run the nodes! If not, the threads won't execute!
GCSComm.Run();
UGVComm.Run();

//// Loop. To exit, Click the red button on the top left (Windows Visual Studio) OR
//// CNTRL+C (Linux).
int id = 77;
int pos = 1000;
int pos1 = 20;
int pos2 = 30;
int pos3 = 40;
int pos4 = 50;
while (true) {
std::cout << "Sleeping..." << std::endl;
// comm1 will be sending the packet.
std::string word;
std::cout << "enter message:";
std::cin >> word;
ngcp::ArmCommand amc(id, pos);
ngcp::ArmPosition amp(pos1, pos2, pos3, pos4);
UGVComm.Send(amp, GCSID);
UGVComm.Send(amc, GCSID);
//UGVComm.Send(message, 2);
id++;
pos++;
pos1++;
pos2++;
pos3++;
pos4++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

}
//std::cin.ignore();
//while (true) {
//std:string word;
// std::cin >> word;
// ngcp::ArmCommand amc(22, 1337);
// comm1.Send(amc, 2);
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
//}
}
//test two xbees on same machine.
void localTcpTest(int& hsocket)
{
Expand Down Expand Up @@ -150,26 +226,8 @@ void localTcpTest(int& hsocket)
<< comm1.AddAddress(2, "127.0.0.1", 1338)
<< std::endl;

// link to callback function.
comm1.LinkCallback(new ngcp::AirVehicleGroundRelativeState(), new comnet::Callback((comnet::callback_t)AirVehicleGroundRelativeStateCallback));
comm1.LinkCallback(new ngcp::ArmCommand(), new comnet::Callback((comnet::callback_t)ArmCommandCallback));
comm1.LinkCallback(new ngcp::ArmPosition(1, 1, 1, 1), new comnet::Callback((comnet::callback_t)ArmPositionCallback));
comm1.LinkCallback(new ngcp::Battery(), new comnet::Callback((comnet::callback_t)BatteryCallback));
comm1.LinkCallback(new ngcp::TargetAcknowledgement(), new comnet::Callback((comnet::callback_t)TargetAcknowledgementCallback));
comm1.LinkCallback(new ngcp::TargetDesignationCommand(), new comnet::Callback((comnet::callback_t)TargetDesignationCommandCallback));
comm1.LinkCallback(new ngcp::TargetStatus(), new comnet::Callback((comnet::callback_t)TargetStatusCallback));
comm1.LinkCallback(new ngcp::VehicleAttitude(), new comnet::Callback((comnet::callback_t)VehicleAttitudeCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationReply(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationReplyCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationRequest(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationRequestCallback));
comm1.LinkCallback(new ngcp::VehicleBodySensedState(), new comnet::Callback((comnet::callback_t)VehicleBodySensedStateCallback));
comm1.LinkCallback(new ngcp::VehicleGlobalPosition(), new comnet::Callback((comnet::callback_t)VehicleGlobalPositionCallback));
comm1.LinkCallback(new ngcp::VehicleIdentification(), new comnet::Callback((comnet::callback_t)VehicleIdentificationCallback));
comm1.LinkCallback(new ngcp::VehicleInertialState(), new comnet::Callback((comnet::callback_t)VehicleInertialStateCallback));
comm1.LinkCallback(new ngcp::VehicleModeCommand(), new comnet::Callback((comnet::callback_t)VehicleModeCommandCallback));
comm1.LinkCallback(new ngcp::VehicleSystemStatus(), new comnet::Callback((comnet::callback_t)VehicleSystemStatusCallback));
comm1.LinkCallback(new ngcp::VehicleTelemetryCommand(), new comnet::Callback((comnet::callback_t)VehicleTelemetryCommandCallback));
comm1.LinkCallback(new ngcp::VehicleTerminationCommand(), new comnet::Callback((comnet::callback_t)VehicleTerminationCommandCallback));
comm1.LinkCallback(new ngcp::VehicleWaypointCommand(), new comnet::Callback((comnet::callback_t)VehicleWaypointCommandCallback));
// Link callbacks
linkCallbacks(comm1);


// Test packet.
Expand Down Expand Up @@ -234,13 +292,36 @@ int main()

localTcpTest(hsock);
//xbeeTest();
//doublexbeeTest();
delete pkt;
closesocket(hsock);
WSACleanup();
cin.get();
return 0;
}

void linkCallbacks(comnet::Comms& comm1)
{
// link to callback function.
comm1.LinkCallback(new ngcp::AirVehicleGroundRelativeState(), new comnet::Callback((comnet::callback_t)AirVehicleGroundRelativeStateCallback));
comm1.LinkCallback(new ngcp::ArmCommand(), new comnet::Callback((comnet::callback_t)ArmCommandCallback));
comm1.LinkCallback(new ngcp::ArmPosition(1, 1, 1, 1), new comnet::Callback((comnet::callback_t)ArmPositionCallback));
comm1.LinkCallback(new ngcp::Battery(), new comnet::Callback((comnet::callback_t)BatteryCallback));
comm1.LinkCallback(new ngcp::TargetAcknowledgement(), new comnet::Callback((comnet::callback_t)TargetAcknowledgementCallback));
comm1.LinkCallback(new ngcp::TargetDesignationCommand(), new comnet::Callback((comnet::callback_t)TargetDesignationCommandCallback));
comm1.LinkCallback(new ngcp::TargetStatus(), new comnet::Callback((comnet::callback_t)TargetStatusCallback));
comm1.LinkCallback(new ngcp::VehicleAttitude(), new comnet::Callback((comnet::callback_t)VehicleAttitudeCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationReply(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationReplyCallback));
comm1.LinkCallback(new ngcp::VehicleAuthorizationRequest(), new comnet::Callback((comnet::callback_t)VehicleAuthorizationRequestCallback));
comm1.LinkCallback(new ngcp::VehicleBodySensedState(), new comnet::Callback((comnet::callback_t)VehicleBodySensedStateCallback));
comm1.LinkCallback(new ngcp::VehicleGlobalPosition(), new comnet::Callback((comnet::callback_t)VehicleGlobalPositionCallback));
comm1.LinkCallback(new ngcp::VehicleIdentification(), new comnet::Callback((comnet::callback_t)VehicleIdentificationCallback));
comm1.LinkCallback(new ngcp::VehicleInertialState(), new comnet::Callback((comnet::callback_t)VehicleInertialStateCallback));
comm1.LinkCallback(new ngcp::VehicleModeCommand(), new comnet::Callback((comnet::callback_t)VehicleModeCommandCallback));
comm1.LinkCallback(new ngcp::VehicleSystemStatus(), new comnet::Callback((comnet::callback_t)VehicleSystemStatusCallback));
comm1.LinkCallback(new ngcp::VehicleTelemetryCommand(), new comnet::Callback((comnet::callback_t)VehicleTelemetryCommandCallback));
comm1.LinkCallback(new ngcp::VehicleTerminationCommand(), new comnet::Callback((comnet::callback_t)VehicleTerminationCommandCallback));
comm1.LinkCallback(new ngcp::VehicleWaypointCommand(), new comnet::Callback((comnet::callback_t)VehicleWaypointCommandCallback));
}

bool initSocket(int& hsocket, char* host_name, int host_port)
{
Expand Down

0 comments on commit 2c95399

Please sign in to comment.