Compare commits

...
10 Commits
Author SHA1 Message Date
zeunerti74726 3dea00a8d1 Too big too handle 2023-03-03 19:48:03 +01:00
zeunerti74726 960b05e76f Testfahrten 2023-03-03 18:57:35 +01:00
zeunerti74726 94e9c4ba7a Faster! 2023-03-03 18:47:41 +01:00
zeunerti74726 6da9bfff54 Bugs found during tests 2023-03-03 18:46:31 +01:00
zeunerti74726 c40b69ebe2 Save video 2023-03-03 18:45:03 +01:00
zeunerti74726 397e6fe331 Let signals to stop always pass 2023-02-23 20:39:28 +01:00
zeunerti74726 60fbc8758d fabs instead of abs 2023-02-23 20:38:18 +01:00
zeunerti74726 fe4af52380 Merge branch 'master' of https://git.efi.th-nuernberg.de/gitea/yasarba71520/Line-Following-Robot 2023-02-23 11:12:40 +01:00
zeunerti74726 426f57798e uint -> int; max send rate; 2023-02-22 14:45:50 +01:00
zeunerti74726 9d896df965 Index error 2023-02-22 14:42:09 +01:00
6 changed files with 90 additions and 44 deletions
@@ -36,7 +36,7 @@ void ControlModule::adjustSpeed(){
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
{ {
motors[i] *= maxSpeed; motors[i] *= factor;
} }
}; };
@@ -85,7 +85,7 @@ void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, d
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
double rotationRatio = ratio(angle); double rotationRatio = ratio(angle);
rotate(rotationRatio); rotate(rotationRatio);
drive(1.0-rotationRatio); drive(1.0-std::fabs(rotationRatio));
unit(); unit();
adjustSpeed(); adjustSpeed();
} }
@@ -102,13 +102,21 @@ double ControlModule::ratio(double angle)
double maxAngularSpeed = 1.0; double maxAngularSpeed = 1.0;
double speedRange = maxAngularSpeed - minAngularSpeed; double speedRange = maxAngularSpeed - minAngularSpeed;
double minAngle = -90.0; double minAngle = -20.0;
double maxAngle = 90.0; double maxAngle = 20.0;
double angularRange = maxAngle - minAngle; double angularRange = maxAngle - minAngle;
if(angle < minAngle)
{
angle = minAngle;
}
if(angle > maxAngle)
{
angle = maxAngle;
}
double progress = angle - minAngle; double progress = angle - minAngle;
double progressPercent = progress/angularRange; double progressPercent = progress/angularRange;
double speed = minAngularSpeed + progressPercent*speedRange; double speed = minAngularSpeed + progressPercent*speedRange;
return abs(speed); return speed;
} }
@@ -2,6 +2,7 @@
#include <mutex> #include <mutex>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <cmath>
class ControlModule class ControlModule
{ {
+37 -24
View File
@@ -2,6 +2,7 @@
LFR_UART::LFR_UART() : fileDescriptor(-1) { LFR_UART::LFR_UART() : fileDescriptor(-1) {
this->last =std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
this->openSerialPort(); this->openSerialPort();
this->configureSerialPort(); this->configureSerialPort();
} }
@@ -13,12 +14,13 @@ void LFR_UART::openFile(const char *fileName) {
this->fileDescriptor = open(fileName, O_RDWR | O_NONBLOCK); this->fileDescriptor = open(fileName, O_RDWR | O_NONBLOCK);
} }
int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) { int LFR_UART::writeDataToFile(int8_t *buff, uint32_t bufferLength) {
//std::cout << "Sending uart telegram" << std::endl; //std::cout << "Sending uart: " << std::bitset<8>(buff[0]) << std::endl;
std::cout << "Sending Uart: " << std::bitset<8>(buff[0]) << ", " << std::bitset<8>(buff[1]) << ", " << std::bitset<8>(buff[2]) << ", " << std::bitset<8>(buff[3]) << " csum: " << std::bitset<8>(buff[4]) << std::endl;
return write(this->fileDescriptor, buff, bufferLength); return write(this->fileDescriptor, buff, bufferLength);
} }
int LFR_UART::readFromFile(uint8_t *buff, uint32_t bufferLength) { int LFR_UART::readFromFile(int8_t *buff, uint32_t bufferLength) {
return read(this->fileDescriptor, buff, bufferLength); return read(this->fileDescriptor, buff, bufferLength);
} }
@@ -26,37 +28,32 @@ int LFR_UART::closeFile() {
return close(this->fileDescriptor); return close(this->fileDescriptor);
} }
uint8_t LFR_UART::doubleToByte(double in){ int8_t LFR_UART::doubleToByte(double in){
/* /*
* Map the range of -1.0 to 1.0 as double to the range of a byte. * Map the range of -1.0 to 1.0 as double to the range of a byte. (int8 range)
*
* -1 -> 0
* 0.0 -> 127
* 1.0 -> 254
* Not using the full range upto 254 to hit the 0.0
*/ */
double minDouble = -1.0; double minDouble = -1.0;
double maxDouble = 1.0; double maxDouble = 1.0;
double rangeDouble = maxDouble - minDouble; double rangeDouble = maxDouble - minDouble;
double minByte = 0.0; double minByte = -128.0;
double maxByte = 255.0; double maxByte = 127.0;
double rangeByte = maxByte - minByte; double rangeByte = maxByte - minByte;
double progress = in - minDouble; double progress = in - minDouble;
double progressPercent = progress/rangeDouble; double progressPercent = progress/rangeDouble;
double inputInByteRange = minByte + progressPercent*rangeByte; double inputInByteRange = minByte + progressPercent*rangeByte;
return uint8_t(inputInByteRange); return int8_t(inputInByteRange);
} }
double LFR_UART::byteToDouble(uint8_t in){ double LFR_UART::byteToDouble(int8_t in){
double minDouble = -1.0; double minDouble = -1.0;
double maxDouble = 1.0; double maxDouble = 1.0;
double rangeDouble = maxDouble - minDouble; double rangeDouble = maxDouble - minDouble;
double minByte = 0.0; double minByte = -128.0;
double maxByte = 255.0; double maxByte = 127.0;
double rangeByte = maxByte - minByte; double rangeByte = maxByte - minByte;
double progress = double(in) - minByte; double progress = double(in) - minByte;
@@ -67,27 +64,43 @@ double LFR_UART::byteToDouble(uint8_t in){
} }
void LFR_UART::sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4){ void LFR_UART::sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4){
std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
unsigned int deltaMs = static_cast<unsigned int>((now-last).count());
if (deltaMs < 50 && (std::fabs(wheel1)+std::fabs(wheel2)+std::fabs(wheel3)+std::fabs(wheel4)) > 0.0005)
{
std::cout << "Too fast" << std::endl;
return;
}
last = now;
if(wheel1 > 1.0 || wheel2 > 1.0 || wheel3 > 1.0 || wheel4 > 1.0){ if(wheel1 > 1.0 || wheel2 > 1.0 || wheel3 > 1.0 || wheel4 > 1.0){
throw CommunicatorException("Wheel value must not be greater than 1.0"); throw CommunicatorException("Wheel value must not be greater than 1.0");
} }
if(wheel1 < -1.0 || wheel2 < -1.0 || wheel3 < -1.0 || wheel4 < -1.0){ if(wheel1 < -1.0 || wheel2 < -1.0 || wheel3 < -1.0 || wheel4 < -1.0){
throw CommunicatorException("Wheel value must not be smaller than -1.0"); throw CommunicatorException("Wheel value must not be smaller than -1.0");
} }
// Discrepancy between the numbering of the wheels in App/Pi and the µc
// Our - their
// 1 - 4
// 2 - 2
// 3 - 3
// 4 - 1
int8_t wheel1B = this->doubleToByte(wheel4);
//int8_t wheel1B = this->doubleToByte(wheel1);
int8_t wheel2B = this->doubleToByte(wheel2);
int8_t wheel3B = this->doubleToByte(wheel3);
int8_t wheel4B = this->doubleToByte(wheel1);
//int8_t wheel4B = this->doubleToByte(wheel4);
uint8_t wheel1B = this->doubleToByte(wheel1); int8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B;
uint8_t wheel2B = this->doubleToByte(wheel2);
uint8_t wheel3B = this->doubleToByte(wheel3);
uint8_t wheel4B = this->doubleToByte(wheel4);
uint8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B; int8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
uint8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
uint32_t telegram_length = 5; uint32_t telegram_length = 5;
this->writeDataToFile(telegram_buffer, telegram_length); this->writeDataToFile(telegram_buffer, telegram_length);
} }
bool LFR_UART::readTelegram(double* buffer){ bool LFR_UART::readTelegram(double* buffer){
uint8_t tmp_buffer[5] = {0, 0, 0, 0, 0}; int8_t tmp_buffer[5] = {0, 0, 0, 0, 0};
uint32_t telegram_length = 5; uint32_t telegram_length = 5;
this->readFromFile(tmp_buffer, telegram_length); this->readFromFile(tmp_buffer, telegram_length);
+9 -4
View File
@@ -11,12 +11,16 @@
#include <iostream> #include <iostream>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <chrono>
#include <cmath>
#include <bitset> #include <bitset>
class LFR_UART class LFR_UART
{ {
public:
std::chrono::milliseconds last;
int fileDescriptor; int fileDescriptor;
const char* serialPortPath = "/dev/ttyS0"; const char* serialPortPath = "/dev/ttyS0";
@@ -29,13 +33,14 @@ class LFR_UART
void configureSerialPort(); void configureSerialPort();
void closeSerialPort(); void closeSerialPort();
int writeDataToFile(uint8_t *buff, uint32_t bufferLength);
int readFromFile(uint8_t *buff, uint32_t bufferLength); int writeDataToFile(int8_t *buff, uint32_t bufferLength);
int readFromFile(int8_t *buff, uint32_t bufferLength);
public: public:
uint8_t doubleToByte(double in); int8_t doubleToByte(double in);
double byteToDouble(uint8_t in); double byteToDouble(int8_t in);
void sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4); void sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4);
bool readTelegram(double* buffer); bool readTelegram(double* buffer);
+24 -10
View File
@@ -67,9 +67,9 @@ void LFR_StateMachine::parseString(string s)
double wheels[4] = {0.0, 0.0, 0.0, 0.0}; double wheels[4] = {0.0, 0.0, 0.0, 0.0};
int mode = std::stoi(splitStr[0]); int mode = std::stoi(splitStr[0]);
if(mode == 0) { if(mode == 0) {
for(int i = 1; i < 4; i++) for(int i = 1; i <= 4; i++)
{ {
wheels[i] = std::stod(splitStr[i]); wheels[i-1] = std::stod(splitStr[i]);
} }
setState(State::Manual::getInstance()); setState(State::Manual::getInstance());
uartCommunicator.sendTelegram(wheels[0], wheels[1], wheels[2], wheels[3]); uartCommunicator.sendTelegram(wheels[0], wheels[1], wheels[2], wheels[3]);
@@ -104,16 +104,26 @@ LFR_StateMachine::LFR_StateMachine():
currentState = &State::Idle::getInstance(); currentState = &State::Idle::getInstance();
currentState->enter(this); currentState->enter(this);
//Start the permanent loop cv::VideoWriter writer = cv::VideoWriter("video_200.avi", cv::VideoWriter::fourcc('M','J','P','G'), 4.0, cv::Size(videoWidth, videoHeight), true);
char input;
std::cout << "press q to quit" << std::endl; auto t_start = std::chrono::high_resolution_clock::now();
std::cin >> input; auto t_end = std::chrono::high_resolution_clock::now();
std::cout << "binned" << std::endl; double dur = std::chrono::duration<double, std::milli>(t_end-t_start).count();
while (input != 'q')
while(dur < 60000)
{ {
std::cin >> input; t_end = std::chrono::high_resolution_clock::now();
std::cout << "binned" << std::endl; dur = std::chrono::duration<double, std::milli>(t_end-t_start).count();
{
this_thread::sleep_for(std::chrono::milliseconds(50));
std::unique_lock<std::mutex> lock(imgMutex);
if(!this->image.empty())
{
writer.write(this->image);
}
}
} }
writer.release();
std::cout << "Exiting central" << std::endl; std::cout << "Exiting central" << std::endl;
} }
@@ -146,6 +156,10 @@ void LFR_StateMachine::enterAutonomous()
double delta = static_cast<double>(deltaMs) / 1000.0; double delta = static_cast<double>(deltaMs) / 1000.0;
double frameRate = 1.0 / static_cast<double>(delta);*/ double frameRate = 1.0 / static_cast<double>(delta);*/
double frameRate = -1.0; double frameRate = -1.0;
{
std::unique_lock<std::mutex> lock(imgMutex);
image = result.rawImage;
}
if (result.validLane) if (result.validLane)
{ {
+6 -1
View File
@@ -16,7 +16,7 @@ class LFR_StateMachine
const int videoHeight = 720; const int videoHeight = 720;
const int videoWidth = 1280; const int videoWidth = 1280;
const int gaussKernelSize = 11; const int gaussKernelSize = 11;
const double maxSpeed = 0.5; const double maxSpeed = 0.20;
std::mutex mutex; std::mutex mutex;
@@ -24,6 +24,9 @@ class LFR_StateMachine
LFR_UART uartCommunicator; LFR_UART uartCommunicator;
LFR_Socket socket; LFR_Socket socket;
std::mutex imgMutex;
Mat image;
vector<string> split (string s, string delimiter) const; vector<string> split (string s, string delimiter) const;
void sanitize (string& s) const; void sanitize (string& s) const;
bool checkStringValidity (const std::vector<std::string>& s) const; bool checkStringValidity (const std::vector<std::string>& s) const;
@@ -36,3 +39,5 @@ public:
inline LFR_IState* getCurrentState() const {return currentState;} inline LFR_IState* getCurrentState() const {return currentState;}
void setState(LFR_IState& newState); void setState(LFR_IState& newState);
}; };