From 7fa04a5c352585cdbe4ac4c427c8808182c72344 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Tue, 22 Oct 2024 15:10:08 +0200 Subject: [PATCH] made it so we can read indiviual buttons --- src/C++/Driver/src/CKobuki.cpp | 2 +- src/C++/Driver/src/KobukiParser.cpp | 10 ++-- src/C++/Driver/src/KobukiParser.h | 3 +- src/C++/Driver/src/main.cpp | 88 +++++++++++++++-------------- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/C++/Driver/src/CKobuki.cpp b/src/C++/Driver/src/CKobuki.cpp index ae909b8..61eed6e 100755 --- a/src/C++/Driver/src/CKobuki.cpp +++ b/src/C++/Driver/src/CKobuki.cpp @@ -671,7 +671,7 @@ void CKobuki::sendNullMessage(){ 0x00, // Placeholder for radius 0x00 // Placeholder for checksum }; - + message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^ message[7] ^ message[8] ^ message[9]; diff --git a/src/C++/Driver/src/KobukiParser.cpp b/src/C++/Driver/src/KobukiParser.cpp index 0c1e957..c82ce02 100644 --- a/src/C++/Driver/src/KobukiParser.cpp +++ b/src/C++/Driver/src/KobukiParser.cpp @@ -1,8 +1,6 @@ #include "KobukiParser.h" #include -//checkedValue const maken -//bitwise operators nachecken met website van kobuki serial website -//cliffsensor kan rauwe getallen zijn +//moet checkenvalue gebruiken of moet kijken naar de payloadlength welke dingen er extra zijn int KobukiParser::parseKobukiMessage(TKobukiData &output, unsigned char *data) { int rtrnvalue = checkChecksum(data); if (rtrnvalue != 0) { @@ -110,7 +108,7 @@ void KobukiParser::parseBasicData(TKobukiData &output, unsigned char *data, int checkedValue += 2; output.BumperCenter = (data[checkedValue] & 0x02) >> 1; output.BumperLeft = (data[checkedValue] & 0x04) >> 2; - output.BumperRight = data[checkedValue] & 0x01; + output.BumperRight = data[checkedValue] & 0x01; checkedValue++; output.WheelDropLeft = (data[checkedValue] & 0x02) >> 1; output.WheelDropRight = data[checkedValue] & 0x01; @@ -127,7 +125,9 @@ void KobukiParser::parseBasicData(TKobukiData &output, unsigned char *data, int checkedValue++; output.PWMright = data[checkedValue]; checkedValue++; - output.ButtonPress = data[checkedValue]; + output.ButtonPress1 = data[checkedValue] & 0x01; + output.ButtonPress2 = data[checkedValue] & 0x02; + output.ButtonPress3 = data[checkedValue] & 0x04; checkedValue++; output.Charger = data[checkedValue]; checkedValue++; diff --git a/src/C++/Driver/src/KobukiParser.h b/src/C++/Driver/src/KobukiParser.h index aca3eb0..1d3cef4 100644 --- a/src/C++/Driver/src/KobukiParser.h +++ b/src/C++/Driver/src/KobukiParser.h @@ -19,7 +19,8 @@ struct TKobukiData { int CliffCenter, CliffLeft, CliffRight; int EncoderLeft, EncoderRight; int PWMleft, PWMright; - int ButtonPress, Charger, Battery, overCurrent; + int ButtonPress1, ButtonPress2, ButtonPress3; + int Charger, Battery, overCurrent; int IRSensorRight, IRSensorCenter, IRSensorLeft; int GyroAngle, GyroAngleRate; int CliffSensorRight, CliffSensorCenter, CliffSensorLeft; diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index 6014547..6979f28 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -12,60 +12,60 @@ void logToFile(); int main() { - unsigned char *null_ptr(0); - robot.startCommunication("/dev/ttyUSB0", true, null_ptr); - - std::thread safety([&robot]() { robot.robotSafety(); }); // use a lambda function to call the member function + unsigned char *null_ptr(0); + robot.startCommunication("/dev/ttyUSB0", true, null_ptr); + + std::thread safety([&robot]() + { robot.robotSafety(); }); // use a lambda function to call the member function safety.detach(); thread movementThread(movement); - movementThread.join(); //so the program doesnt quit - return 0; + movementThread.join(); // so the program doesnt quit + return 0; } int checkCenterCliff() { - while (true) - { - std::cout << robot.parser.data.CliffSensorRight << endl; - } + while (true) + { + std::cout << robot.parser.data.CliffSensorRight << endl; + } } int movement() { - int text; - while (true) - { + int text; + while (true) + { cout << "gimme input: "; - cin >> text; + cin >> text; - if (text == 1) - { + if (text == 1) + { robot.forward(400); - - } - else if (text == 2) - { - // 1 is full circle - robot.Rotate(90); - } - else if (text == 3) - { - // Add your code here for text == 3 - } - else - { - try - { - robot.doRotation(text); - throw "NaN"; - } - catch (const char *msg) - { - cerr << msg << endl; - } - } - } + } + else if (text == 2) + { + // 1 is full circle + robot.Rotate(90); + } + else if (text == 3) + { + // Add your code here for text == 3 + } + else + { + try + { + robot.doRotation(text); + throw "NaN"; + } + catch (const char *msg) + { + cerr << msg << endl; + } + } + } } void logToFile() @@ -73,7 +73,7 @@ void logToFile() while (true) { TKobukiData robotData = robot.parser.data; - std::ofstream outputFile("log", std::ios_base::app); // Open file in append mode to not overwrite own content + std::ofstream outputFile("log", std::ios_base::app); // Open file in append mode to not overwrite own content if (outputFile.is_open()) { // check if the file was opened successfully // Get current time @@ -98,7 +98,9 @@ void logToFile() outputFile << "EncoderRight: " << robotData.EncoderRight << "\n"; outputFile << "PWMleft: " << robotData.PWMleft << "\n"; outputFile << "PWMright: " << robotData.PWMright << "\n"; - outputFile << "ButtonPress: " << robotData.ButtonPress << "\n"; + outputFile << "ButtonPress: " << robotData.ButtonPress1 << "\n"; + outputFile << "ButtonPress: " << robotData.ButtonPress2 << "\n"; + outputFile << "ButtonPress: " << robotData.ButtonPress3 << "\n"; outputFile << "Charger: " << robotData.Charger << "\n"; outputFile << "Battery: " << robotData.Battery << "\n"; outputFile << "overCurrent: " << robotData.overCurrent << "\n"; @@ -122,7 +124,7 @@ void logToFile() outputFile << "UDID0: " << robotData.extraInfo.UDID0 << "\n"; outputFile << "UDID1: " << robotData.extraInfo.UDID1 << "\n"; outputFile << "UDID2: " << robotData.extraInfo.UDID2 << "\n"; - outputFile.close(); + outputFile.close(); } else {