diff --git a/bfs/implementations/breadcrumbs/CMakeLists.txt b/bfs/implementations/breadcrumbs/CMakeLists.txt index 64a6766..b2ac1e1 100644 --- a/bfs/implementations/breadcrumbs/CMakeLists.txt +++ b/bfs/implementations/breadcrumbs/CMakeLists.txt @@ -2,3 +2,7 @@ set (VirtualOutputIOProcessor_IMPL_LIBS "ea_tdk" PARENT_SCOPE) + +set (TactorOutputIOProcessor_IMPL_LIBS + "ea_tdk" + PARENT_SCOPE) diff --git a/bfs/implementations/breadcrumbs/algos/AlgoBreadcrumbs.cpp b/bfs/implementations/breadcrumbs/algos/AlgoBreadcrumbs.cpp index 0ced946..1ae76b8 100644 --- a/bfs/implementations/breadcrumbs/algos/AlgoBreadcrumbs.cpp +++ b/bfs/implementations/breadcrumbs/algos/AlgoBreadcrumbs.cpp @@ -1,5 +1,6 @@ #include "AlgoBreadcrumbs.hpp" +#include "BreadcrumbsConstants.hpp" void AlgoBreadcrumbs::loop() @@ -19,6 +20,9 @@ void AlgoBreadcrumbs::loop() sendAttribute(msgCountAttrib); } + int degree = 0; + Attribute degreeAttrib = { ATTRKEY_EATAC_DEGREE, sizeof(double), °ree }; + sendAttribute(degreeAttrib); } bool AlgoBreadcrumbs::loopCondition() diff --git a/bfs/implementations/breadcrumbs/ea_tactor/ResponseHelper.cpp b/bfs/implementations/breadcrumbs/ea_tactor/ResponseHelper.cpp new file mode 100644 index 0000000..35fa228 --- /dev/null +++ b/bfs/implementations/breadcrumbs/ea_tactor/ResponseHelper.cpp @@ -0,0 +1,138 @@ +#include "ResponseHelper.h" + +#include +#include + +void PrintPacketMsg(string msg, float timer, unsigned char*buffer, int size) { + + printf("[TDK] %i,%f,%s,", 0, timer, msg.c_str()); + + for (int i = 0; i < size; ++i) + printf("%X:", buffer[i]); + printf("\n"); +} +unsigned char ComputeCheckSum(unsigned char* buf, int size) { + unsigned char checksum = 0x00; + for (int i = 0; i < size; ++i) { + checksum = checksum ^ buf[i]; + } + checksum ^= 0xEA; + + return checksum; +} + int __stdcall ParsePacket(int deviceID, unsigned char packet[READPACKETLIMIT], int size) + { + //[1] of the packet data is the type. + unsigned char type = packet[1]; + //[2] if the packet data is the data len + unsigned char len = packet[2]; + + unsigned char * data = new unsigned char[len]; + memset(data, '\0', len); + memcpy(data, &packet[3], len); + unsigned char chksum = packet[len + 3]; + //comupting the checksum of the packet to ensure we got the entire packet correctly + unsigned char computed_chksum = ComputeCheckSum(packet, len + 3); + + if (computed_chksum == chksum) + { + switch (type) + { + //ack packet + case 0xc8: + PrintPacketMsg("ACK WITH DATA", 0, packet, size); + break; + //ack packet + case 0xc9: + PrintPacketMsg("ACK", 0, packet, size); + break; + //nak packet + case 0xc4: { + PrintPacketMsg("NAK", 0, packet, size); + //[0] response to wich command + unsigned char responseTo = data[0]; + //[1] reason code for nak + unsigned char reasonCode = data[1]; + switch (reasonCode) { + case 0x01: //The ETX value was not found in the expected place + PrintPacketMsg("ETX value was not found ERROR", 0, packet, size); + break; + case 0x02: //The Checksum value was invalid + PrintPacketMsg(" Checksum value was invalid ERROR", 0, packet, size); + break; + case 0x03: //Insufficient Data in packet + PrintPacketMsg("Insufficient Data ERROR", 0, packet, size); + break; + case 0x04: //An invalid command was used + PrintPacketMsg("invalid command ERROR", 0, packet, size); + break; + case 0x05: //Invaid Data was given with a valid command + PrintPacketMsg("Invalid Data ERROR", 0, packet, size); + break; + case 0x06: //Data length exceeds max payload + PrintPacketMsg("Data length exceeds ERROR", 0, packet, size); + break; + case 0x07: //Invalid Sequence Data + PrintPacketMsg(" Invalid Sequence Data ERROR", 0, packet, size); + break; + case 0x08: //Bus Error Sending to Node failed after three attempts + PrintPacketMsg(" Bus Error Sending ERROR", 0, packet, size); + break; + case 0x09: //Bus Master Times out while waiting on all bytes of incoming command to finish + PrintPacketMsg("Bus Master Times out ERROR", 0, packet, size); + break; + case 0x0A: //Node has a Fault Cannot Complete Request + PrintPacketMsg("Fault ERROR", 0, packet, size); + break; + case 0x0B: // Sequence is busy � you tried to perform a new sequence operation while the previous one is still in progress � sequences not implemented on TDK boards + PrintPacketMsg("NAK_SeqBusy ERROR", 0, packet, size); + break; + case 0x0C: // not used anymore + PrintPacketMsg("NAK_TimeOut ERROR", 0, packet, size); + break; + case 0x0D: // Ramp List is Full + PrintPacketMsg("NAK_RampListFull ERROR", 0, packet, size); + break; + case 0x0E: // Action List is Full + PrintPacketMsg("NAK_ActionListFull ERROR", 0, packet, size); + break; + case 0x0F:// The command you sent is probably valid, but it has not been implemented on this board � you shouldn�t see this outside of early beta firmware + PrintPacketMsg("NAK_NotImplemented ERROR", 0, packet, size); + break; + case 0x10:// Master Packet is Full - you shouldn�t see this one � it means the Bus on eTSAS or other new distrib system is very busy with action-lists & ramps and can�t fit a new command at the instant you sent it. + PrintPacketMsg("NAK_MasterPacketFullOrBusy ERROR", 0, packet, size); + break; + case 0x11: // error while processing a command which is already in the action list + PrintPacketMsg("NAK_ActionListTooManyBytes ERROR", 0, packet, size); + break; + case 0x12: // another error while processing a command which is already in the action list + PrintPacketMsg("NAK_ActionListTooFewBytes", 0, packet, size); + break; + case 0x13: //your packet is too big to fit in the TDK receive buffer + PrintPacketMsg("NAK_TooMuch", 0, packet, size); + break; + case 0x14: //Received a command while Self Test is running + PrintPacketMsg("NAK_Received a command while Self Test is running ERROR", 0, packet, size); + break; + default: //did not find the error within the lookup table. + PrintPacketMsg("UNKNOWN ERROR", 0, packet, size); + break; + + } + break; + } + case 0xEE: + PrintPacketMsg("0xEE", 0, packet, size); + return -1; + break; + } + delete[] data; + } else { + PrintPacketMsg("checksum was bad", 0, packet, + size); + delete[] data; + return -1; + } + + return 0; +} diff --git a/bfs/implementations/breadcrumbs/gen/AHRSInputIOProcessor.cpp b/bfs/implementations/breadcrumbs/gen/AHRSInputIOProcessor.cpp new file mode 100644 index 0000000..fb8a3f3 --- /dev/null +++ b/bfs/implementations/breadcrumbs/gen/AHRSInputIOProcessor.cpp @@ -0,0 +1,23 @@ + +#include + +#include "DataSyncThread.hpp" +#include "AHRSInputIOProcessor.hpp" + + +int main() +{ + IOProcessor* client = new AHRSInputIOProcessor; + + if (!client->init()) + { + while (client->loopCondition()) + client->loop(); + + int result = client->close(); + delete client; + return result; + } + + return 0; +} diff --git a/bfs/implementations/breadcrumbs/gen/TactorOutputIOProcessor.cpp b/bfs/implementations/breadcrumbs/gen/TactorOutputIOProcessor.cpp new file mode 100644 index 0000000..2066e88 --- /dev/null +++ b/bfs/implementations/breadcrumbs/gen/TactorOutputIOProcessor.cpp @@ -0,0 +1,23 @@ + +#include + +#include "DataSyncThread.hpp" +#include "TactorOutputIOProcessor.hpp" + + +int main() +{ + IOProcessor* client = new TactorOutputIOProcessor; + + if (!client->init()) + { + while (client->loopCondition()) + client->loop(); + + int result = client->close(); + delete client; + return result; + } + + return 0; +} diff --git a/bfs/implementations/breadcrumbs/include/AHRSInputIOProcessor.hpp b/bfs/implementations/breadcrumbs/include/AHRSInputIOProcessor.hpp new file mode 100644 index 0000000..49ea2d6 --- /dev/null +++ b/bfs/implementations/breadcrumbs/include/AHRSInputIOProcessor.hpp @@ -0,0 +1,38 @@ +/* +* +* This is the IO Processor for the Xsens MTi-630 AHRS: +* https://www.xsens.com/products/mti-600-series +* The AHRS is able to calculate a lot of different orientational data, +* we are specfically using it to find the Euler Angles. +* Currently you must manually specify which COM port the device is connected +* to, this is right at the begining of the CreateFile() method call. +* By defualt it is set to COM8, you may have to change it to the correct port +* on your system. You can find the corrct port manually by opening Device Manager, +* under "Ports(COM & LPT)" you can see the ports detected by your system. +*/ + +#ifndef AHRS_INPUT_IO_PROCESSOR_HPP +#define AHRS_INPUT_IO_PROCESSOR_HPP + +#include "IOProcessor.hpp" +#include "DataSyncThread.hpp" +#include "Attribute.hpp" + +#include +#include + +class AHRSInputIOProcessor : public IOProcessor +{ +public: + using IOProcessor::IOProcessor; + + void loop(); + bool loopCondition(); + int configAHRS(); // Initialize the AHRS for the first time +private: + int iterations = 10; + bool configured = false; + HANDLE hSerial; +}; + +#endif diff --git a/bfs/implementations/breadcrumbs/include/BreadcrumbsConstants.hpp b/bfs/implementations/breadcrumbs/include/BreadcrumbsConstants.hpp new file mode 100644 index 0000000..6dbf2ae --- /dev/null +++ b/bfs/implementations/breadcrumbs/include/BreadcrumbsConstants.hpp @@ -0,0 +1,8 @@ +#ifndef BREADCRUMBS_CONSTANTS_HPP +#define BREADCRUMBS_CONSTANTS_HPP + + +#define ATTRKEY_EATAC_DEGREE "EATACDEG" + + +#endif \ No newline at end of file diff --git a/bfs/implementations/breadcrumbs/include/ResponseHelper.h b/bfs/implementations/breadcrumbs/include/ResponseHelper.h new file mode 100644 index 0000000..29a88bd --- /dev/null +++ b/bfs/implementations/breadcrumbs/include/ResponseHelper.h @@ -0,0 +1,23 @@ +#ifndef _EAIRESPONSEH_ +#define _EAIRESPONSEH_ + +// This file was copied from the "TDK" folder in the examples directory! + +#include +#include +using namespace std; + +#define READPACKETLIMIT 512 // maximum bytes of an entire read packet + +// The calling convention on windows is stdcall, +// on Linux, this identifier doesn't exist. +#ifndef _WIN32 +#ifndef __stdcall +#define __stdcall +#endif // __stdcall +#endif // !_WIN32 + +//Parse Packet is the callback used for the device packet data. +int __stdcall ParsePacket(int deviceID, unsigned char packet[READPACKETLIMIT], int size); + +#endif diff --git a/bfs/implementations/breadcrumbs/include/TactorOutputIOProcessor.hpp b/bfs/implementations/breadcrumbs/include/TactorOutputIOProcessor.hpp new file mode 100644 index 0000000..9f15020 --- /dev/null +++ b/bfs/implementations/breadcrumbs/include/TactorOutputIOProcessor.hpp @@ -0,0 +1,31 @@ +#ifndef TACTOR_OUTPUT_IO_PROCESSOR_HPP +#define TACTOR_OUTPUT_IO_PROCESSOR_HPP + +#include + +#include "IOProcessor.hpp" +#include "DataSyncThread.hpp" +#include "Attribute.hpp" +#include "BreadcrumbsConstants.hpp" +#include "TactorInterface.h" +#include "ResponseHelper.h" + + +class TactorOutputIOProcessor : public IOProcessor +{ +public: + using IOProcessor::IOProcessor; + + void loop(); + bool loopCondition(); + double getTactorDistance(double tactorA, double tactorB, int tactorNumber); + void pulseTactors(double degree, int tactorNumber, int pulseRadius); + void checkForError(int errorCode); + int initializeController(); + +private: + bool isControllerInit = false; + double tactorDegree = 0; +}; + +#endif \ No newline at end of file diff --git a/bfs/implementations/breadcrumbs/io_procs/AHRSInputIOProcessor.cpp b/bfs/implementations/breadcrumbs/io_procs/AHRSInputIOProcessor.cpp new file mode 100644 index 0000000..69bb90d --- /dev/null +++ b/bfs/implementations/breadcrumbs/io_procs/AHRSInputIOProcessor.cpp @@ -0,0 +1,184 @@ +#include "AHRSInputIOProcessor.hpp" + +/* + * Return Values: + * 0 : No Errors. + * 1 : Serial port does not exist. + * 2 : Some other error occurred. + * 3 : Error getting state. + * 4 : Error setting serial port state + * 5 : Error occured while setting timoemouts. + * 6 : Error occured while trying to send GoToConfig message. + * 7 : Error occured while trying to send SetOutputConfiguration message. + * 8 : Error occured while trying to send GoToMeasurment message. +*/ +int AHRSInputIOProcessor::configAHRS() +{ + hSerial = CreateFile("COM8", // The COM port to connect to, may have to be changed + GENERIC_READ | GENERIC_WRITE, // depending on what COM port gets selected. + 0, + 0, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + 0); + + if (hSerial == INVALID_HANDLE_VALUE) + { + if(GetLastError() == ERROR_FILE_NOT_FOUND) + { + std::cerr << "Serial port does not exist." << std::endl; + return 1; + } + std::cerr << "Some other error occurred." << std::endl; + return 2; + } + + DCB dcb = { 0 }; + dcb.DCBlength = sizeof(dcb); + + if (!GetCommState(hSerial, &dcb)) + { + std::cerr << "Error getting state." << std::endl; + return 3; + } + + dcb.BaudRate = CBR_115200; // defualt baud rate of MT + dcb.ByteSize = 8; + dcb.StopBits = ONESTOPBIT; + dcb.Parity = NOPARITY; + + if (!SetCommState(hSerial, &dcb)) + { + std::cerr << "Error setting serial port state." << std::endl; + return 4; + } + + COMMTIMEOUTS timeouts = {0}; + + timeouts.ReadIntervalTimeout = 50; + timeouts.ReadTotalTimeoutConstant = 50; + timeouts.ReadTotalTimeoutMultiplier = 10; + timeouts.WriteTotalTimeoutConstant = 50; + timeouts.WriteTotalTimeoutMultiplier = 10; + + if (!SetCommTimeouts(hSerial, &timeouts)) + { + std::cerr << "Error occured while setting timoemouts." << std::endl; + return 5; + } + + std::cout << "Connection established!" << std::endl; + std::cout << "Setting up Config..." << std::endl; + + // This is the GoToConfig message, MID 48 + // Switch the active state of the device from Measurment State to Config State. + unsigned char wBuff1[] = {0xFA,0xFF,0x30,0x00,0xD1}; + DWORD dwBytesWritten = 0; // Number of bytes actually written + + if (!WriteFile(hSerial, wBuff1, 6, &dwBytesWritten, NULL)) + { + std::cerr << "Error occured while trying to send GoToConfig message." << std::endl; + return 6; + } + + // This is the SetOutputConfiguration message, MID 192 + // Set the output configuration of the device. + unsigned char wBuff2[] = {0xFA,0xFF,0xC0,0x04,0x20,0x33,0x00,0x00,0xEA}; + dwBytesWritten = 0; + + if (!WriteFile(hSerial, wBuff2, 10, &dwBytesWritten, NULL)) + { + std::cerr << "Error occured while trying to send SetOutputConfiguration message." << std::endl; + return 7; + } + + // This is the GoToMeasurment message, MID 16 + // Switch the active state of the device from Config State to Measurment State. + unsigned char wBuff3[] = {0xFA,0xFF,0x10,0x00,0xF1}; + dwBytesWritten = 0; + + if (!WriteFile(hSerial, wBuff3, 6, &dwBytesWritten, NULL)) + { + std::cerr << "Error occured while trying to send GoToMeasurment message." << std::endl; + return 8; + } + + configured = true; + return 0; +} + +bool AHRSInputIOProcessor::loopCondition() +{ + return true; +} + +void AHRSInputIOProcessor::loop() +{ + if (!configured) + configAHRS(); + + unsigned char szBuff[64 + 1] = {0}; // Buffer size + DWORD dwBytesRead = 0; // Number of bytes actually read + + if (!ReadFile(hSerial, szBuff, 64, &dwBytesRead, NULL)) + { + std::cerr << "Error occured while trying to read bytes." << std::endl; + } + + /* This gets the index of the bytes for the Euler angles in the captured + * buffer (szBuff). First, it finds the first byte of the message (i). Then, + * it finds the 14th index of the message (dataStart). This is where the last + * byte of the roll angle is. This allows me loop backward through each group + * of bytes, since the order needs to be reversed because the AHRS is in Big + * Endian. + + * i is where the message starts in the buffer. + * dataStart = i + 14, which is where the data we want starts (for Roll). + * To find Pitch and Yaw, we add 8 and 16 respectivly. + * + * MESSAGE STRUCTURE: + * Roll Angle Bytes = RX, Pitch Angle Bytes = PX, Yaw Angle Bytes = YX + * +-------------------------+-------------------------+-------------------------+ + * | Roll | Pitch | Yaw | + * +-------------------------+-------------------------+-------------------------+ + * | R1 R2 R3 R4 R5 R6 R7 R8 | P1 P2 P3 P4 P5 P6 P7 P8 | Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 | + * +-------------------------+-------------------------+-------------------------+ + * ... | 7 8 9 10 11 12 13 14 | 15 16 17 18 19 20 21 22 | 23 24 25 26 27 28 29 30 | ... + * | ^ | ^ | ^ | + * | dataStart| dataStart + 8| dataStart + 16| + * +-------------------------+-------------------------+-------------------------+ + */ + int dataStart = -1; + for (int i=0; i < 32; i++) + { + if (szBuff[i] == 0xFA && szBuff[i+1] == 0xFF && szBuff[i+2] == 0x36) + { + dataStart = i + 14; + break; + } + } + + unsigned char rollArray[8]; + unsigned char pitchArray[8]; + unsigned char yawArray[8]; + // Puts the bytes for each angle in their own array backwards, since they are sent in big endian + if (dataStart != -1) + { + for (int i = 0; i < 8; i++) + { + rollArray[i] = szBuff[dataStart - i]; + pitchArray[i] = szBuff[dataStart - i + 8]; + yawArray[i] = szBuff[dataStart - i + 16]; + } + } + + double doubleRoll = *reinterpret_cast(rollArray); + double doublePitch = *reinterpret_cast(pitchArray); + double doubleYaw = *reinterpret_cast(yawArray); + Attribute attRoll("XSIMUROL", 8, &doubleRoll); + getComms()->sendAttribute(attRoll); + Attribute attPitch("XSIMUPIT", 8, &doublePitch); + getComms()->sendAttribute(attPitch); + Attribute attYaw("XSIMUYAW", 8, &doubleYaw); + getComms()->sendAttribute(attYaw); +} diff --git a/bfs/implementations/breadcrumbs/io_procs/TactorOutputIOProcessor.cpp b/bfs/implementations/breadcrumbs/io_procs/TactorOutputIOProcessor.cpp new file mode 100644 index 0000000..e034006 --- /dev/null +++ b/bfs/implementations/breadcrumbs/io_procs/TactorOutputIOProcessor.cpp @@ -0,0 +1,134 @@ +#include "TactorOutputIOProcessor.hpp" +#include "DataSyncThread.hpp" +#include "Attribute.hpp" +#include +#include + +//position A and position B +double TactorOutputIOProcessor::getTactorDistance(double tactorA, double tactorB, int tactorNumber) { + double tactorAOffset = tactorA + tactorNumber; + double difference = abs(tactorA - tactorB); + // We need to compute this for the case that one of the tactor positions is near 0 degrees + double offsetDifference = abs(tactorAOffset - tactorB); + if (offsetDifference < difference) + return offsetDifference; + return difference; +} + +/** + * degree: the degree that the tactors should point + * tactorNumber: the number of tactors + * pulseRadius: the number of tactors next to the degree that can be vibrating. + */ +void TactorOutputIOProcessor::pulseTactors(double degree, int tactorNumber, int pulseRadius) { + double pulsePosition = (degree / 360.0) * tactorNumber; + int deviceId = 0; + + // Record all the tactor magnitudes so we can compare them to the max and normalize them later on + + double* tactorMags = new double[tactorNumber]; + + double maxTactorMag = 0; + for (int i = 0; i < tactorNumber; i++) { + double distance1 = getTactorDistance(static_cast(i), pulsePosition, tactorNumber); + double distance2 = getTactorDistance(pulsePosition, static_cast(i), tactorNumber); + double distance = distance2; + if (distance1 < distance2) { + distance = distance1; + } + if (distance < pulseRadius) { + tactorMags[i] = abs(distance - pulseRadius); + if (tactorMags[i] > maxTactorMag) + maxTactorMag = tactorMags[i]; + } + else { + tactorMags[i] = -1; + } + } + // Pulsing the tactors at their normalized values + for (int i = 0; i < tactorNumber; i++) + // Pulse the tactor at a frequency proportional to the magnitude above + if (tactorMags[i] > 0) { + double magnitude = tactorMags[i] / maxTactorMag; + cout << "Pulsing " << i << " at a magnitude of " << magnitude << endl; + //setting frequency for all tactors + checkForError(ChangeFreq(deviceId, 0, 300, 0)); + //setting gain for varying tactors + checkForError(ChangeGain(deviceId, i, magnitude * 254 + 1, 0)); + checkForError(Pulse(deviceId, i, 150, 10)); + } + else { + checkForError(ChangeGain(deviceId, i, 1, 0)); + } + + //deallocate the array + delete[] tactorMags; +} + + +int TactorOutputIOProcessor::initializeController() { + + char* connectionName = (char*)"COM3"; //name of the tactor controller + int deviceId = -1; + + checkForError(InitializeTI()); + + //Trying to discover the tactor device + int deviceCount = Discover(DEVICE_TYPE_SERIAL); + + if (deviceCount > 0) { + //attempt to connect to device + //Get Discovered Name or connectionName works just fine + deviceId = Connect(GetDiscoveredDeviceName(0), DEVICE_TYPE_SERIAL, ParsePacket); + checkForError(deviceId); + } + else { + if (deviceCount == 0) + cout << "Discover found 0 devices\n"; + else + checkForError(deviceCount); + return 0; + } + if (deviceCount >= 0) + //Using the TI_Update method to return any errors that may have occured + checkForError(UpdateTI()); + return 0; +} + + +void TactorOutputIOProcessor::loop() +{ + + if (!isControllerInit) { + initializeController(); + isControllerInit = true; + } + + if (getComms()->areIncomingAttributesAvailable()) { + map attributes = getComms()->getIncomingAttributesMap(); + //looking for degree value + auto newDegree = attributes.find(string(ATTRKEY_EATAC_DEGREE)); + //if degree value available + if (newDegree != attributes.end()) { + this->tactorDegree = *((double*) ((*newDegree).second.getValue())); + } + } + pulseTactors(this->tactorDegree, 16, 2); + Sleep(5000); //five second delay between each pulse + +} + +bool TactorOutputIOProcessor::loopCondition() +{ + return true; +} + +void TactorOutputIOProcessor::checkForError(int errorCode) +{ + //check if something went wrong + if (errorCode < 0) + { + //gets the last error code recorded in the tactorinterface + std::cout << "Error Code " << GetLastEAIError() << "\nCheck EAI_Defines.h For Reason\n"; + } +} \ No newline at end of file