Skip to content

Commit

Permalink
Add: dac sin wave
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonyang-ee committed Jun 5, 2023
1 parent f86b188 commit 092c48a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
6 changes: 6 additions & 0 deletions Application/Inc/DAC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CustomDAC {
enum State {
s_off,
s_on,
s_breath,
} m_state;

void setLevel(double);
Expand All @@ -26,6 +27,9 @@ class CustomDAC {

void on();
void off();
void breath();

void schedule();

private:
void applyLevel();
Expand All @@ -35,6 +39,8 @@ class CustomDAC {
double m_level{0.0};
double m_vref{3.3};

uint32_t m_breeath_period{325};

/**
* @brief Using HAL predefined bit mask for DAC output options
* Options: DAC_CHANNEL_1, DAC_CHANNEL_2
Expand Down
14 changes: 11 additions & 3 deletions Application/Src/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,24 @@ int32_t CLI::cmd_motor(int32_t argc, char** argv) {
// Detailed Menu
const char* help_text =
"\nDAC Functions:\n"
" breath \tBreathing DAC\n"
" level #value\tSet DAC level\n"
" add #value\tIncrease or Decrease DAC level\n\n";

// Sub Command
if (argc == 2) {
if (!strcmp(argv[1], "help"))
if (!strcmp(argv[1], "help")) {
serialCOM.sendString(help_text);
else
} else if (!strcmp(argv[1], "on")) {
motor_dac.on();
} else if (!strcmp(argv[1], "off")) {
motor_dac.off();
} else if (!strcmp(argv[1], "breath")) {
motor_dac.breath();
} else {
serialCOM.sendString("Unknown Command\n");
}
}
}
if (argc == 3) {
if (!strcmp(argv[1], "level")) {
motor_dac.setLevel(atof(argv[2]));
Expand Down
13 changes: 13 additions & 0 deletions Application/Src/DAC.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DAC.hpp"

#include "Instances.hpp"
#include "cmath"

CustomDAC::CustomDAC() {}

Expand Down Expand Up @@ -33,6 +34,15 @@ void CustomDAC::on() { setState(CustomDAC::State::s_on); }

void CustomDAC::off() { setState(CustomDAC::State::s_off); }

void CustomDAC::breath() { setState(CustomDAC::State::s_breath); }

void CustomDAC::schedule() {
if (m_state == CustomDAC::State::s_breath) {
m_level = abs(sin(HAL_GetTick() % m_breeath_period)) * 3.3;
}
applyLevel();
}

void CustomDAC::setState(uint8_t cmd) { setState(static_cast<CustomDAC::State>(cmd)); }

uint8_t CustomDAC::getState() { return static_cast<uint8_t>(m_state); }
Expand All @@ -54,6 +64,9 @@ void CustomDAC::setState(CustomDAC::State cmd) {
case CustomDAC::State::s_on:
applyLevel();
break;
case CustomDAC::State::s_breath:
zeroLevel();
break;
}
m_state = cmd;
}
32 changes: 13 additions & 19 deletions Application/Src/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Thread::Thread() {
auto t2 = [](void *arg) { static_cast<Thread *>(arg)->init(); };
xTaskCreate(t2, "system init", 256, this, 6, &init_handle);

// auto t3 = [](void *arg) { static_cast<Thread *>(arg)->utilities(); };
// xTaskCreate(t3, "utilities", 64, this, 1, &utilities_handle);
auto t3 = [](void *arg) { static_cast<Thread *>(arg)->utilities(); };
xTaskCreate(t3, "utilities", 128, this, 1, &utilities_handle);

auto a2 = [](void *arg) { static_cast<Thread *>(arg)->app_dac(); };
xTaskCreate(a2, "dac + adc", 64, this, 0, &app_dac_handle);
xTaskCreate(a2, "dac + adc", 128, this, 0, &app_dac_handle);

auto s1 = [](void *arg) { static_cast<Thread *>(arg)->schedule_20Hz(); };
xTaskCreate(s1, "schedule 20Hz", 64, this, 5, &schedule_20Hz_handle);
Expand Down Expand Up @@ -48,27 +48,21 @@ void Thread::init() {

void Thread::utilities() {
while (1) {
vTaskSuspend(NULL);
serialCOM.sendString("\nDAC Target Value: ");
serialCOM.sendNumber(motor_dac.getLevel());
serialCOM.sendString("\nADC Sensing Value: ");
serialCOM.sendNumber(sensor_adc.getVolt());
serialCOM.sendLn();
// vTaskSuspend(NULL);
vTaskDelay(1000);
}
}

void Thread::app_dac() {
while (1) {
// ADC + DAC

// if (sensor_adc.getValue() > 3000)
// motor_dac.setLevel(0);
// else
// motor_dac.addLevel(0.1);

// serialCOM.sendString("DAC value: ");
// serialCOM.sendNumber(motor_dac.getLevel());
// serialCOM.sendString("\nADC value: ");
// serialCOM.sendNumber(sensor_adc.getVolt());
// serialCOM.sendLn();

vTaskSuspend(NULL);
// vTaskDelay(10000);
motor_dac.schedule();
// vTaskSuspend(NULL);
vTaskDelay(1000);
}
}

Expand Down

0 comments on commit 092c48a

Please sign in to comment.