Compare commits
10
Commits
ce6ca27490
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3dea00a8d1 | ||
|
|
960b05e76f | ||
|
|
94e9c4ba7a | ||
|
|
6da9bfff54 | ||
|
|
c40b69ebe2 | ||
|
|
397e6fe331 | ||
|
|
60fbc8758d | ||
|
|
fe4af52380 | ||
|
|
426f57798e | ||
|
|
9d896df965 |
@@ -36,7 +36,7 @@ void ControlModule::adjustSpeed(){
|
||||
|
||||
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);
|
||||
double rotationRatio = ratio(angle);
|
||||
rotate(rotationRatio);
|
||||
drive(1.0-rotationRatio);
|
||||
drive(1.0-std::fabs(rotationRatio));
|
||||
unit();
|
||||
adjustSpeed();
|
||||
}
|
||||
@@ -102,13 +102,21 @@ double ControlModule::ratio(double angle)
|
||||
double maxAngularSpeed = 1.0;
|
||||
double speedRange = maxAngularSpeed - minAngularSpeed;
|
||||
|
||||
double minAngle = -90.0;
|
||||
double maxAngle = 90.0;
|
||||
double minAngle = -20.0;
|
||||
double maxAngle = 20.0;
|
||||
double angularRange = maxAngle - minAngle;
|
||||
|
||||
if(angle < minAngle)
|
||||
{
|
||||
angle = minAngle;
|
||||
}
|
||||
if(angle > maxAngle)
|
||||
{
|
||||
angle = maxAngle;
|
||||
}
|
||||
double progress = angle - minAngle;
|
||||
double progressPercent = progress/angularRange;
|
||||
|
||||
double speed = minAngularSpeed + progressPercent*speedRange;
|
||||
return abs(speed);
|
||||
return speed;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
class ControlModule
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
|
||||
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->configureSerialPort();
|
||||
}
|
||||
@@ -13,12 +14,13 @@ void LFR_UART::openFile(const char *fileName) {
|
||||
this->fileDescriptor = open(fileName, O_RDWR | O_NONBLOCK);
|
||||
}
|
||||
|
||||
int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) {
|
||||
//std::cout << "Sending uart telegram" << std::endl;
|
||||
int LFR_UART::writeDataToFile(int8_t *buff, uint32_t bufferLength) {
|
||||
//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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -26,37 +28,32 @@ int LFR_UART::closeFile() {
|
||||
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.
|
||||
*
|
||||
* -1 -> 0
|
||||
* 0.0 -> 127
|
||||
* 1.0 -> 254
|
||||
* Not using the full range upto 254 to hit the 0.0
|
||||
* Map the range of -1.0 to 1.0 as double to the range of a byte. (int8 range)
|
||||
*/
|
||||
double minDouble = -1.0;
|
||||
double maxDouble = 1.0;
|
||||
double rangeDouble = maxDouble - minDouble;
|
||||
|
||||
double minByte = 0.0;
|
||||
double maxByte = 255.0;
|
||||
double minByte = -128.0;
|
||||
double maxByte = 127.0;
|
||||
double rangeByte = maxByte - minByte;
|
||||
|
||||
double progress = in - minDouble;
|
||||
double progressPercent = progress/rangeDouble;
|
||||
|
||||
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 maxDouble = 1.0;
|
||||
double rangeDouble = maxDouble - minDouble;
|
||||
|
||||
double minByte = 0.0;
|
||||
double maxByte = 255.0;
|
||||
double minByte = -128.0;
|
||||
double maxByte = 127.0;
|
||||
double rangeByte = maxByte - 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){
|
||||
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){
|
||||
throw CommunicatorException("Wheel value must not be greater than 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");
|
||||
}
|
||||
// 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);
|
||||
uint8_t wheel2B = this->doubleToByte(wheel2);
|
||||
uint8_t wheel3B = this->doubleToByte(wheel3);
|
||||
uint8_t wheel4B = this->doubleToByte(wheel4);
|
||||
int8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B;
|
||||
|
||||
uint8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B;
|
||||
|
||||
uint8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
|
||||
int8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
|
||||
uint32_t telegram_length = 5;
|
||||
this->writeDataToFile(telegram_buffer, telegram_length);
|
||||
}
|
||||
|
||||
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;
|
||||
this->readFromFile(tmp_buffer, telegram_length);
|
||||
|
||||
|
||||
@@ -11,12 +11,16 @@
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
#include <bitset>
|
||||
|
||||
|
||||
class LFR_UART
|
||||
{
|
||||
public:
|
||||
std::chrono::milliseconds last;
|
||||
int fileDescriptor;
|
||||
const char* serialPortPath = "/dev/ttyS0";
|
||||
|
||||
@@ -29,13 +33,14 @@ class LFR_UART
|
||||
void configureSerialPort();
|
||||
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:
|
||||
|
||||
uint8_t doubleToByte(double in);
|
||||
double byteToDouble(uint8_t in);
|
||||
int8_t doubleToByte(double in);
|
||||
double byteToDouble(int8_t in);
|
||||
|
||||
void sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4);
|
||||
bool readTelegram(double* buffer);
|
||||
|
||||
+24
-10
@@ -67,9 +67,9 @@ void LFR_StateMachine::parseString(string s)
|
||||
double wheels[4] = {0.0, 0.0, 0.0, 0.0};
|
||||
int mode = std::stoi(splitStr[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());
|
||||
uartCommunicator.sendTelegram(wheels[0], wheels[1], wheels[2], wheels[3]);
|
||||
@@ -104,16 +104,26 @@ LFR_StateMachine::LFR_StateMachine():
|
||||
currentState = &State::Idle::getInstance();
|
||||
currentState->enter(this);
|
||||
|
||||
//Start the permanent loop
|
||||
char input;
|
||||
std::cout << "press q to quit" << std::endl;
|
||||
std::cin >> input;
|
||||
std::cout << "binned" << std::endl;
|
||||
while (input != 'q')
|
||||
cv::VideoWriter writer = cv::VideoWriter("video_200.avi", cv::VideoWriter::fourcc('M','J','P','G'), 4.0, cv::Size(videoWidth, videoHeight), true);
|
||||
|
||||
auto t_start = std::chrono::high_resolution_clock::now();
|
||||
auto t_end = std::chrono::high_resolution_clock::now();
|
||||
double dur = std::chrono::duration<double, std::milli>(t_end-t_start).count();
|
||||
|
||||
while(dur < 60000)
|
||||
{
|
||||
std::cin >> input;
|
||||
std::cout << "binned" << std::endl;
|
||||
t_end = std::chrono::high_resolution_clock::now();
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -146,6 +156,10 @@ void LFR_StateMachine::enterAutonomous()
|
||||
double delta = static_cast<double>(deltaMs) / 1000.0;
|
||||
double frameRate = 1.0 / static_cast<double>(delta);*/
|
||||
double frameRate = -1.0;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(imgMutex);
|
||||
image = result.rawImage;
|
||||
}
|
||||
|
||||
if (result.validLane)
|
||||
{
|
||||
|
||||
+6
-1
@@ -16,7 +16,7 @@ class LFR_StateMachine
|
||||
const int videoHeight = 720;
|
||||
const int videoWidth = 1280;
|
||||
const int gaussKernelSize = 11;
|
||||
const double maxSpeed = 0.5;
|
||||
const double maxSpeed = 0.20;
|
||||
|
||||
std::mutex mutex;
|
||||
|
||||
@@ -24,6 +24,9 @@ class LFR_StateMachine
|
||||
LFR_UART uartCommunicator;
|
||||
LFR_Socket socket;
|
||||
|
||||
std::mutex imgMutex;
|
||||
Mat image;
|
||||
|
||||
vector<string> split (string s, string delimiter) const;
|
||||
void sanitize (string& s) const;
|
||||
bool checkStringValidity (const std::vector<std::string>& s) const;
|
||||
@@ -36,3 +39,5 @@ public:
|
||||
inline LFR_IState* getCurrentState() const {return currentState;}
|
||||
void setState(LFR_IState& newState);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user