Skip to content

Commit 5c22143

Browse files
authored
Merge pull request #44 from Team-Micras/feature/maze-algorithms
Feature/maze algorithms
2 parents 4ec774f + 2a08dc4 commit 5c22143

File tree

29 files changed

+734
-352
lines changed

29 files changed

+734
-352
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ cube/*
5959
.ionide
6060

6161
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,c++
62+
63+
### Clangd ###
64+
.clangd

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
"clangd.arguments": [
2121
"--pretty",
22-
"--clang-tidy",
2322
"--compile-commands-dir=${workspaceFolder}/build/",
24-
"--enable-config",
25-
"--query-driver=/usr/bin/arm-none-eabi-g++"
2623
]
2724
}

config/constants.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define MICRAS_CONSTANTS_HPP
77

88
#include <cstdint>
9-
#include <numbers>
109

1110
#include "micras/nav/action_queuer.hpp"
1211
#include "micras/nav/follow_wall.hpp"
@@ -28,6 +27,7 @@ constexpr float start_offset{0.04F + wall_thickness / 2.0F};
2827
constexpr float exploration_speed{0.5F};
2928
constexpr float max_linear_acceleration{1.0F};
3029
constexpr float max_angular_acceleration{200.0F};
30+
constexpr float crash_acceleration{20.0F};
3131

3232
constexpr core::WallSensorsIndex wall_sensors_index{
3333
.left_front = 0,
@@ -95,6 +95,7 @@ const nav::Maze::Config maze_config{
9595
{maze_width / 2, (maze_height - 1) / 2},
9696
{(maze_width - 1) / 2, (maze_height - 1) / 2},
9797
}},
98+
.cost_margin = 1.2F,
9899
};
99100

100101
const nav::Odometry::Config odometry_config{

include/micras/micras.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class Micras {
7373
*/
7474
void reset();
7575

76+
/**
77+
* @brief Use the imu to check if the robot crashed.
78+
*
79+
* @return True if the robot crashed, false otherwise.
80+
*/
81+
bool check_crash() const;
82+
7683
/**
7784
* @brief Get the current objective of the robot.
7885
*

include/micras/states/error.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ class ErrorState : public BaseState {
1212
public:
1313
using BaseState::BaseState;
1414

15+
/**
16+
* @brief Execute the entry function of this state.
17+
*/
18+
void on_entry() override {
19+
this->micras.stop();
20+
this->micras.send_event(Interface::Event::ERROR);
21+
}
22+
1523
/**
1624
* @brief Execute this state.
1725
*
1826
* @return The id of the next state.
1927
*/
20-
uint8_t execute() override {
21-
this->micras.send_event(Interface::Event::ERROR);
22-
23-
return this->get_id();
24-
}
28+
uint8_t execute() override { return this->get_id(); }
2529
};
2630
} // namespace micras
2731

include/micras/states/run.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ class RunState : public BaseState {
2626
* @return The id of the next state.
2727
*/
2828
uint8_t execute() override {
29+
if (this->micras.check_crash()) {
30+
return Micras::State::ERROR;
31+
}
32+
2933
if (not this->micras.run()) {
3034
return this->get_id();
3135
}
3236

3337
switch (this->micras.get_objective()) {
3438
case core::Objective::EXPLORE:
3539
this->micras.set_objective(core::Objective::RETURN);
40+
this->micras.save_best_route();
3641
return Micras::State::WAIT_FOR_RUN;
3742

3843
case core::Objective::RETURN:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @file
3+
*/
4+
5+
#ifndef MICRAS_CORE_VECTOR_HPP
6+
#define MICRAS_CORE_VECTOR_HPP
7+
8+
namespace micras::core {
9+
/**
10+
* @brief Type to store a point in 2D space.
11+
*/
12+
struct Vector {
13+
/**
14+
* @brief Calculate the distance to another point.
15+
*
16+
* @param other The other point.
17+
* @return The distance to the other point.
18+
*/
19+
float distance(const Vector& other) const;
20+
21+
/**
22+
* @brief Calculate the distance of the point to the origin.
23+
*
24+
* @return The distance of the point to the origin.
25+
*/
26+
float magnitude() const;
27+
28+
/**
29+
* @brief Calculate the angle between two points.
30+
*
31+
* @param other The other point.
32+
* @return The angle between the two points.
33+
*/
34+
float angle_between(const Vector& other) const;
35+
36+
/**
37+
* @brief Move the point towards another point by a given distance.
38+
*
39+
* @param other The point to move towards.
40+
* @param distance The distance to move.
41+
* @return The point after moving towards the other point.
42+
*/
43+
Vector move_towards(const Vector& other, float distance) const;
44+
45+
/**
46+
* @brief Add a point to another.
47+
*
48+
* @param other The point to add.
49+
* @return The result of the addition.
50+
*/
51+
Vector operator+(const Vector& other) const;
52+
53+
/**
54+
* @brief Subtract a point from another.
55+
*
56+
* @param other The point to subtract.
57+
* @return The result of the subtraction.
58+
*/
59+
Vector operator-(const Vector& other) const;
60+
61+
/**
62+
* @brief Compare two points for equality.
63+
*
64+
* @param other The other point to compare.
65+
* @return True if the points are equal, false otherwise.
66+
*/
67+
bool operator==(const Vector& other) const;
68+
69+
/**
70+
* @brief Calculate the remainder of the division of the point by a value.
71+
*
72+
* @param value The value to divide the point by.
73+
* @return The remainder of the division.
74+
*/
75+
Vector operator%(float value) const;
76+
77+
/**
78+
* @brief The x coordinate of the point in 2D space.
79+
*/
80+
float x;
81+
82+
/**
83+
* @brief The y coordinate of the point in 2D space.
84+
*/
85+
float y;
86+
};
87+
} // namespace micras::core
88+
89+
#endif // MICRAS_CORE_VECTOR_HPP

micras_core/src/vector.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @file
3+
*/
4+
5+
#include <cmath>
6+
7+
#include "micras/core/vector.hpp"
8+
9+
namespace micras::core {
10+
float Vector::distance(const Vector& other) const {
11+
return std::hypot(other.x - this->x, other.y - this->y);
12+
}
13+
14+
float Vector::magnitude() const {
15+
return std::hypot(this->x, this->y);
16+
}
17+
18+
float Vector::angle_between(const Vector& other) const {
19+
return std::atan2(other.y - this->y, other.x - this->x);
20+
}
21+
22+
Vector Vector::move_towards(const Vector& other, float distance) const {
23+
const float angle = this->angle_between(other);
24+
return {this->x + distance * std::cos(angle), this->y + distance * std::sin(angle)};
25+
}
26+
27+
Vector Vector::operator+(const Vector& other) const {
28+
return {this->x + other.x, this->y + other.y};
29+
}
30+
31+
Vector Vector::operator-(const Vector& other) const {
32+
return {this->x - other.x, this->y - other.y};
33+
}
34+
35+
bool Vector::operator==(const Vector& other) const {
36+
return this->x == other.x and this->y == other.y;
37+
}
38+
39+
Vector Vector::operator%(float value) const {
40+
return {std::fmod(this->x, value), std::fmod(this->y, value)};
41+
}
42+
} // namespace micras::core

micras_hal/include/micras/hal/mcu.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#ifndef MICRAS_HAL_MCU_HPP
66
#define MICRAS_HAL_MCU_HPP
77

8-
#include <cstdint>
9-
108
namespace micras::hal {
119
/**
1210
* @brief Microcontroller unit class.

micras_nav/include/micras/nav/action_queuer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef MICRAS_NAV_ACTION_QUEUER_HPP
66
#define MICRAS_NAV_ACTION_QUEUER_HPP
77

8-
#include <map>
8+
#include <list>
99
#include <memory>
1010
#include <queue>
1111

@@ -82,7 +82,7 @@ class ActionQueuer {
8282
/**
8383
* @brief Fill the action queue with a sequence of actions to the end.
8484
*/
85-
void recompute(const std::map<uint16_t, GridPose, std::greater<>>& best_route);
85+
void recompute(const std::list<GridPose>& best_route);
8686

8787
private:
8888
/**

0 commit comments

Comments
 (0)