added curly brackets

This commit is contained in:
ishak jmilou.ishak
2024-10-10 12:24:15 +02:00
parent 9de0bbf50e
commit a8fcf0e0f8

View File

@@ -4,19 +4,16 @@
#include <cstddef> #include <cstddef>
#include <iostream> #include <iostream>
// plot p; // plot p;
static std::vector<float> vectorX; static std::vector<float> vectorX;
static std::vector<float> vectorY; static std::vector<float> vectorY;
static std::vector<float> vectorGyroTheta; static std::vector<float> vectorGyroTheta;
// obsluha tty pod unixom // obsluha tty pod unixom
int set_interface_attribs2(int fd, int speed, int parity) int set_interface_attribs2(int fd, int speed, int parity) {
{
struct termios tty; struct termios tty;
memset(&tty, 0, sizeof tty); memset(&tty, 0, sizeof tty);
if (tcgetattr(fd, &tty) != 0) if (tcgetattr(fd, &tty) != 0) {
{
printf("error %d from tcgetattr", errno); printf("error %d from tcgetattr", errno);
return -1; return -1;
} }
@@ -44,20 +41,17 @@ int set_interface_attribs2(int fd, int speed, int parity)
tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS; tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) if (tcsetattr(fd, TCSANOW, &tty) != 0) {
{
printf("error %d from tcsetattr", errno); printf("error %d from tcsetattr", errno);
return -1; return -1;
} }
return 0; return 0;
} }
void set_blocking2(int fd, int should_block) void set_blocking2(int fd, int should_block) {
{
struct termios tty; struct termios tty;
memset(&tty, 0, sizeof tty); memset(&tty, 0, sizeof tty);
if (tcgetattr(fd, &tty) != 0) if (tcgetattr(fd, &tty) != 0) {
{
printf("error %d from tggetattr", errno); printf("error %d from tggetattr", errno);
return; return;
} }
@@ -69,17 +63,13 @@ void set_blocking2(int fd, int should_block)
printf("error %d setting term attributes", errno); printf("error %d setting term attributes", errno);
} }
int CKobuki::connect(char *comportT) int CKobuki::connect(char *comportT) {
{
HCom = open(comportT, O_RDWR | O_NOCTTY | O_NONBLOCK); HCom = open(comportT, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (HCom == -1) if (HCom == -1) {
{
printf("Kobuki nepripojeny\n"); printf("Kobuki nepripojeny\n");
return HCom; return HCom;
} } else {
else
{
set_interface_attribs2(HCom, B115200, set_interface_attribs2(HCom, B115200,
0); // set speed to 115,200 bps, 8n1 (no parity) 0); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking2(HCom, 0); // set no blocking set_blocking2(HCom, 0); // set no blocking
@@ -103,37 +93,31 @@ int CKobuki::connect(char *comportT)
} }
} }
unsigned char *CKobuki::readKobukiMessage() unsigned char *CKobuki::readKobukiMessage() {
{
unsigned char buffer[1]; unsigned char buffer[1];
ssize_t Pocet; ssize_t Pocet;
buffer[0] = 0; buffer[0] = 0;
unsigned char *null_buffer(0); unsigned char *null_buffer(0);
// citame kym nezachytime zaciatok spravy // citame kym nezachytime zaciatok spravy
do do {
{
Pocet = read(HCom, buffer, 1); Pocet = read(HCom, buffer, 1);
} while (buffer[0] != 0xAA); } while (buffer[0] != 0xAA);
// mame zaciatok spravy (asi) // mame zaciatok spravy (asi)
if (Pocet == 1 && buffer[0] == 0xAA) if (Pocet == 1 && buffer[0] == 0xAA) {
{
// citame dalsi byte // citame dalsi byte
do do {
{
Pocet = read(HCom, buffer, 1); Pocet = read(HCom, buffer, 1);
} while (Pocet != 1); // na linuxe -1 na windowse 0 } while (Pocet != 1); // na linuxe -1 na windowse 0
// a ak je to druhy byte hlavicky // a ak je to druhy byte hlavicky
if (Pocet == 1 && buffer[0] == 0x55) if (Pocet == 1 && buffer[0] == 0x55) {
{
// precitame dlzku // precitame dlzku
Pocet = read(HCom, buffer, 1); Pocet = read(HCom, buffer, 1);
// ReadFile(hCom, buffer, 1, &Pocet, NULL); // ReadFile(hCom, buffer, 1, &Pocet, NULL);
if (Pocet == 1) if (Pocet == 1) {
{
// mame dlzku.. nastavime vektor a precitame ho cely // mame dlzku.. nastavime vektor a precitame ho cely
int readLenght = buffer[0]; int readLenght = buffer[0];
unsigned char *outputBuffer = unsigned char *outputBuffer =
@@ -141,8 +125,7 @@ unsigned char *CKobuki::readKobukiMessage()
outputBuffer[0] = buffer[0]; outputBuffer[0] = buffer[0];
int pct = 0; int pct = 0;
do do {
{
Pocet = 0; Pocet = 0;
int readpoc = (readLenght + 1 - pct); int readpoc = (readLenght + 1 - pct);
Pocet = read(HCom, outputBuffer + 1 + pct, readpoc); Pocet = read(HCom, outputBuffer + 1 + pct, readpoc);
@@ -164,9 +147,15 @@ unsigned char *CKobuki::readKobukiMessage()
return null_buffer; return null_buffer;
} }
void CKobuki::setLed(int led1, int led2) void CKobuki::setLed(int led1, int led2) {
{ unsigned char message[8] = {0xaa,
unsigned char message[8] = {0xaa, 0x55, 0x04, 0x0c, 0x02, 0x00, (unsigned char)((led1 + led2 * 4) % 256), 0x00}; 0x55,
0x04,
0x0c,
0x02,
0x00,
(unsigned char)((led1 + led2 * 4) % 256),
0x00};
message[7] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6]; message[7] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6];
uint32_t pocet; uint32_t pocet;
pocet = write(HCom, &message, 8); pocet = write(HCom, &message, 8);
@@ -174,18 +163,15 @@ void CKobuki::setLed(int led1, int led2)
// tato funkcia nema moc sama o sebe vyznam, payload o tom, ze maju byt externe // tato funkcia nema moc sama o sebe vyznam, payload o tom, ze maju byt externe
// napajania aktivne musi byt aj tak v kazdej sprave... // napajania aktivne musi byt aj tak v kazdej sprave...
void CKobuki::setPower(int value) void CKobuki::setPower(int value) {
{ if (value == 1) {
if (value == 1)
{
unsigned char message[8] = {0xaa, 0x55, 0x04, 0x0C, 0x02, 0xf0, 0x00, 0xAF}; unsigned char message[8] = {0xaa, 0x55, 0x04, 0x0C, 0x02, 0xf0, 0x00, 0xAF};
uint32_t pocet; uint32_t pocet;
pocet = write(HCom, &message, 8); pocet = write(HCom, &message, 8);
} }
} }
void CKobuki::setTranslationSpeed(int mmpersec) void CKobuki::setTranslationSpeed(int mmpersec) {
{
unsigned char message[14] = {0xaa, 0x55, 0x0A, 0x0c, 0x02, unsigned char message[14] = {0xaa, 0x55, 0x0A, 0x0c, 0x02,
0xf0, 0x00, 0x01, 0x04, mmpersec % 256, 0xf0, 0x00, 0x01, 0x04, mmpersec % 256,
mmpersec >> 8, 0x00, 0x00, 0x00}; mmpersec >> 8, 0x00, 0x00, 0x00};
@@ -197,8 +183,7 @@ void CKobuki::setTranslationSpeed(int mmpersec)
pocet = write(HCom, &message, 14); pocet = write(HCom, &message, 14);
} }
void CKobuki::setRotationSpeed(double radpersec) void CKobuki::setRotationSpeed(double radpersec) {
{
int speedvalue = radpersec * 230.0f / 2.0f; int speedvalue = radpersec * 230.0f / 2.0f;
unsigned char message[14] = {0xaa, unsigned char message[14] = {0xaa,
0x55, 0x55,
@@ -222,10 +207,8 @@ void CKobuki::setRotationSpeed(double radpersec)
pocet = write(HCom, &message, 14); pocet = write(HCom, &message, 14);
} }
void CKobuki::setArcSpeed(int mmpersec, int radius) void CKobuki::setArcSpeed(int mmpersec, int radius) {
{ if (radius == 0) {
if (radius == 0)
{
setTranslationSpeed(mmpersec); setTranslationSpeed(mmpersec);
return; return;
} }
@@ -253,8 +236,7 @@ void CKobuki::setArcSpeed(int mmpersec, int radius)
pocet = write(HCom, &message, 14); pocet = write(HCom, &message, 14);
} }
void CKobuki::setSound(int noteinHz, int duration) void CKobuki::setSound(int noteinHz, int duration) {
{
int notevalue = floor((double)1.0 / ((double)noteinHz * 0.00000275) + 0.5); int notevalue = floor((double)1.0 / ((double)noteinHz * 0.00000275) + 0.5);
unsigned char message[9] = {0xaa, 0x55, 0x05, unsigned char message[9] = {0xaa, 0x55, 0x05,
0x03, 0x03, notevalue % 256, 0x03, 0x03, notevalue % 256,
@@ -266,26 +248,24 @@ void CKobuki::setSound(int noteinHz, int duration)
pocet = write(HCom, &message, 9); pocet = write(HCom, &message, 9);
} }
void CKobuki::startCommunication(char *portname, bool CommandsEnabled, void *userDataL) void CKobuki::startCommunication(char *portname, bool CommandsEnabled,
{ void *userDataL) {
connect(portname); connect(portname);
enableCommands(CommandsEnabled); enableCommands(CommandsEnabled);
userData = userDataL; userData = userDataL;
int pthread_result; int pthread_result;
pthread_result = pthread_create(&threadHandle, NULL, KobukiProcess, (void *)this); pthread_result =
pthread_create(&threadHandle, NULL, KobukiProcess, (void *)this);
if (pthread_result != 0) { if (pthread_result != 0) {
std::cerr << "Error creating thread: " << pthread_result << std::endl; std::cerr << "Error creating thread: " << pthread_result << std::endl;
} }
} }
int CKobuki::measure() int CKobuki::measure() {
{ while (stopVlakno == 0) {
while (stopVlakno == 0)
{
unsigned char *message = readKobukiMessage(); unsigned char *message = readKobukiMessage();
if (message == NULL) if (message == NULL) {
{
// printf("vratil null message\n"); // printf("vratil null message\n");
continue; continue;
} }
@@ -293,8 +273,7 @@ int CKobuki::measure()
// maximalne moze trvat callback funkcia 20 ms, ak by trvala viac, nestihame // maximalne moze trvat callback funkcia 20 ms, ak by trvala viac, nestihame
// citat // citat
if (ok == 0) if (ok == 0) {
{
loop(userData, parser.data); loop(userData, parser.data);
} }
free(message); free(message);
@@ -320,25 +299,20 @@ int CKobuki::measure()
// if (data[checkedValue] != 0x0F) // if (data[checkedValue] != 0x0F)
// return -1; // return -1;
// checkedValue++; // checkedValue++;
// output.timestamp = data[checkedValue + 1] * 256 + data[checkedValue]; // output.timestamp = data[checkedValue + 1] * 256 +
// checkedValue += 2; // data[checkedValue]; checkedValue += 2; output.BumperCenter =
// output.BumperCenter = data[checkedValue] && 0x02; // data[checkedValue] && 0x02; output.BumperLeft =
// output.BumperLeft = data[checkedValue] && 0x04; // data[checkedValue] && 0x04; output.BumperRight =
// output.BumperRight = data[checkedValue] && 0x01; // data[checkedValue] && 0x01; checkedValue++; output.WheelDropLeft
// checkedValue++; // = data[checkedValue] && 0x02; output.WheelDropRight =
// output.WheelDropLeft = data[checkedValue] && 0x02; // data[checkedValue] && 0x01; checkedValue++; output.CliffCenter =
// output.WheelDropRight = data[checkedValue] && 0x01; // data[checkedValue] && 0x02; output.CliffLeft = data[checkedValue]
// checkedValue++; // && 0x04; output.CliffRight = data[checkedValue] && 0x01;
// output.CliffCenter = data[checkedValue] && 0x02;
// output.CliffLeft = data[checkedValue] && 0x04;
// output.CliffRight = data[checkedValue] && 0x01;
// checkedValue++;
// output.EncoderLeft = data[checkedValue + 1] * 256 + data[checkedValue];
// checkedValue += 2;
// output.EncoderRight = data[checkedValue + 1] * 256 + data[checkedValue];
// checkedValue += 2;
// output.PWMleft = data[checkedValue];
// checkedValue++; // checkedValue++;
// output.EncoderLeft = data[checkedValue + 1] * 256 +
// data[checkedValue]; checkedValue += 2; output.EncoderRight =
// data[checkedValue + 1] * 256 + data[checkedValue]; checkedValue
// += 2; output.PWMleft = data[checkedValue]; checkedValue++;
// output.PWMright = data[checkedValue]; // output.PWMright = data[checkedValue];
// checkedValue++; // checkedValue++;
// output.ButtonPress = data[checkedValue]; // output.ButtonPress = data[checkedValue];
@@ -369,10 +343,10 @@ int CKobuki::measure()
// if (data[checkedValue] != 0x07) // if (data[checkedValue] != 0x07)
// return -4; // return -4;
// checkedValue++; // checkedValue++;
// output.GyroAngle = data[checkedValue + 1] * 256 + data[checkedValue]; // output.GyroAngle = data[checkedValue + 1] * 256 +
// checkedValue += 2; // data[checkedValue]; checkedValue += 2; output.GyroAngleRate =
// output.GyroAngleRate = data[checkedValue + 1] * 256 + data[checkedValue]; // data[checkedValue + 1] * 256 + data[checkedValue]; checkedValue
// checkedValue += 5; // 3 unsued // += 5; // 3 unsued
// } // }
// else if (data[checkedValue] == 0x05) // else if (data[checkedValue] == 0x05)
// { // {
@@ -457,16 +431,14 @@ int CKobuki::measure()
// if (data[checkedValue] != 0x10) // if (data[checkedValue] != 0x10)
// return -10; // return -10;
// checkedValue++; // checkedValue++;
// output.digitalInput = data[checkedValue + 1] * 256 + data[checkedValue]; // output.digitalInput = data[checkedValue + 1] * 256 +
// checkedValue += 2; // data[checkedValue]; checkedValue += 2; output.analogInputCh0 =
// output.analogInputCh0 = data[checkedValue + 1] * 256 + data[checkedValue]; // data[checkedValue + 1] * 256 + data[checkedValue]; checkedValue
// checkedValue += 2; // += 2; output.analogInputCh1 = data[checkedValue + 1] * 256 +
// output.analogInputCh1 = data[checkedValue + 1] * 256 + data[checkedValue]; // data[checkedValue]; checkedValue += 2; output.analogInputCh2 =
// checkedValue += 2; // data[checkedValue + 1] * 256 + data[checkedValue]; checkedValue
// output.analogInputCh2 = data[checkedValue + 1] * 256 + data[checkedValue]; // += 2; output.analogInputCh3 = data[checkedValue + 1] * 256 +
// checkedValue += 2; // data[checkedValue]; checkedValue += 8; // 2+6
// output.analogInputCh3 = data[checkedValue + 1] * 256 + data[checkedValue];
// checkedValue += 8; // 2+6
// } // }
// else if (data[checkedValue] == 0x13) // else if (data[checkedValue] == 0x13)
// { // {
@@ -474,17 +446,20 @@ int CKobuki::measure()
// if (data[checkedValue] != 0x0C) // if (data[checkedValue] != 0x0C)
// return -11; // return -11;
// checkedValue++; // checkedValue++;
// output.extraInfo.UDID0 = data[checkedValue + 3] * 256 * 256 * 256 + // output.extraInfo.UDID0 = data[checkedValue + 3] * 256 * 256 * 256
// +
// data[checkedValue + 2] * 256 * 256 + // data[checkedValue + 2] * 256 * 256 +
// data[checkedValue + 1] * 256 + // data[checkedValue + 1] * 256 +
// data[checkedValue]; // data[checkedValue];
// checkedValue += 4; // checkedValue += 4;
// output.extraInfo.UDID1 = data[checkedValue + 3] * 256 * 256 * 256 + // output.extraInfo.UDID1 = data[checkedValue + 3] * 256 * 256 * 256
// +
// data[checkedValue + 2] * 256 * 256 + // data[checkedValue + 2] * 256 * 256 +
// data[checkedValue + 1] * 256 + // data[checkedValue + 1] * 256 +
// data[checkedValue]; // data[checkedValue];
// checkedValue += 4; // checkedValue += 4;
// output.extraInfo.UDID2 = data[checkedValue + 3] * 256 * 256 * 256 + // output.extraInfo.UDID2 = data[checkedValue + 3] * 256 * 256 * 256
// +
// data[checkedValue + 2] * 256 * 256 + // data[checkedValue + 2] * 256 * 256 +
// data[checkedValue + 1] * 256 + // data[checkedValue + 1] * 256 +
// data[checkedValue]; // data[checkedValue];
@@ -499,25 +474,19 @@ int CKobuki::measure()
// return 0; // return 0;
// } // }
long double CKobuki::gyroToRad(signed short GyroAngle) long double CKobuki::gyroToRad(signed short GyroAngle) {
{
long double rad; long double rad;
if (GyroAngle < 0) if (GyroAngle < 0) {
{
rad = GyroAngle + 360; rad = GyroAngle + 360;
} } else {
else
{
rad = GyroAngle; rad = GyroAngle;
} }
return (long double)rad * PI / 180.0; return (long double)rad * PI / 180.0;
} }
long CKobuki::loop(void *user_data, TKobukiData &Kobuki_data) long CKobuki::loop(void *user_data, TKobukiData &Kobuki_data) {
{ if (iterationCount == 0) {
if (iterationCount == 0)
{
prevLeftEncoder = Kobuki_data.EncoderLeft; prevLeftEncoder = Kobuki_data.EncoderLeft;
prevRightEncoder = Kobuki_data.EncoderRight; prevRightEncoder = Kobuki_data.EncoderRight;
prevTimestamp = Kobuki_data.timestamp; prevTimestamp = Kobuki_data.timestamp;
@@ -526,35 +495,27 @@ long CKobuki::loop(void *user_data, TKobukiData &Kobuki_data)
} }
int dLeft; int dLeft;
if (abs(Kobuki_data.EncoderLeft - prevLeftEncoder) > 32000) if (abs(Kobuki_data.EncoderLeft - prevLeftEncoder) > 32000) {
{
dLeft = Kobuki_data.EncoderLeft - prevLeftEncoder + dLeft = Kobuki_data.EncoderLeft - prevLeftEncoder +
(Kobuki_data.EncoderLeft > prevLeftEncoder ? -65536 : +65536); (Kobuki_data.EncoderLeft > prevLeftEncoder ? -65536 : +65536);
} } else {
else
{
dLeft = Kobuki_data.EncoderLeft - prevLeftEncoder; dLeft = Kobuki_data.EncoderLeft - prevLeftEncoder;
} }
int dRight; int dRight;
if (abs(Kobuki_data.EncoderRight - prevRightEncoder) > 32000) if (abs(Kobuki_data.EncoderRight - prevRightEncoder) > 32000) {
{
dRight = Kobuki_data.EncoderRight - prevRightEncoder + dRight = Kobuki_data.EncoderRight - prevRightEncoder +
(Kobuki_data.EncoderRight > prevRightEncoder ? -65536 : +65536); (Kobuki_data.EncoderRight > prevRightEncoder ? -65536 : +65536);
} } else {
else
{
dRight = Kobuki_data.EncoderRight - prevRightEncoder; dRight = Kobuki_data.EncoderRight - prevRightEncoder;
} }
long double dGyroTheta = prevGyroTheta - gyroToRad(Kobuki_data.GyroAngle); long double dGyroTheta = prevGyroTheta - gyroToRad(Kobuki_data.GyroAngle);
if (dGyroTheta > PI) if (dGyroTheta > PI) {
{
dGyroTheta -= 2 * PI; dGyroTheta -= 2 * PI;
} }
if (dGyroTheta < -1 * PI) if (dGyroTheta < -1 * PI) {
{
dGyroTheta += 2 * PI; dGyroTheta += 2 * PI;
} }
@@ -565,12 +526,9 @@ long CKobuki::loop(void *user_data, TKobukiData &Kobuki_data)
long double mLeft = dLeft * tickToMeter; long double mLeft = dLeft * tickToMeter;
long double mRight = dRight * tickToMeter; long double mRight = dRight * tickToMeter;
if (mLeft == mRight) if (mLeft == mRight) {
{
x = x + mRight; x = x + mRight;
} } else {
else
{
x = x + (b * (mRight + mLeft)) / (2 * (mRight - mLeft)) * x = x + (b * (mRight + mLeft)) / (2 * (mRight - mLeft)) *
(sin((mRight - mLeft) / b + theta) - sin(theta)); (sin((mRight - mLeft) / b + theta) - sin(theta));
y = y + (b * (mRight + mLeft)) / (2 * (mRight - mLeft)) * y = y + (b * (mRight + mLeft)) / (2 * (mRight - mLeft)) *
@@ -619,14 +577,12 @@ long CKobuki::loop(void *user_data, TKobukiData &Kobuki_data)
return 0; return 0;
} }
// tells the kobuki to go a few meters forward or backward, the sign decides // tells the kobuki to go a few meters forward or backward, the sign decides
// the function compensates for walking straight with the controller, internally it uses setArcSpeed and // the function compensates for walking straight with the controller, internally
// uses encoder data as feedback // it uses setArcSpeed and uses encoder data as feedback
void CKobuki::goStraight(long double distance){ void CKobuki::goStraight(long double distance) {
long double u_translation = 0; // controlled magnitude, speed of the robot in motion long double u_translation =
0; // controlled magnitude, speed of the robot in motion
long double w_translation = distance; // requested value long double w_translation = distance; // requested value
// controller parameters // controller parameters
@@ -646,9 +602,8 @@ void CKobuki::goStraight(long double distance){
theta = 0; theta = 0;
long i = 5; long i = 5;
//send command and hold until robot reaches point // send command and hold until robot reaches point
while (fabs(x - w_translation) > 0.005 && x < w_translation) while (fabs(x - w_translation) > 0.005 && x < w_translation) {
{
e_translation = w_translation - x; e_translation = w_translation - x;
u_translation = Kp_translation * e_translation; u_translation = Kp_translation * e_translation;
@@ -663,21 +618,18 @@ void CKobuki::goStraight(long double distance){
u_translation = lower_thresh_translation; u_translation = lower_thresh_translation;
// rewrite starting speed with line // rewrite starting speed with line
if (i < u_translation) if (i < u_translation) {
{
u_translation = i; u_translation = i;
} }
if (fabs(u_rotation) > 32767) if (fabs(u_rotation) > 32767) {
{
u_rotation = -32767; u_rotation = -32767;
} }
if (u_rotation == 0) if (u_rotation == 0) {
{
u_rotation = -32767; u_rotation = -32767;
} }
//send command to robot // send command to robot
this->setArcSpeed(u_translation, u_rotation); this->setArcSpeed(u_translation, u_rotation);
// increment starting speed // increment starting speed
@@ -688,9 +640,9 @@ void CKobuki::goStraight(long double distance){
/// the method performs the rotation, it rotates using the regulator, the /// the method performs the rotation, it rotates using the regulator, the
/// gyroscope serves as feedback, because it is much more accurate than encoders /// gyroscope serves as feedback, because it is much more accurate than encoders
void CKobuki::doRotation(long double th) void CKobuki::doRotation(long double th) {
{ long double u =
long double u = 0; // controlled variable, angular speed of the robot during movement 0; // controlled variable, angular speed of the robot during movement
long double w = th; // desired value in radians long double w = th; // desired value in radians
long double Kp = PI; long double Kp = PI;
long double e = 0; long double e = 0;
@@ -703,20 +655,18 @@ void CKobuki::doRotation(long double th)
long double i = 0; long double i = 0;
if (w > 0) if (w > 0) {
{ while (gyroTheta < w) {
while (gyroTheta < w)
{
e = w - gyroTheta; e = w - gyroTheta;
u = Kp * e; u = Kp * e;
if (u > thresh) if (u > thresh) {
u = thresh; u = thresh;
if (u < 0.4) }
if (u < 0.4) {
u = 0.4; u = 0.4;
}
if (i < u) if (i < u) {
{
u = i; u = i;
} }
@@ -725,11 +675,8 @@ void CKobuki::doRotation(long double th)
usleep(25 * 1000); usleep(25 * 1000);
i = i + 0.1; i = i + 0.1;
} }
} } else {
else while (gyroTheta > w) {
{
while (gyroTheta > w)
{
e = w - gyroTheta; e = w - gyroTheta;
u = Kp * e * -1; u = Kp * e * -1;
@@ -738,8 +685,7 @@ void CKobuki::doRotation(long double th)
if (u < 0.4) if (u < 0.4)
u = 0.4; u = 0.4;
if (i < u) if (i < u) {
{
u = i; u = i;
} }
@@ -752,13 +698,12 @@ void CKobuki::doRotation(long double th)
std::cout << "Rotation done" << std::endl; std::cout << "Rotation done" << std::endl;
this->setRotationSpeed(0); this->setRotationSpeed(0);
usleep(25*1000); usleep(25 * 1000);
} }
// combines navigation to a coordinate and rotation by an angle, performs movement to // combines navigation to a coordinate and rotation by an angle, performs
// the selected coordinate in the robot's coordinate system // movement to the selected coordinate in the robot's coordinate system
void CKobuki::goToXy(long double xx, long double yy) void CKobuki::goToXy(long double xx, long double yy) {
{
long double th; long double th;
yy = yy * -1; yy = yy * -1;
@@ -789,7 +734,7 @@ void CKobuki::forward(int speedvalue, long double distance) {
int actual_speed = speedvalue; int actual_speed = speedvalue;
int actual_radius = 0; // Pure translation (straight line) int actual_radius = 0; // Pure translation (straight line)
unsigned char message[11] = { unsigned char message[11] = {
0xaa, // Start byte 1 0xaa, // Start byte 1
0x55, // Start byte 2 0x55, // Start byte 2
0x08, // Payload length (the first 2 bytes dont count) 0x08, // Payload length (the first 2 bytes dont count)
@@ -800,10 +745,10 @@ unsigned char message[11] = {
0x00, // Placeholder for radius 0x00, // Placeholder for radius
0x00, // Placeholder for radius 0x00, // Placeholder for radius
0x00 // Placeholder for checksum 0x00 // Placeholder for checksum
}; };
// Calculate checksum // Calculate checksum
message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^ message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^
message[7] ^ message[8] ^ message[9]; message[7] ^ message[8] ^ message[9];
// Send the message // Send the message