Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 3882c8f

Browse files
committed
Calculate position and display it.
1 parent 633f00f commit 3882c8f

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

remote-website/robot-control.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
22
$image = imagecreatetruecolor(1000, 1000);
33
$default_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
44

5+
$data_server = "http://kps32.ddns.net/?positions";
6+
$positionString = file_get_contents($data_server);
7+
8+
59
$positions = array();
6-
for ($i = 0; $i < 10; $i ++) {
7-
$positions[] = rand(0, 1000);
10+
if($positionString === FALSE) {
11+
echo "Could not read the data from ".$data_server.".";
12+
} else {
13+
$positions = explode(";", $positionString);
814
}
915

1016
$diameter = 10;
11-
$start_x = 0;
12-
$start_y = 0;
17+
$offset_x = 500;
18+
$offset_y = 500;
19+
$start_x = $offset_x;
20+
$start_y = $offset_y;
1321

14-
for ($i = 0; $i < count($positions); $i += 2) {
15-
$x = $positions[$i];
16-
$y = $positions[$i + 1];
22+
for ($i = 0; $i+1 < count($positions); $i += 2) {
23+
$x = ((int) $positions[$i])/10+$offset_x;
24+
$y = ((int) $positions[$i + 1])/10+$offset_y;
1725

1826
imagefilledellipse($image, $x, $y, $diameter, $diameter, $default_color);
19-
imagestring($image, 3, $x + $diameter / 2, $y + $diameter / 2, sprintf("%u;%u", $x, $y), $default_color);
27+
imagestring($image, 3, $x + $diameter / 2, $y + $diameter / 2, sprintf("%u", $i/2), $default_color);
2028
imageline($image, $start_x, $start_y, $x, $y, $default_color);
2129

2230
$start_x = $x;

robot-control-src/Drives.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
namespace drives
99
{
1010

11+
static constexpr float stepsPerDeg = 100/(4.8*360);
12+
static constexpr float stepsPerRad = stepsPerDeg / ((2*pi)/360);
13+
14+
static enum class Action
15+
{
16+
STRAIGHTFORWARD, TURN_LEFT, TURN_RIGHT
17+
} lastAction;
18+
19+
Position lastKnownPosition = {0,0};
20+
static float orientation = 0; // in radians
21+
1122
template<typename MOTORCONTROL, MOTORCONTROL &motorControlpin, typename DIRECTIONPIN, DIRECTIONPIN &directionPin, typename ODOPIN, ODOPIN &odoPin>
1223
Counter volatile Drive<MOTORCONTROL, motorControlpin, DIRECTIONPIN, directionPin, ODOPIN, odoPin>::counter = 0;
1324
template<typename MOTORCONTROL, MOTORCONTROL &motorControlpin, typename DIRECTIONPIN, DIRECTIONPIN &directionPin, typename ODOPIN, ODOPIN &odoPin>
@@ -49,19 +60,20 @@ static Amplitude calcRightSpeed(const Amplitude leftSpeed)
4960

5061
void rotateCounter(const Counter steps, const Amplitude amplitude, bool const clockwise)
5162
{
63+
lastAction = clockwise ? Action::TURN_RIGHT : Action::TURN_LEFT;
5264
LeftDrive::drive(steps, amplitude, !clockwise);
5365
RightDrive::drive(steps, calcRightSpeed(amplitude), clockwise);
5466
}
5567

5668
void driveCounter(const Counter steps, const Amplitude amplitude, const bool backwards)
5769
{
70+
lastAction = Action::STRAIGHTFORWARD;
5871
LeftDrive::drive(steps, amplitude, backwards);
5972
RightDrive::drive(steps, calcRightSpeed(amplitude), backwards);
6073
}
6174

6275
void rotate(const float deg, const Amplitude amplitude, bool const clockwise)
6376
{
64-
constexpr float stepsPerDeg = 100/(4.8*360);
6577
const Counter steps = std::round(deg * stepsPerDeg);
6678
rotateCounter(steps, amplitude, clockwise);
6779
}
@@ -73,7 +85,35 @@ void drive(const float distance, const Amplitude amplitude, const bool backwards
7385
driveCounter(steps, amplitude, backwards);
7486
}
7587

76-
IRAM_ATTR void stopDrives() {
88+
Position flushCurrentPosition()
89+
{
90+
switch (lastAction) {
91+
case Action::STRAIGHTFORWARD:
92+
{
93+
lastKnownPosition.x += LeftDrive::counter * board::odoIntervalLength * std::cos(orientation);
94+
lastKnownPosition.y += LeftDrive::counter * board::odoIntervalLength * std::sin(orientation);
95+
break;
96+
}
97+
case Action::TURN_LEFT:
98+
{
99+
orientation -= LeftDrive::counter / stepsPerRad;
100+
break;
101+
}
102+
case Action::TURN_RIGHT:
103+
{
104+
orientation += LeftDrive::counter / stepsPerRad;
105+
break;
106+
}
107+
default:
108+
break;
109+
}
110+
LeftDrive::counter = 0;
111+
RightDrive::counter = 0;
112+
return lastKnownPosition;
113+
}
114+
115+
IRAM_ATTR void stopDrives()
116+
{
77117
LeftDrive::stop();
78118
RightDrive::stop();
79119
}

robot-control-src/Drives.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <cstdint>
77
#include <Arduino.h>
88

9+
using Distance = std::int16_t; //!< in [mm]
10+
struct Position
11+
{
12+
Distance x, y;
13+
};
14+
915
namespace drives
1016
{
1117

@@ -73,6 +79,8 @@ void rotate(const float deg, const Amplitude amplitude, bool const clockwise);
7379
void driveCounter(const Counter distance, const Amplitude amplitude, const bool backwards);
7480
void drive(const float distance, const Amplitude amplitude, const bool backwards);
7581

82+
Position flushCurrentPosition();
83+
7684
void stopDrives();
7785

7886
};

robot-control-src/robot-control-src.ino

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "board.hpp"
22
#include "Drives.hpp"
33
#include "wifi_ap.hpp"
4+
#include <assert.h>
45
#include <ESP8266WebServer.h>
56

67
static ESP8266WebServer server(80);
@@ -14,6 +15,10 @@ static struct
1415
bool clockwise = true;
1516
} newTarget;
1617

18+
static constexpr std::size_t numberOfPositions = 50;
19+
static Position positions[numberOfPositions] = { {0,0} };
20+
static std::size_t positionIndex = 0;
21+
1722
constexpr auto htmlSource =
1823
"<!DOCTYPE html>\n"
1924
"<html lang=\"en\">\n"
@@ -68,7 +73,24 @@ static void handleRoot()
6873
newTarget.isTargetNew = true;
6974
Serial.printf("Got right by %u!\n", newTarget.newRotate);
7075
}
71-
server.send(200, "text/html", htmlSource);
76+
if(server.hasArg("positions"))
77+
{
78+
constexpr std::size_t charPerEntry = (5+1)*2;
79+
constexpr std::size_t bufferLength = charPerEntry*numberOfPositions+1;
80+
char positionStringBuffer[bufferLength] = { 0 };
81+
std::size_t charPos = 0;
82+
for(std::size_t i=0; i<positionIndex; i++)
83+
{
84+
const int writtenCharacters = snprintf(&(positionStringBuffer[charPos]), charPerEntry+1, "%i;%i;", positions[i].x, positions[i].y);
85+
assert(writtenCharacters>0);
86+
charPos += writtenCharacters;
87+
}
88+
server.send(200, "text/plain", positionStringBuffer);
89+
}
90+
else
91+
{
92+
server.send(200, "text/html", htmlSource);
93+
}
7294
digitalWrite(board::debugLed, HIGH);
7395
}
7496

@@ -117,6 +139,8 @@ void loop()
117139
//Serial.printf("left: \t%3u, right: \t%3u\n", drives::LeftDrive::counter, drives::RightDrive::counter);
118140
if(drives::LeftDrive::isIdle && drives::RightDrive::isIdle && newTarget.isTargetNew)
119141
{
142+
positions[positionIndex++] = drives::flushCurrentPosition();
143+
positionIndex %= numberOfPositions;
120144
if(newTarget.newDrive!=0)
121145
{
122146
drives::driveCounter(newTarget.newDrive, drives::cruiseSpeed, !newTarget.forward);

0 commit comments

Comments
 (0)