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

Set up USB serial parsing of velocity and throttle #36

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider": "ms-vscode.cmake-tools"
},
{
"name": "Pico (Ubuntu)",
"includePath": ["${workspaceFolder}/**", "${env:PICO_SDK_PATH}/**"],
"defines": [],
"compilerPath": "/usr/bin/arm-none-eabi-gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
Expand Down
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@
"CMAKE_MODULE_PATH": "${env:PICO_INSTALL_PATH}/pico-sdk-tools"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"C_Cpp.autoAddFileAssociations": false
"C_Cpp.autoAddFileAssociations": false,
"C_Cpp.vcFormat.newLine.beforeOpenBrace.type": "newLine",
"C_Cpp.formatting": "vcFormat",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.lambda": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.namespace": "newLine",
"C_Cpp.vcFormat.indent.accessSpecifiers": false
}
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ cmake_minimum_required(VERSION 3.13)
# Include build functions from Pico SDK
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

include_directories(include/)
file(GLOB SOURCE "src/*.cpp")
file(GLOB HEADER "include/*.hpp")

# Set name of project (as PROJECT_NAME) and C/C standards
project(inverter-control C CXX ASM)
set(CMAKE_C_STANDARD 11)
Expand All @@ -14,6 +18,8 @@ pico_sdk_init()

# Tell CMake where to find the executable source file
add_executable(${PROJECT_NAME}
${HEADER}
${SOURCE}
src/main.cpp
)

Expand Down
12 changes: 12 additions & 0 deletions include/pod_communication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef POD_COMMUNICATION_HPP
#define POD_COMMUNICATION_HPP

struct LimControlMessage
{
float velocity;
float throttle;
};

LimControlMessage read_control_message();

#endif
39 changes: 25 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <cmath>
#include <vector>

#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "tusb.h"

#include <cmath>
#include <vector>
#include "pod_communication.hpp"

const unsigned PIN_LOGIC = 28;
const unsigned PIN_ENABLE = 14;
Expand All @@ -17,9 +20,10 @@ constexpr bool TEST_CIRCUIT = false;

void initialize_pins()
{
const std::vector<unsigned> pins = TEST_CIRCUIT ?
const std::vector<unsigned> pins = TEST_CIRCUIT ?
std::vector<unsigned>{PIN_LOGIC, PIN_ENABLE} :
std::vector<unsigned>{pin_H, pin_L};
std::vector<unsigned>{ pin_H, pin_L };

for (unsigned pin : pins)
{
gpio_init(pin);
Expand All @@ -35,7 +39,6 @@ float calculate_frequency(float velocity, float throttle)
float g = 0.0305; // air gap between stators (meters)
float mu_r = 1.00000037; // relative permeability of air


// The thrust equation has the form F = Bs / (C + As^2)
// which has a peak value at s = √(C/A)
// where C is the square of the magnetic sensitivity
Expand All @@ -56,7 +59,8 @@ float calculate_frequency(float velocity, float throttle)
return (slip + velocity) * 2 * M_PI / L;
}

void set_logic_pin_(bool v) {
void set_logic_pin_(bool v)
{
gpio_put(PIN_LOGIC, v);
gpio_put(PIN_ENABLE, 1);
}
Expand All @@ -66,10 +70,13 @@ void set_hilo_pins_(bool v)
// Even though we should not need to manually introduce a delay (deadtime),
// we should still ensure that the rise always occurs after the fall on the
// GPIO pins for each phase.
if (v) {
if (v)
{
gpio_put(pin_L, !v);
gpio_put(pin_H, v);
} else {
}
else
{
gpio_put(pin_H, v);
gpio_put(pin_L, !v);
}
Expand All @@ -91,21 +98,25 @@ void run_inverter_cycle(int N, float amplitude)
}
}

int frequency_to_samples(float frequency) {
int frequency_to_samples(float frequency)
{
return OPERATING_FREQUENCY / frequency - OFFSET;
}

float get_frequency() {
return 1;
}

int main()
{
stdio_init_all();

// Wait until USB device is connected
while (!tud_cdc_connected())
sleep_ms(250);

initialize_pins();

while (true)
{
float frequency = get_frequency();
LimControlMessage message = read_control_message();
float frequency = calculate_frequency(message.velocity, message.throttle);
int N = frequency_to_samples(frequency);
run_inverter_cycle(N, 1);
}
Expand Down
14 changes: 14 additions & 0 deletions src/pod_communication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include "pod_communication.hpp"

LimControlMessage read_control_message()
{
float velocity;
float throttle;
std::cin >> velocity >> throttle;

std::cout << "Received velocity: " << velocity << std::endl;
std::cout << "Received throttle: " << throttle << std::endl;

return { velocity, throttle };
}