WIP: start cpp refactoring
This commit is contained in:
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Pico",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"${userHome}/.pico-sdk/sdk/2.3.1/**"
|
||||
],
|
||||
"forcedInclude": [
|
||||
"${workspaceFolder}/build/generated/pico_base/pico/config_autogen.h",
|
||||
"${userHome}/.pico-sdk/sdk/2.3.1/src/common/pico_base_headers/include/pico.h"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "${userHome}/.pico-sdk/toolchain/15_2_Rel1/bin/arm-none-eabi-gcc",
|
||||
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
|
||||
"cStandard": "c17",
|
||||
"cppStandard": "c++14",
|
||||
"intelliSenseMode": "linux-gcc-arm"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
Vendored
+9
-3
@@ -1,9 +1,15 @@
|
||||
[
|
||||
{
|
||||
"name": "RPi Pico ─ tttapa/arm-pico-eabi",
|
||||
"toolchainFile": "${env:HOME}/opt/x-tools/arm-pico-eabi/arm-pico-eabi.toolchain.cmake",
|
||||
"name": "Pico",
|
||||
"compilers": {
|
||||
"C": "${command:raspberry-pi-pico.getCompilerPath}",
|
||||
"CXX": "${command:raspberry-pi-pico.getCxxCompilerPath}"
|
||||
},
|
||||
"environmentVariables": {
|
||||
"PICO_SDK_PATH": "${env:HOME}/pico/pico-sdk"
|
||||
"PATH": "${command:raspberry-pi-pico.getEnvPath};${env:PATH}"
|
||||
},
|
||||
"cmakeSettings": {
|
||||
"Python3_EXECUTABLE": "${command:raspberry-pi-pico.getPythonPath}"
|
||||
}
|
||||
}
|
||||
]
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"marus25.cortex-debug",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.cpptools-extension-pack",
|
||||
"ms-vscode.vscode-serial-monitor",
|
||||
"raspberry-pi.raspberry-pi-pico"
|
||||
]
|
||||
}
|
||||
Vendored
+45
-21
@@ -1,28 +1,52 @@
|
||||
{
|
||||
"version": "0.3.0",
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Pico attach to OpenOCD",
|
||||
"type": "cppdbg",
|
||||
"name": "Pico Debug (Cortex-Debug)",
|
||||
"cwd": "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
|
||||
"executable": "${command:raspberry-pi-pico.launchTargetPath}",
|
||||
"request": "launch",
|
||||
"program": "${command:cmake.launchTargetPath}",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"miDebuggerServerAddress": "localhost:3333",
|
||||
"useExtendedRemote": true,
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb-multiarch",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
},
|
||||
{
|
||||
"description": "Set Disassembly Flavor to Intel",
|
||||
"text": "-gdb-set disassembly-flavor intel",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
"type": "cortex-debug",
|
||||
"servertype": "openocd",
|
||||
"serverpath": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
|
||||
"gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
|
||||
"debuggerArgs": ["-ex", "set debug-file-directory /debug"],
|
||||
"device": "${command:raspberry-pi-pico.getChipUppercase}",
|
||||
"configFiles": [
|
||||
"interface/cmsis-dap.cfg",
|
||||
"target/${command:raspberry-pi-pico.getTarget}.cfg"
|
||||
],
|
||||
"svdFile": "${userHome}/.pico-sdk/sdk/2.3.1/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
|
||||
"runToEntryPoint": "main",
|
||||
// Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
|
||||
// Also works fine for flash binaries
|
||||
"overrideLaunchCommands": [
|
||||
"monitor reset init",
|
||||
"load \"${command:raspberry-pi-pico.launchTargetPath}\""
|
||||
],
|
||||
"openOCDLaunchCommands": [
|
||||
"adapter speed 5000"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Pico Debug (Cortex-Debug with external OpenOCD)",
|
||||
"cwd": "${workspaceRoot}",
|
||||
"executable": "${command:raspberry-pi-pico.launchTargetPath}",
|
||||
"request": "launch",
|
||||
"type": "cortex-debug",
|
||||
"servertype": "external",
|
||||
"gdbTarget": "localhost:3333",
|
||||
"gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
|
||||
"debuggerArgs": ["-ex", "set debug-file-directory /debug"],
|
||||
"device": "${command:raspberry-pi-pico.getChipUppercase}",
|
||||
"svdFile": "${userHome}/.pico-sdk/sdk/2.3.1/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
|
||||
"runToEntryPoint": "main",
|
||||
// Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
|
||||
// Also works fine for flash binaries
|
||||
"overrideLaunchCommands": [
|
||||
"monitor reset init",
|
||||
"load \"${command:raspberry-pi-pico.launchTargetPath}\""
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"cmake.showSystemKits": false,
|
||||
"cmake.options.statusBarVisibility": "hidden",
|
||||
"cmake.options.advanced": {
|
||||
"build": {
|
||||
"statusBarVisibility": "hidden"
|
||||
},
|
||||
"launch": {
|
||||
"statusBarVisibility": "hidden"
|
||||
},
|
||||
"debug": {
|
||||
"statusBarVisibility": "hidden"
|
||||
},
|
||||
"variant": {
|
||||
"statusBarVisibility": "hidden"
|
||||
},
|
||||
"buildTarget": {
|
||||
"statusBarVisibility": "hidden"
|
||||
}
|
||||
},
|
||||
"cmake.configureOnEdit": false,
|
||||
"cmake.automaticReconfigure": false,
|
||||
"cmake.configureOnOpen": false,
|
||||
"cmake.generator": "Ninja",
|
||||
"cmake.cmakePath": "${userHome}/.pico-sdk/cmake/v4.3.4/bin/cmake",
|
||||
"C_Cpp.debugShortcut": false,
|
||||
"terminal.integrated.env.windows": {
|
||||
"PICO_SDK_PATH": "${env:USERPROFILE}/.pico-sdk/sdk/2.3.1",
|
||||
"PICO_TOOLCHAIN_PATH": "${env:USERPROFILE}/.pico-sdk/toolchain/15_2_Rel1",
|
||||
"Path": "${env:USERPROFILE}/.pico-sdk/toolchain/15_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.3.1/picotool;${env:USERPROFILE}/.pico-sdk/cmake/v4.3.4/bin;${env:USERPROFILE}/.pico-sdk/ninja/v1.13.2;${env:PATH}"
|
||||
},
|
||||
"terminal.integrated.env.osx": {
|
||||
"PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.3.1",
|
||||
"PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/15_2_Rel1",
|
||||
"PATH": "${env:HOME}/.pico-sdk/toolchain/15_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.3.1/picotool:${env:HOME}/.pico-sdk/cmake/v4.3.4/bin:${env:HOME}/.pico-sdk/ninja/v1.13.2:${env:PATH}"
|
||||
},
|
||||
"terminal.integrated.env.linux": {
|
||||
"PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.3.1",
|
||||
"PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/15_2_Rel1",
|
||||
"PATH": "${env:HOME}/.pico-sdk/toolchain/15_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.3.1/picotool:${env:HOME}/.pico-sdk/cmake/v4.3.4/bin:${env:HOME}/.pico-sdk/ninja/v1.13.2:${env:PATH}"
|
||||
},
|
||||
"raspberry-pi-pico.cmakeAutoConfigure": true,
|
||||
"raspberry-pi-pico.useCmakeTools": false,
|
||||
"raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v4.3.4/bin/cmake",
|
||||
"raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.13.2/ninja"
|
||||
}
|
||||
Vendored
+102
@@ -0,0 +1,102 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "Compile Project",
|
||||
"type": "process",
|
||||
"isBuildCommand": true,
|
||||
"command": "${userHome}/.pico-sdk/ninja/v1.13.2/ninja",
|
||||
"args": ["-C", "${workspaceFolder}/build"],
|
||||
"group": "build",
|
||||
"presentation": {
|
||||
"reveal": "always",
|
||||
"panel": "dedicated"
|
||||
},
|
||||
"problemMatcher": "$gcc",
|
||||
"windows": {
|
||||
"command": "${env:USERPROFILE}/.pico-sdk/ninja/v1.13.2/ninja.exe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Run Project",
|
||||
"type": "process",
|
||||
"command": "${env:HOME}/.pico-sdk/picotool/2.3.1/picotool/picotool",
|
||||
"args": [
|
||||
"load",
|
||||
"${command:raspberry-pi-pico.launchTargetPath}",
|
||||
"-fx"
|
||||
],
|
||||
"presentation": {
|
||||
"reveal": "always",
|
||||
"panel": "dedicated"
|
||||
},
|
||||
"problemMatcher": [],
|
||||
"windows": {
|
||||
"command": "${env:USERPROFILE}/.pico-sdk/picotool/2.3.1/picotool/picotool.exe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Flash",
|
||||
"type": "process",
|
||||
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
|
||||
"args": [
|
||||
"-s",
|
||||
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
|
||||
"-f",
|
||||
"interface/cmsis-dap.cfg",
|
||||
"-f",
|
||||
"target/${command:raspberry-pi-pico.getTarget}.cfg",
|
||||
"-c",
|
||||
"adapter speed 5000; program \"${command:raspberry-pi-pico.launchTargetPath}\" verify reset exit"
|
||||
],
|
||||
"problemMatcher": [],
|
||||
"windows": {
|
||||
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Rescue Reset",
|
||||
"type": "process",
|
||||
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
|
||||
"args": [
|
||||
"-s",
|
||||
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
|
||||
"-f",
|
||||
"interface/cmsis-dap.cfg",
|
||||
"-f",
|
||||
"target/${command:raspberry-pi-pico.getChip}-rescue.cfg",
|
||||
"-c",
|
||||
"adapter speed 5000; reset halt; exit"
|
||||
],
|
||||
"problemMatcher": [],
|
||||
"windows": {
|
||||
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "RISC-V Reset (RP2350)",
|
||||
"type": "process",
|
||||
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
|
||||
"args": [
|
||||
"-s",
|
||||
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
|
||||
"-c",
|
||||
"set USE_CORE { rv0 rv1 cm0 cm1 }",
|
||||
"-f",
|
||||
"interface/cmsis-dap.cfg",
|
||||
"-f",
|
||||
"target/rp2350.cfg",
|
||||
"-c",
|
||||
"adapter speed 5000; init;",
|
||||
"-c",
|
||||
"write_memory 0x40120158 8 { 0x3 }; echo [format \"Info : ARCHSEL 0x%02x\" [read_memory 0x40120158 8 1]];",
|
||||
"-c",
|
||||
"reset halt; targets rp2350.rv0; echo [format \"Info : ARCHSEL_STATUS 0x%02x\" [read_memory 0x4012015C 8 1]]; exit"
|
||||
],
|
||||
"problemMatcher": [],
|
||||
"windows": {
|
||||
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+51
-10
@@ -1,16 +1,57 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(pico-cpp LANGUAGES C CXX ASM)
|
||||
if (DEFINED PICO_SDK_VERSION_STRING)
|
||||
pico_sdk_init()
|
||||
# Generated Cmake Pico project file
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Initialise pico_sdk from installed location
|
||||
# (note this can come from environment, CMake cache etc)
|
||||
|
||||
# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
|
||||
if(WIN32)
|
||||
set(USERHOME $ENV{USERPROFILE})
|
||||
else()
|
||||
set(USERHOME $ENV{HOME})
|
||||
endif()
|
||||
set(sdkVersion 2.3.1)
|
||||
set(toolchainVersion 15_2_Rel1)
|
||||
set(picotoolVersion 2.3.1)
|
||||
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
|
||||
if (EXISTS ${picoVscode})
|
||||
include(${picoVscode})
|
||||
endif()
|
||||
# ====================================================================================
|
||||
set(PICO_BOARD pico CACHE STRING "Board type")
|
||||
|
||||
add_library(warnings INTERFACE)
|
||||
target_compile_options(warnings INTERFACE "-Wall" "-Wextra" "-Wno-psabi")
|
||||
# Pull in Raspberry Pi Pico SDK (must be before project)
|
||||
include(pico_sdk_import.cmake)
|
||||
|
||||
add_executable(main "main.cpp")
|
||||
target_compile_features(main PRIVATE cxx_std_23)
|
||||
target_link_libraries(main PRIVATE warnings pico_stdlib hardware_adc)
|
||||
pico_enable_stdio_usb(main 1)
|
||||
project(main C CXX ASM)
|
||||
|
||||
# Initialise the Raspberry Pi Pico SDK
|
||||
pico_sdk_init()
|
||||
|
||||
# Add executable. Default name is the project name, version 0.1
|
||||
|
||||
add_executable(main main.cpp )
|
||||
|
||||
pico_set_program_name(main "main")
|
||||
pico_set_program_version(main "0.1")
|
||||
|
||||
# Modify the below lines to enable/disable output over UART/USB
|
||||
pico_enable_stdio_uart(main 0)
|
||||
pico_enable_stdio_usb(main 1)
|
||||
|
||||
# Add the standard library to the build
|
||||
target_link_libraries(main
|
||||
pico_stdlib)
|
||||
|
||||
# Add the standard include files to the build
|
||||
target_include_directories(main PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
pico_add_extra_outputs(main)
|
||||
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
include "pico/stdlib.h"
|
||||
include <stdio.h>
|
||||
include <stdlib.h>
|
||||
|
||||
define POLL_PERIOD 50 //time between polls (ms)
|
||||
define MAX_STORED_VALUES 10 //number of stored values to calculate e_dot and e_dot_dot values
|
||||
|
||||
int kp = 10;
|
||||
int ki = 100;
|
||||
int kd = 1000;
|
||||
|
||||
int set_temp = 30;
|
||||
|
||||
int* e_prev;
|
||||
|
||||
// Perform initialisation
|
||||
int* create_array(int size) {
|
||||
int* array = (int*)malloc(size * sizeof(int));
|
||||
|
||||
for (int i = 0; i < size; i++) { //fill with 0s so it doesnt confuse the controller
|
||||
array[i] = 0;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
int calculate_integral(int* array) {
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < MAX_STORED_VALUES; i++ ) {
|
||||
sum += array[i];
|
||||
}
|
||||
|
||||
sum *= POLL_PERIOD;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int* push_array(int* array, int value) {
|
||||
for (int i = 1; i < MAX_STORED_VALUES; i++) {
|
||||
array[i] = array[i-1];
|
||||
}
|
||||
|
||||
array[0] = value;
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
void init(void) {
|
||||
*e_prev = create_array(MAX_STORED_VALUES);
|
||||
gpio_init(PICO_DEFAULT_LED_PIN);
|
||||
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
}
|
||||
|
||||
void pico_set_led(bool led_on) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, led_on);
|
||||
}
|
||||
|
||||
int calculate_pid(int* error_array, float x) {
|
||||
int e = set_temp-x;
|
||||
|
||||
push_array(error_array, e);
|
||||
|
||||
int e_dot = (e - error_array[0]) / POLL_PERIOD;
|
||||
int e_int = calculate_integral(error_array);
|
||||
|
||||
return e * kp + e_dot * kd + e_int * ki;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//init();
|
||||
stdio_init_all();
|
||||
while (true)
|
||||
{
|
||||
/* code */
|
||||
printf("sadf");
|
||||
sleep_ms(100);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <array>
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define POLL_PERIOD 50 //time between polls (ms)
|
||||
#define MAX_STORED_VALUES 10 //number of stored values to calculate e_dot and e_dot_dot values
|
||||
|
||||
/*
|
||||
trying to keep the code clean, so some conventions:
|
||||
classes: PascalCase
|
||||
function: camelCase
|
||||
var names: snake_case
|
||||
*/
|
||||
|
||||
class CustomArray {
|
||||
int* items;
|
||||
int size;
|
||||
|
||||
public:
|
||||
CustomArray(int s) {
|
||||
size = s;
|
||||
items = (int*) malloc(s * sizeof(int));
|
||||
for (int i = 0; i < s; i++ )
|
||||
{
|
||||
items[i] = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class PID {
|
||||
private:
|
||||
int kp, ki, kd, pwm_pin, thermistor_pin, index, desired_temp;
|
||||
bool on;
|
||||
CustomArray error_array;
|
||||
|
||||
int calculate_integral(int* array) {
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < MAX_STORED_VALUES; i++ ) {
|
||||
sum += array[i];
|
||||
}
|
||||
|
||||
sum *= POLL_PERIOD;
|
||||
return sum;
|
||||
}
|
||||
|
||||
public:
|
||||
PID(int kp) {
|
||||
error_array = new CustomArray(MAX_STORED_VALUES);
|
||||
this.kp = kp;
|
||||
}
|
||||
|
||||
int calulatePid( int x ) {
|
||||
int e = desired_temp-x;
|
||||
|
||||
int e_dot = (e - error_array[0]) / POLL_PERIOD;
|
||||
int e_int = calculate_integral(error_array);
|
||||
|
||||
|
||||
return e * kp + e_dot * kd + e_int * ki;
|
||||
}
|
||||
};
|
||||
|
||||
void init() {
|
||||
gpio_init(25);
|
||||
gpio_set_dir(25, GPIO_OUT);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stdio_init_all();
|
||||
|
||||
|
||||
while (true) {
|
||||
//printf("Hello, world!\n");
|
||||
gpio_put(25, 0);
|
||||
sleep_ms(250);
|
||||
gpio_put(25, 1);
|
||||
sleep_ms(250);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user