-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
374 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |