Skip to content

Commit 38f3c4b

Browse files
authored
Merge pull request #13 from rogy-AquaLab/timeout-suspend
2 parents bc55b88 + 7bf4d2e commit 38f3c4b

File tree

5 files changed

+61
-98
lines changed

5 files changed

+61
-98
lines changed

Diff for: include/umiusi/defered_delay.hpp

-14
This file was deleted.

Diff for: include/umiusi/outputs.hpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <DigitalOut.h>
88
#include <Mutex.h>
99
#include <PwmOut.h>
10+
#include <events/EventQueue.h>
1011

1112
#include "umiusi/state.hpp"
1213

@@ -81,11 +82,7 @@ class Outputs {
8182
Outputs();
8283
void activate();
8384
void deactivate();
84-
/// BLDC(に繋がっているESC)を起動する。完了までに2秒を要する。
85-
/// 完了時点でbldcのパルス幅は各100usとなる
86-
void wake_up();
87-
/// activate->wake_up
88-
void setup();
85+
void prepare_wake_up();
8986
void set_powers(
9087
const std::array<std::pair<uint16_t, uint16_t>, THRUSTER_NUM>& pulsewidths_us
9188
);
@@ -107,7 +104,7 @@ class OutputMachine {
107104
const std::array<std::pair<uint16_t, uint16_t>, THRUSTER_NUM>& pulsewidths_us
108105
);
109106
void suspend();
110-
void initialize();
107+
void initialize_with_equeue(events::EventQueue& equeue);
111108
};
112109

113110
#endif

Diff for: src/main.cpp

+48-54
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,40 @@
11
#include <array>
2+
#include <atomic>
23
#include <cstdint>
34
#include <mutex>
45

56
#include "BufferedSerial.h"
6-
#include "EventFlags.h"
7-
#include "ThisThread.h"
8-
#include "Thread.h"
7+
#include "events/EventQueue.h"
8+
#include "events/Event.h"
99

10-
#include "umiusi/defered_delay.hpp"
1110
#include "umiusi/inputs.hpp"
1211
#include "umiusi/outputs.hpp"
1312

1413
using namespace std::chrono_literals;
1514

1615
int main() {
17-
constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024;
18-
constexpr size_t SETUP_THREAD_STACK_SIZE = 512;
16+
constexpr std::size_t EQUEUE_BUFFER_SIZE = 32 * EVENTS_EVENT_SIZE;
17+
18+
unsigned char equeue_buffer[EQUEUE_BUFFER_SIZE] = {};
1919

2020
CachedInputs inputs{};
2121
OutputMachine output{};
2222
mbed::BufferedSerial pc(USBTX, USBRX);
23+
std::atomic_bool received_order;
24+
pc.set_blocking(false);
25+
inputs.read();
2326

24-
rtos::EventFlags trigger_setup{};
25-
unsigned char setup_thread_stack[SETUP_THREAD_STACK_SIZE] = {};
26-
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
27-
28-
rtos::Thread setup_thread(
29-
osPriorityBelowNormal, SETUP_THREAD_STACK_SIZE, setup_thread_stack
30-
);
31-
rtos::Thread inputs_thread(
32-
osPriorityBelowNormal, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
33-
);
34-
35-
// TODO: handle osStatus
36-
setup_thread.start([&output, &trigger_setup]() {
37-
while (true) {
38-
trigger_setup.wait_any(1, osWaitForever, false);
39-
output.initialize();
40-
trigger_setup.clear();
41-
}
42-
});
43-
osStatus inputs_thread_status = inputs_thread.start([&inputs]() {
44-
while (true) {
45-
inputs.read();
46-
rtos::ThisThread::sleep_for(10ms);
27+
events::EventQueue equeue(EQUEUE_BUFFER_SIZE, equeue_buffer);
28+
auto initialize_event = equeue.event(mbed::callback([&output, &equeue]() {
29+
if (output.state() != State::INITIALIZING) {
30+
output.initialize_with_equeue(equeue);
4731
}
48-
});
49-
if (inputs_thread_status != osOK) {
50-
// 本来ここに入ることはあってはならないが、一応書いておく
51-
// スレッドを開始することができなかったので、入力が読み取られなくなる
52-
// そのため、一度だけ値を読んでおくことにする
53-
// 得られる値を見て実行状況を判断すること
54-
inputs.read();
55-
}
56-
while (true) {
57-
DeferedDelay _delay(10ms);
58-
pc.sync();
59-
uint8_t header = 0;
60-
// TODO: timeout
61-
ssize_t read = pc.read(&header, 1);
62-
if (read < 1) {
63-
continue;
64-
}
65-
// なぜかこれがないと動かない
66-
rtos::ThisThread::sleep_for(20ms);
32+
}));
33+
auto suspend_event = equeue.event(mbed::callback([&output]() {
34+
output.suspend();
35+
}));
36+
37+
const auto process_order = [&pc, &inputs, &output, &initialize_event, &suspend_event](std::uint8_t header) {
6738
switch (header) {
6839
case 0: {
6940
// write
@@ -74,7 +45,7 @@ int main() {
7445
// bldc
7546
uint16_t pulsewidth_us_lsb = static_cast<uint16_t>(buffer[i * 2 + 0]);
7647
uint16_t pulsewidth_us_msb = static_cast<uint16_t>(buffer[i * 2 + 1]);
77-
pulsewidths_us[i].first = (pulsewidth_us_lsb << 0)
48+
pulsewidths_us[i].first = (pulsewidth_us_lsb << 0)
7849
| (pulsewidth_us_msb << 8);
7950
// servo
8051
pulsewidth_us_lsb
@@ -99,18 +70,41 @@ int main() {
9970
case 0xFE: {
10071
// (re)start
10172
if (output.state() == State::INITIALIZING) {
102-
continue;
73+
return;
10374
}
104-
trigger_setup.set(1);
75+
initialize_event.call();
10576
} break;
10677
case 0xFF:
10778
// suspend
108-
output.suspend();
79+
suspend_event.call();
10980
break;
11081
default:
11182
// unexpected
112-
continue;
83+
return;
11384
}
114-
}
85+
};
86+
equeue.call_every(10ms, [&equeue, &pc, &received_order, &process_order]() {
87+
std::uint8_t header = 0;
88+
ssize_t res = pc.read(&header, 1);
89+
if (res < 1) {
90+
return;
91+
}
92+
received_order.store(true, std::memory_order_release);
93+
equeue.call(process_order, header);
94+
});
95+
96+
equeue.call_every(10ms, [&inputs]() {
97+
inputs.read();
98+
});
99+
100+
equeue.call_every(1s, [&received_order, &suspend_event]() {
101+
const bool received = received_order.load(std::memory_order_acquire);
102+
received_order.store(false, std::memory_order_release);
103+
if (!received) {
104+
suspend_event.call();
105+
}
106+
});
107+
108+
equeue.dispatch_forever();
115109
return 0;
116110
}

Diff for: src/umiusi/defered_delay.cpp

-9
This file was deleted.

Diff for: src/umiusi/outputs.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "umiusi/outputs.hpp"
2-
#include "umiusi/defered_delay.hpp"
32

43
using namespace std::chrono_literals;
54

@@ -29,19 +28,12 @@ void Outputs::deactivate() {
2928
this->init_status.write(0);
3029
}
3130

32-
/// ESC の起動待ち
33-
void Outputs::wake_up() {
34-
DeferedDelay _(2s);
31+
void Outputs::prepare_wake_up(){
3532
for (mbed::PwmOut& bldc : this->bldcs) {
3633
bldc.pulsewidth_us(100);
3734
}
3835
}
3936

40-
void Outputs::setup() {
41-
this->activate();
42-
this->wake_up();
43-
}
44-
4537
void Outputs::set_powers(
4638
const std::array<std::pair<uint16_t, uint16_t>, THRUSTER_NUM>& pulsewidths_us
4739
) {
@@ -85,15 +77,18 @@ void OutputMachine::suspend() {
8577
this->outputs.deactivate();
8678
}
8779

88-
void OutputMachine::initialize() {
80+
void OutputMachine::initialize_with_equeue(events::EventQueue& equeue){
8981
if (this->state() == State::INITIALIZING) {
9082
return;
9183
}
9284
this->set_state(State::INITIALIZING);
9385
this->outputs.reset();
94-
this->outputs.setup();
95-
if (this->state() == State::INITIALIZING) {
96-
// setup前後で値が変化する可能性がある
97-
this->set_state(State::RUNNING);
98-
}
86+
this->outputs.activate();
87+
this->outputs.prepare_wake_up();
88+
equeue.call_in(2s, [this]() {
89+
if (this->state() == State::INITIALIZING) {
90+
this->set_state(State::RUNNING);
91+
}
92+
});
9993
}
94+

0 commit comments

Comments
 (0)