Skip to content

Commit

Permalink
starting tutorial for 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Known4225 committed Oct 10, 2024
1 parent d348fd4 commit 92b941d
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 16 deletions.
3 changes: 3 additions & 0 deletions sdk/app_cpu1/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ int main()
// Initialize timing manager
timing_manager_init();

// test - enable ADC on timing manager
timing_manager_enable_sensor(ADC);

#if USER_CONFIG_ENABLE_LOGGING == 1
log_init();
#endif
Expand Down
60 changes: 44 additions & 16 deletions sdk/app_cpu1/user/.cproject

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions sdk/app_cpu1/user/.project
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
<linkedResources>
<link>
<name>common</name>
<type>2</type>
<location>C:/Users/srich008/Information/gitMistakes/1.3/sdk/app_cpu1/common</location>
</link>
</linkedResources>
</projectDescription>
8 changes: 8 additions & 0 deletions sdk/app_cpu1/user/usr/user_apps.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "usr/blink/app_blink.h"
#endif

#ifdef APP_VSI
#include <usr/vsiApp/app_vsiApp.h>
#endif

#ifdef APP_BETA_LABS
#include "usr/beta_labs/app_beta_labs.h"
#endif
Expand All @@ -38,6 +42,10 @@ void user_apps_init(void)
app_blink_init();
#endif

#ifdef APP_VSI
app_vsiApp_init();
#endif

#ifdef APP_BETA_LABS
app_beta_labs_init();
#endif
Expand Down
16 changes: 16 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/app_vsiApp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#ifdef APP_VSI

#include "usr/vsiApp/app_vsiApp.h"
#include "usr/vsiApp/cmd/cmd_vsiApp.h"
#include "usr/vsiApp/task_vsiApp.h"

void app_vsiApp_init(void)
{
// Register "vsi" command with system
cmd_vsiApp_register();

// task_vsiApp_init();
}

#endif // APP_VSI
6 changes: 6 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/app_vsiApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef APP_VSI_H
#define APP_VSI_H

void app_vsiApp_init(void);

#endif // APP_VSI_H
110 changes: 110 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/cmd/cmd_vsiApp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

#ifdef APP_VSI

#include "usr/vsiApp/cmd/cmd_vsiApp.h"
#include "sys/commands.h"
#include "sys/debug.h"
#include "sys/defines.h"
#include "sys/util.h"
#include <usr/vsiApp/task_vsiApp.h>
#include <stdlib.h>
#include <string.h>

// Stores command entry for command system module
static command_entry_t cmd_entry;

// Defines help content displayed for this command
// when user types "help" at command prompt
static command_help_t cmd_help[] = {
{ "init", "Start task" },
{ "deinit", "Stop task" },
{ "amplitude <value>", "set the analog wave to a specific amplitude" },
{ "frequency <value>", "set the analog wave to a specific frequency" },
{ "stats print", "Print stats to screen" },
{ "stats reset", "Reset the task timing stats" }
};

void cmd_vsiApp_register(void)
{
// Populate the command entry block
//
// Here is where you define the base command string: "blink"
// and what function is called to handle command
commands_cmd_init(&cmd_entry, "vsi", "VSI application commands", cmd_help, ARRAY_SIZE(cmd_help), cmd_vsiApp);

// Register the command with the system
commands_cmd_register(&cmd_entry);
}

int cmd_vsiApp(int argc, char **argv)
{
if (argc == 3 && strcmp("amplitude", argv[1]) == 0) {
double argDoub;
sscanf(argv[2], "%lf", &argDoub);
if (task_vsiApp_amplitude(argDoub) != SUCCESS) {
return CMD_FAILURE;
}

return CMD_SUCCESS;
}
if (argc == 3 && strcmp("frequency", argv[1]) == 0) {
double argDoub;
sscanf(argv[2], "%lf", &argDoub);
if (task_vsiApp_frequency(argDoub) != SUCCESS) {
return CMD_FAILURE;
}

return CMD_SUCCESS;
}
if (strcmp("RMS", argv[1]) == 0) {
if (argc == 4 && strcmp("set", argv[2]) == 0) {
double argDoub;
sscanf(argv[2], "%lf", &argDoub);
if (task_vsiApp_RMS(argDoub) != SUCCESS) {
return CMD_FAILURE;
}
return CMD_SUCCESS;
}
if (argc == 3 && strcmp("enable", argv[2]) == 0) {
if (task_vsiApp_RMS_enable() != SUCCESS) {
return CMD_FAILURE;
}
return CMD_SUCCESS;
}
if (argc == 3 && strcmp("disable", argv[2]) == 0) {
if (task_vsiApp_RMS_enable() != SUCCESS) {
return CMD_FAILURE;
}
return CMD_SUCCESS;
}
}

if (argc == 2 && strcmp("init", argv[1]) == 0) {
if (task_vsiApp_init() != SUCCESS) {
return CMD_FAILURE;
}

return CMD_SUCCESS;
}

if (argc == 2 && strcmp("deinit", argv[1]) == 0) {
if (task_vsiApp_deinit() != SUCCESS) {
return CMD_FAILURE;
}

return CMD_SUCCESS;
}
if (argc == 3 && strcmp("stats", argv[1]) == 0) {
if (strcmp("print", argv[2]) == 0) {
task_vsiApp_stats_print();
return CMD_SUCCESS_QUIET;
}
if (strcmp("reset", argv[2]) == 0) {
task_vsiApp_stats_reset();
return CMD_SUCCESS;
}
}
return CMD_INVALID_ARGUMENTS;
}

#endif // APP_VSI
11 changes: 11 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/cmd/cmd_vsiApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CMD_VSI_H
#define CMD_VSI_H

// Called in app init function to register command with system
void cmd_vsiApp_register(void);

// Function called when user types "vsi" command into command prompt
// i.e., this is the vsi command handler function
int cmd_vsiApp(int argc, char **argv);

#endif // CMD_VSI_H
145 changes: 145 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/task_vsiApp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

#ifdef APP_VSI

#include "usr/vsiApp/task_vsiApp.h"
#include "drv/led.h"
#include "drv/timing_manager.h"
#include "sys/scheduler.h"
#include "drv/cpu_timer.h"
#include "drv/pwm.h"
#include "drv/analog.h"
#include <math.h>
#include <stdlib.h>
#include <stdint.h>

// Scheduler TCB which holds task "context"
static task_control_block_t tcb = {0};


static double Ts = 1.0 / 10000.0; // [sec]
static double theta = 0.0; // [rad]
static double omega = 10.0 * 2 * PI; // [rad/s]
static double Do = 0.3; // [--]
static uint8_t RMS_driven = 0;
static double RMS_target = 0.0;

// Logging variables
float LOG_current_a = 0.0;
float LOG_current_b = 0.0;
float LOG_current_c = 0.0;

/* trying this out */
#include "xil_io.h"
#define PWM_MUX_ADDR_LOGGING (0x43C40000)

float LOG_voltage_a = 0.0;
float LOG_voltage_b = 0.0;
float LOG_voltage_c = 0.0;


int task_vsiApp_init(void)
{
if (scheduler_tcb_is_registered(&tcb)) {
return FAILURE;
}

pwm_enable();

// Fill TCB with parameters
scheduler_tcb_init(&tcb, task_vsiApp_callback, NULL, "vsiApp", TASK_VSI_INTERVAL_USEC);

task_stats_enable(&tcb.stats);

// Register task with scheduler
return scheduler_tcb_register(&tcb);
}

int task_vsiApp_deinit(void)
{
pwm_disable();
return scheduler_tcb_unregister(&tcb);
}

void task_vsiApp_callback(void *arg)
{

// Update theta
theta += (Ts * omega);
theta = fmod(theta, 2.0 * M_PI); // Wrap to 2*pi

// Calculate desired duty ratios
double duty_a = 0.5 + Do / 2.0 * cos(theta);
double duty_b = 0.5 + Do / 2.0 * cos(theta - 2.0 * M_PI / 3.0);
double duty_c = 0.5 + Do / 2.0 * cos(theta - 4.0 * M_PI / 3.0);

// Update PWM peripheral in FPGA
pwm_set_duty(0, duty_a); // Set HB1 duty ratio (INV1, PWM1 and PWM2)
pwm_set_duty(1, duty_b); // Set HB2 duty ratio (INV1, PWM3 and PWM4)
pwm_set_duty(2, duty_c); // Set HB3 duty ratio (INV1, PWM5 and PWM6)

// Update logging variables
analog_getf(ANALOG_IN5, &LOG_current_a);
analog_getf(ANALOG_IN6, &LOG_current_b);
analog_getf(ANALOG_IN7, &LOG_current_c);
LOG_current_a *= 0.5;
LOG_current_b *= 0.5;
LOG_current_c *= 0.5;

LOG_voltage_a = (duty_a - 0.5) * 20;
LOG_voltage_b = (duty_b - 0.5) * 20;
LOG_voltage_c = (duty_c - 0.5) * 20;

pwm_get_switching_freq() / timing_manager_get_ratio(); // number of samples per second
omega / (2.0 * M_PI); // number of periods per second
(pwm_get_switching_freq() / timing_manager_get_ratio()) * (2.0 * M_PI) / omega; // number of samples per period
if (RMS_driven) {
double currentRMS = calculateRMS();
Do += (RMS_target - currentRMS) * SQRT2;
}

}

double calculateRMS() {
return RMS_target;
}

int task_vsiApp_amplitude(double amplitude) {
Do = amplitude;
return SUCCESS;
}

int task_vsiApp_frequency(double frequency) {
omega = frequency * 2.0 * M_PI; // rad/s = frequency (Hz) * (2 * PI)
return SUCCESS;
}

int task_vsiApp_RMS(double RMS) {
RMS_target = RMS;
return SUCCESS;
}

int task_vsiApp_RMS_enable() {
if (RMS_driven == 0) {
RMS_driven = 1;
return SUCCESS;
}
return FAILURE;
}

int task_vsiApp_RMS_disable() {
if (RMS_driven) {
RMS_driven = 0;
return SUCCESS;
}
return FAILURE;
}

void task_vsiApp_stats_print(void) {
task_stats_print(&tcb.stats);
}

void task_vsiApp_stats_reset(void) {
task_stats_reset(&tcb.stats);
}

#endif // APP_BLINK
24 changes: 24 additions & 0 deletions sdk/app_cpu1/user/usr/vsiApp/task_vsiApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef TASK_VSIAPP_H
#define TASK_VSIAPP_H

#include "sys/scheduler.h"

#define TASK_VSI_UPDATES_PER_SEC (10000)
#define TASK_VSI_INTERVAL_USEC (USEC_IN_SEC / TASK_VSI_UPDATES_PER_SEC)

int task_vsiApp_init(void);
int task_vsiApp_deinit(void);

void task_vsiApp_callback(void *arg);

int task_vsiApp_amplitude(double amplitude);
int task_vsiApp_frequency(double frequency);
int task_vsiApp_RMS(double RMS);
int task_vsiApp_RMS_enable();
int task_vsiApp_RMS_disable();
double calculateRMS();

void task_vsiApp_stats_print(void);
void task_vsiApp_stats_reset(void);

#endif // TASK_VSIAPP_H

0 comments on commit 92b941d

Please sign in to comment.