Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
url = https://github.com/ryanplusplus/stm8s-header
[submodule "lib/stm8-tiny"]
path = lib/stm8-tiny
url = https://github/com/ryanplusplus/stm8-tiny
url = https://github.com/ryanplusplus/stm8-tiny
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"name": "Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/zumo_tests",
"program": "${workspaceFolder}/build/test/zumo_tests",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand All @@ -44,4 +44,4 @@
"preLaunchTask": "debug-deps-test"
}
]
}
}
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"editor.tabSize": 2,
"editor.insertSpaces": true
},
"editor.formatOnSave": true
"editor.formatOnSave": true,
"files.associations": {
"tuning.h": "c",
"tiny_utils.h": "c",
"cstdio": "c"
}
}
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{
"label": "debug-deps-test",
"type": "shell",
"command": "make -j8 -f test.mk debug-deps",
"command": "",
"presentation": {
"echo": true,
"reveal": "silent",
Expand All @@ -30,4 +30,4 @@
}
}
]
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ make -f test.mk

## Pinout
| STM8 Pin | Zumo Pin | Peripheral | Function |
|----------|----------|------------|------------------------|
| -------- | -------- | ---------- | ---------------------- |
| PA3 | N/A | GPIO | Heartbeat LED |
| PE1 | SCL | I2C SCL | I2C SCL |
| PE2 | SDA | I2C SDA | I2C SDA |
Expand All @@ -58,6 +58,7 @@ make -f test.mk
| PB3 | ? | AN2 | Left distance sensor |
| N/A | N/A | AN4 | Center distance sensor |
| PB2 | ? | AN3 | Right distance sensor |
| PC7 | 12 | GPIO | Configuration pin |

## Resources
- [STM8S/STM8AF Reference Manual](https://www.st.com/resource/en/reference_manual/cd00190271.pdf)
Expand Down
2 changes: 1 addition & 1 deletion src/application/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void application_init(application_t* self, tiny_timer_group_t* timer_group) {

motors_plugin_init(&self->motors_plugin, store);
line_sensors_plugin_init(&self->line_sensors_plugin, store, timer_group);
distance_sensors_plugin_init(&self->distance_sensors_plugin, store, timer_group);
// distance_sensors_plugin_init(&self->distance_sensors_plugin, store, timer_group);
accelerometer_plugin_init(&self->accelerometer_plugin, store, timer_group);
buzzer_plugin_init(&self->buzzer_plugin, store);
strategy_plugin_init(&self->strategy_plugin, store, timer_group);
Expand Down
2 changes: 2 additions & 0 deletions src/application/data_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "direction.h"
#include "distance_in_cm.h"
#include "buzzer_frequency.h"
#include "i_tiny_adc.h"

// clang-format off
#define data_model_key_value_pairs(pair) \
Expand All @@ -37,6 +38,7 @@
pair(key_center_sensor_distance, distance_in_cm_t) \
pair(key_right_sensor_distance, distance_in_cm_t) \
pair(key_buzzer_frequency, buzzer_frequency_t) \
pair(key_knob_counts, tiny_adc_counts_t) \
// clang-format on

enumerate_ram_key_value_pairs(data_model_key_value_pairs);
Expand Down
1 change: 1 addition & 0 deletions src/application/meta-sensing/tactic.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

enum {
tactic_idle,
tactic_init,
tactic_seek_clockwise,
tactic_seek_counterclockwise,
tactic_charge,
Expand Down
23 changes: 15 additions & 8 deletions src/application/plugin/strategy_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "strategy_plugin.h"
#include "data_model.h"
#include "tuning.h"

static const strategist_keys_t strategist_keys = {
.key_tactic = key_current_tactic,
Expand All @@ -24,13 +25,6 @@ static const line_detected_keys_t line_detected_keys = {
.key_tactic_stopped = key_tactic_stopped
};

static const line_detected_config_t line_detected_config = {
.near_wheel_power = 30,
.far_wheel_power = 100,
.back_up_time = 200,
.turn_time = 300
};

static const charge_keys_t charge_keys = {
.left_motor = key_left_motor,
.right_motor = key_right_motor,
Expand All @@ -44,14 +38,27 @@ static const seeking_keys_t seeking_keys = {
.tactic = key_current_tactic
};

static const spin_on_init_keys_t spin_on_init_keys = {
.left_motor = key_left_motor,
.right_motor = key_right_motor,
.knob_counts = key_knob_counts,
.tactic = key_current_tactic,
.tactic_stopped = key_tactic_stopped
};

void strategy_plugin_init(strategy_plugin_t* self, i_tiny_key_value_store_t* key_value_store, tiny_timer_group_t* timer_group) {
strategist_init(&self->strategist, key_value_store, &strategist_keys);

spin_on_init_init(
&self->spin_on_init,
key_value_store,
&spin_on_init_keys,
timer_group);

line_detected_init(
&self->line_detected,
key_value_store,
&line_detected_keys,
&line_detected_config,
timer_group);

charge_init(&self->charge, key_value_store, &charge_keys);
Expand Down
2 changes: 2 additions & 0 deletions src/application/plugin/strategy_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
#include "line_detected.h"
#include "charge.h"
#include "seeking.h"
#include "spin_on_init.h"

typedef struct {
strategist_t strategist;
line_detected_t line_detected;
charge_t charge;
seeking_t seeking;
spin_on_init_t spin_on_init;
} strategy_plugin_t;

void strategy_plugin_init(
Expand Down
7 changes: 3 additions & 4 deletions src/application/tactics/charge.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include "enemy_location.h"
#include "tactic.h"
#include "tiny_utils.h"
#include "tuning.h"
#include "motor_power.h"

enum {
near_wheel_power = 30,
far_wheel_power = 100,
reduced_charge = 80,
charge = 100,
reduced_charge = charge_reduced_power,
charge = charge_full_power,
toward_the_left = enemy_location_front_left,
toward_the_right = enemy_location_front_right
};
Expand Down
18 changes: 9 additions & 9 deletions src/application/tactics/line_detected.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "line_detected.h"
#include "tactic.h"
#include "tiny_utils.h"
#include "tuning.h"

static void stop_running_timer_expired(tiny_timer_group_t* group, void* context) {
reinterpret(self, context, line_detected_t*);
Expand Down Expand Up @@ -39,13 +40,13 @@ static void back_up_timer_expired(tiny_timer_group_t* group, void* context) {
motor_power_t left_power;
motor_power_t right_power;

if(the_left_line_was_detected(self)) {
left_power = 100;
right_power = -30;
if(self->left_line_was_detected) {
left_power = line_detection_near_wheel_power;
right_power = line_detection_far_wheel_power;
}
else {
left_power = -30;
right_power = 100;
left_power = line_detection_far_wheel_power;
right_power = line_detection_near_wheel_power;
}

tiny_key_value_store_write(
Expand All @@ -61,7 +62,7 @@ static void back_up_timer_expired(tiny_timer_group_t* group, void* context) {
tiny_timer_start(
self->timer_group,
&self->stop_running_timer,
self->config->turn_time,
line_detection_turn_time,
stop_running_timer_expired,
self);
}
Expand All @@ -75,6 +76,7 @@ static void data_changed(void* context, const void* _args) {

if(*tactic == tactic_avoid_line) {
motor_power_t power = motor_power_min;
self->left_line_was_detected = the_left_line_was_detected(self);
tiny_key_value_store_write(
self->key_value_store,
self->keys->key_left_motor,
Expand All @@ -87,7 +89,7 @@ static void data_changed(void* context, const void* _args) {
tiny_timer_start(
self->timer_group,
&self->back_up_timer,
self->config->back_up_time,
line_detection_back_up_time,
back_up_timer_expired,
self);
}
Expand All @@ -98,12 +100,10 @@ void line_detected_init(
line_detected_t* self,
i_tiny_key_value_store_t* key_value_store,
const line_detected_keys_t* keys,
const line_detected_config_t* config,
tiny_timer_group_t* timer_group) {
self->key_value_store = key_value_store;
self->keys = keys;
self->timer_group = timer_group;
self->config = config;

tiny_event_subscription_init(&self->on_change_subscription, self, data_changed);
tiny_event_subscribe(tiny_key_value_store_on_change(key_value_store), &self->on_change_subscription);
Expand Down
10 changes: 1 addition & 9 deletions src/application/tactics/line_detected.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,20 @@ typedef struct {
tiny_key_value_store_key_t key_tactic_stopped;
} line_detected_keys_t;

typedef struct {
motor_power_t near_wheel_power;
motor_power_t far_wheel_power;
tiny_timer_ticks_t back_up_time;
tiny_timer_ticks_t turn_time;
} line_detected_config_t;

typedef struct {
tiny_event_subscription_t on_change_subscription;
i_tiny_key_value_store_t* key_value_store;
const line_detected_keys_t* keys;
const line_detected_config_t* config;
tiny_timer_t back_up_timer;
tiny_timer_t stop_running_timer;
tiny_timer_group_t* timer_group;
bool left_line_was_detected;
} line_detected_t;

void line_detected_init(
line_detected_t* self,
i_tiny_key_value_store_t* key_value_store,
const line_detected_keys_t* keys,
const line_detected_config_t* config,
tiny_timer_group_t* timer_group);

#endif
5 changes: 3 additions & 2 deletions src/application/tactics/seeking.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include "seeking.h"
#include "tactic.h"
#include "tiny_utils.h"
#include "tuning.h"
#include "motor_power.h"

enum {
near_wheel_power = 30,
far_wheel_power = 100
near_wheel_power = seeking_near_wheel_power,
far_wheel_power = seeking_far_wheel_power
};

static void set_right_motor_to(seeking_t* self, motor_power_t power) {
Expand Down
93 changes: 93 additions & 0 deletions src/application/tactics/spin_on_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*!
* @file
* @brief
*/

#include "spin_on_init.h"
#include "tactic.h"
#include "tiny_utils.h"
#include "tuning.h"
#include "i_tiny_adc.h"
#include <stdio.h>

enum {
max_counts = 0xFFFF
};

static void stop_running_timer_expired(tiny_timer_group_t* group, void* context) {
reinterpret(self, context, spin_on_init_t*);
(void)group;
uint8_t stopped;
tiny_key_value_store_read(
self->key_value_store,
self->keys->tactic_stopped,
&stopped);

stopped++;

tiny_key_value_store_write(
self->key_value_store,
self->keys->tactic_stopped,
&stopped);
}

static tiny_adc_counts_t potentiometer_counts(spin_on_init_t* self) {
tiny_adc_counts_t counts;
tiny_key_value_store_read(
self->key_value_store,
self->keys->knob_counts,
&counts);
return counts;
}

static void spin(spin_on_init_t* self, bool spin_right) {
motor_power_t left_power = spin_right ? spin_on_init_near_wheel_power : spin_on_init_far_wheel_power;
motor_power_t right_power = spin_right ? spin_on_init_far_wheel_power : spin_on_init_near_wheel_power;
tiny_key_value_store_write(
self->key_value_store,
self->keys->left_motor,
&left_power);
tiny_key_value_store_write(
self->key_value_store,
self->keys->right_motor,
&right_power);
}

// static tiny_timer_ticks_t counts_to_ticks(tiny_adc_counts_t counts) {
// return (((uint64_t)counts) * (uint64_t)turn_time_360_degree) / (uint64_t)max_counts;
// }

static void data_changed(void* context, const void* _args) {
reinterpret(self, context, spin_on_init_t*);
reinterpret(args, _args, const tiny_key_value_store_on_change_args_t*);

if(args->key == self->keys->tactic) {
reinterpret(tactic, args->value, const tactic_t*);

if(*tactic == tactic_init) {
tiny_adc_counts_t counts = potentiometer_counts(self);
bool spin_right = counts < (0xFFFF / 2);
spin(self, spin_right);

tiny_timer_start(
self->timer_group,
&self->back_up_timer,
turn_time_360_degree / 3,
stop_running_timer_expired,
self);
}
}
}

void spin_on_init_init(
spin_on_init_t* self,
i_tiny_key_value_store_t* key_value_store,
const spin_on_init_keys_t* keys,
tiny_timer_group_t* timer_group) {
self->key_value_store = key_value_store;
self->keys = keys;
self->timer_group = timer_group;

tiny_event_subscription_init(&self->on_change_subscription, self, data_changed);
tiny_event_subscribe(tiny_key_value_store_on_change(key_value_store), &self->on_change_subscription);
}
Loading