Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Landed buzzer #189

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
3 changes: 2 additions & 1 deletion avionics/common/include/buzzer_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
enum SongTypes {
SongTypes_SUCCESS,
SongTypes_NONCRITFAIL,
SongTypes_CRITICALFAIL
SongTypes_CRITICALFAIL,
SongTypes_LANDED,
};

/*Classes--------------------------------------------------------------*/
Expand Down
5 changes: 5 additions & 0 deletions avionics/common/include/states/landed.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "cameras.h"
#include "scheduler.hpp"
#include "state_interface.h"
#include "buzzer.h"

namespace State {

Expand All @@ -28,6 +29,10 @@ class Landed : public IState {
* codes and used states. Note that the returned code may be the same state.
*/
StateId getNewState(Calculator const &) { return StateId::LANDED; }

void onEntry() override {
Scheduler::scheduleTask(Hal::now_ms() + Hal::ms(1), static_cast<int>(TaskID::BuzzerBeacon));
}
};

} // namespace State
28 changes: 28 additions & 0 deletions avionics/common/include/tasks/landed_buzzer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Landed Buzzer
*
* @file landed_buzzer.hpp
* @author UBC Rocket Avionics 2023/2024
* @description Class that allows landed buzzer to be run within a task
*/

#pragma once

/*Includes------------------------------------------------------------*/
#include "HAL/time.h"
#include "buzzer.h"

class LandedBuzzer {

private:
Buzzer buzzer_;

public:
LandedBuzzer(Buzzer &buzzer) : buzzer_(buzzer) {}

// run function to be called by a task
static void run(void *self) {
reinterpret_cast<LandedBuzzer *>(self)->buzzer_.sing(SongTypes_LANDED);
}
static constexpr Hal::ms freq{500};
};
7 changes: 7 additions & 0 deletions avionics/common/src/avionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "tasks/led_blinker.hpp"
#include "tasks/main_tasks.hpp"
#include "tasks/restart_camera.hpp"
#include "tasks/landed_buzzer.hpp"

#include "radio.h"
#include "rocket.h"
Expand Down Expand Up @@ -83,6 +84,9 @@ int main(void) {
auto &sensors = rocket.sensors;
auto &ignitors = rocket.ignitors;

// Create instance of landed buzzer
LandedBuzzer landedBuzzer(rocket.buzzer);

if (init_status == RocketStatus::CRITICAL_FAILURE) {
LOG_ERROR("Critical failure; aborting in state machine");
state_machine.abort();
Expand Down Expand Up @@ -112,6 +116,9 @@ int main(void) {
if (init_status == RocketStatus::NONCRITICAL_FAILURE) {
registerTask(TaskID::LEDBlinker, led_blink);
}

Task buzzer(LandedBuzzer::run, &landedBuzzer, Hal::ms(45000));
Scheduler::preregisterTask(static_cast<int>(TaskID::BuzzerBeacon), buzzer, true, false);

// RestartCamera restart_camera_(rocket.cam);
// // This tasks sets its own reschedule interval (since the same task is run
Expand Down
63 changes: 59 additions & 4 deletions avionics/envs/board/src/buzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,66 @@ void Buzzer::sing(SongTypes song) const {
buzz(NOTE_A6, 2500);
break;
}
case SongTypes_LANDED: {
// Play the 2001: A Space Odyssey theme
int tempo = 2;
buzz(NOTE_C5, 2000 / tempo);
buzz(NOTE_G5, 2000 / tempo);
buzz(NOTE_C6, 3500 / tempo);
buzz(NOTE_E6, 250 / tempo);
buzz(NOTE_DS6, 4000 / tempo);

buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);

buzz(NOTE_C5, 2000 / tempo);
buzz(NOTE_G5, 2000 / tempo);
buzz(NOTE_C6, 3500 / tempo);
buzz(NOTE_DS6, 250 / tempo);
buzz(NOTE_E6, 4000 / tempo);

buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);
buzz(NOTE_C4, 500 / tempo);
buzz(NOTE_G3, 500 / tempo);

buzz(NOTE_C5, 2000 / tempo);
buzz(NOTE_G5, 2000 / tempo);
buzz(NOTE_C6, 3500 / tempo);
buzz(NOTE_E6, 250 / tempo);
buzz(NOTE_A6, 4000 / tempo);

buzz(NOTE_A5, 250 / tempo);
buzz(NOTE_B5, 250 / tempo);
buzz(NOTE_C6, 2500 / tempo);
buzz(NOTE_D6, 1000 / tempo);

buzz(NOTE_E6, 500 / tempo);
buzz(NOTE_F6, 500 / tempo);
buzz(NOTE_G6, 2500 / tempo);
buzz(NOTE_E6, 250 / tempo);
buzz(NOTE_F6, 250 / tempo);

buzz(NOTE_G6, 2000 / tempo);
buzz(NOTE_A6, 1000 / tempo);
buzz(NOTE_B6, 1000 / tempo);
buzz(NOTE_C7, 8000 / tempo);
break;
}
}
}

void Buzzer::buzz(long frequency, long length) const {

long delayValue = 1000000 / frequency / 2; // delay between transitions
// 1 000 000 microseconds, divided by the frequency, divided by 2 b/c
// there are two phases to each cycle
Expand All @@ -191,11 +246,11 @@ void Buzzer::buzz(long frequency, long length) const {
for (long i = 0; i < numCycles; i++) { // for the calculated length of time
Hal::digitalWrite(
M_MELODY_PIN,
Hal::PinDigital::HI); // write high to push out the diaphram
Hal::sleep_us(delayValue); // wait for the calculated delay value
Hal::PinDigital::HI); // write high to push out the diaphram
Hal::sleep_us(delayValue); // wait for the calculated delay value
Hal::digitalWrite(
M_MELODY_PIN,
Hal::PinDigital::LO); // write low to pull back the diaphram
Hal::PinDigital::LO); // write low to pull back the diaphram
Hal::sleep_us(delayValue); // wait for the calculated delay value
}
}
Expand Down
1 change: 1 addition & 0 deletions avionics/rockets/bnb_stage_1/include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class TaskID {
RadioTxBulk = 1,
LEDBlinker = 2,
RestartCamera = 3,
BuzzerBeacon = 4,
kNum,
};

Expand Down
1 change: 1 addition & 0 deletions avionics/rockets/bnb_stage_2/include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class TaskID {
RadioTxBulk = 1,
LEDBlinker = 2,
RestartCamera = 3,
BuzzerBeacon = 4,
kNum,
};

Expand Down
1 change: 1 addition & 0 deletions avionics/rockets/hollyburn/include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class TaskID {
RadioTxBulk = 1,
LEDBlinker = 2,
RestartCamera = 3,
BuzzerBeacon = 4,
kNum,
};

Expand Down
1 change: 1 addition & 0 deletions avionics/rockets/silvertip/include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class TaskID {
RadioTxBulk = 1,
LEDBlinker = 2,
RestartCamera = 3,
BuzzerBeacon = 4,
kNum,
};

Expand Down
1 change: 1 addition & 0 deletions avionics/tests/fake_include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum class TaskID {
RadioTxBulk = 1,
LEDBlinker = 2,
RestartCamera = 3,
BuzzerBeacon = 4,
kNum,
};

Expand Down