diff --git a/arduino/head_file_test/main/Command.cpp b/arduino/head_file_test/main/Command.cpp new file mode 100644 index 0000000..9c54a6b --- /dev/null +++ b/arduino/head_file_test/main/Command.cpp @@ -0,0 +1,28 @@ +#include "Command.h" + +void Command::run(char* str){ + for(int i=0; i < call_count; i++){ + if(isSentinel(call_ids[i],str)){ // case : call_ids = "T2" str = "T215.15" + call_list[i](str+strlen(call_ids[i])); // get 15.15 input function + break; + } + } +} +void Command::add(char* id, CommandCallback onCommand){ + call_list[call_count] = onCommand; + call_ids[call_count] = id; + call_count++; +} +void Command::scalar(float* value, char* user_cmd){ + *value = atof(user_cmd); +} +bool Command::isSentinel(char* ch,char* str) +{ + char s[strlen(ch)+1]; + strncpy(s,str,strlen(ch)); + s[strlen(ch)] = '\0'; //strncpy need add end '\0' + if(strcmp(ch, s) == 0) + return true; + else + return false; +} diff --git a/arduino/head_file_test/main/Command.h b/arduino/head_file_test/main/Command.h new file mode 100644 index 0000000..20e2fe5 --- /dev/null +++ b/arduino/head_file_test/main/Command.h @@ -0,0 +1,17 @@ +#include +// callback function pointer definiton +typedef void (* CommandCallback)(char*); //!< command callback function pointer +class Command +{ + public: + void add(char* id , CommandCallback onCommand); + void run(char* str); + void scalar(float* value, char* user_cmd); + bool isSentinel(char* ch,char* str); + private: + // Subscribed command callback variables + CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number + char* call_ids[20]; //!< added callback commands + int call_count;//!< number callbacks that are subscribed + +}; diff --git a/arduino/head_file_test/main/main.ino b/arduino/head_file_test/main/main.ino new file mode 100644 index 0000000..915d5ff --- /dev/null +++ b/arduino/head_file_test/main/main.ino @@ -0,0 +1,11 @@ +#include "Command.h" +Command command;//声明一个自己的该类的对象 +void setup() { + // put your setup code here, to run once: + +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/arduino/main/Command.cpp b/arduino/main/Command.cpp new file mode 100644 index 0000000..9c54a6b --- /dev/null +++ b/arduino/main/Command.cpp @@ -0,0 +1,28 @@ +#include "Command.h" + +void Command::run(char* str){ + for(int i=0; i < call_count; i++){ + if(isSentinel(call_ids[i],str)){ // case : call_ids = "T2" str = "T215.15" + call_list[i](str+strlen(call_ids[i])); // get 15.15 input function + break; + } + } +} +void Command::add(char* id, CommandCallback onCommand){ + call_list[call_count] = onCommand; + call_ids[call_count] = id; + call_count++; +} +void Command::scalar(float* value, char* user_cmd){ + *value = atof(user_cmd); +} +bool Command::isSentinel(char* ch,char* str) +{ + char s[strlen(ch)+1]; + strncpy(s,str,strlen(ch)); + s[strlen(ch)] = '\0'; //strncpy need add end '\0' + if(strcmp(ch, s) == 0) + return true; + else + return false; +} diff --git a/arduino/main/Command.h b/arduino/main/Command.h new file mode 100644 index 0000000..20e2fe5 --- /dev/null +++ b/arduino/main/Command.h @@ -0,0 +1,17 @@ +#include +// callback function pointer definiton +typedef void (* CommandCallback)(char*); //!< command callback function pointer +class Command +{ + public: + void add(char* id , CommandCallback onCommand); + void run(char* str); + void scalar(float* value, char* user_cmd); + bool isSentinel(char* ch,char* str); + private: + // Subscribed command callback variables + CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number + char* call_ids[20]; //!< added callback commands + int call_count;//!< number callbacks that are subscribed + +}; diff --git a/arduino/main/main.ino b/arduino/main/main.ino index 62f5ec6..14f411d 100644 --- a/arduino/main/main.ino +++ b/arduino/main/main.ino @@ -7,6 +7,7 @@ Deng's FOC 闭环速度控制例程 测试库:SimpleFOC 2.1.1 测试硬件: 默认PID针对的电机是 GB6010 ,使用自己的电机需要修改PID参数,才能实现更好效果 */ #include +#include "Command.h" #include #include //引用以使用异步UDP #include // Source: https://github.com/TKJElectronics/KalmanFilter @@ -65,6 +66,7 @@ BLDCDriver3PWM driver = BLDCDriver3PWM(32, 33, 25, 22); //命令设置 +Command command; double target_velocity = 0; double target_angle = 91; double target_voltage = 0; diff --git a/ctest/com.cpp b/ctest/Command.cpp similarity index 53% rename from ctest/com.cpp rename to ctest/Command.cpp index 58c3cb1..3933a55 100644 --- a/ctest/com.cpp +++ b/ctest/Command.cpp @@ -1,8 +1,11 @@ -#include "com.h" +#include "Command.h" +#include +#include +#include void Command::run(char* str){ for(int i=0; i < call_count; i++){ - if(call_ids[i]){ - call_list[i](&str); + if(isSentinel(call_ids[i],str)){ // case : call_ids = "T2" str = "T215.15" + call_list[i](str+strlen(call_ids[i])); // get 15.15 input function break; } } @@ -15,10 +18,11 @@ void Command::add(char* id, CommandCallback onCommand){ void Command::scalar(float* value, char* user_cmd){ *value = atof(user_cmd); } -bool Commander::isSentinel(char* ch,char* str) +bool Command::isSentinel(char* ch,char* str) { - char s[strlen(ch)]; + char s[strlen(ch)+1]; strncpy(s,str,strlen(ch)); + s[strlen(ch)] = '\0'; //strncpy need add end '\0' if(strcmp(ch, s) == 0) return true; else diff --git a/ctest/com.h b/ctest/Command.h similarity index 72% rename from ctest/com.h rename to ctest/Command.h index 0c4a509..ff73c50 100644 --- a/ctest/com.h +++ b/ctest/Command.h @@ -5,12 +5,12 @@ class Command public: void add(char* id , CommandCallback onCommand); void run(char* str); - void Command::scalar(float* value, char* user_cmd); - bool Commander::isSentinel(char* ch); + void scalar(float* value, char* user_cmd); + bool isSentinel(char* ch,char* str); private: // Subscribed command callback variables CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number char* call_ids[20]; //!< added callback commands - int call_count = 0;//!< number callbacks that are subscribed + int call_count;//!< number callbacks that are subscribed -} +}; diff --git a/ctest/Command.o b/ctest/Command.o new file mode 100644 index 0000000..e26f4f9 Binary files /dev/null and b/ctest/Command.o differ diff --git a/ctest/Makefile.win b/ctest/Makefile.win new file mode 100644 index 0000000..4d27c4d --- /dev/null +++ b/ctest/Makefile.win @@ -0,0 +1,31 @@ +# Project: Ŀ2 +# Makefile created by Dev-C++ 5.11 + +CPP = g++.exe +CC = gcc.exe +WINDRES = windres.exe +OBJ = main.o Command.o +LINKOBJ = main.o Command.o +LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc +INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" +CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++" +BIN = Ŀ2.exe +CXXFLAGS = $(CXXINCS) +CFLAGS = $(INCS) +RM = rm.exe -f + +.PHONY: all all-before all-after clean clean-custom + +all: all-before $(BIN) all-after + +clean: clean-custom + ${RM} $(OBJ) $(BIN) + +$(BIN): $(OBJ) + $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS) + +main.o: main.cpp + $(CPP) -c main.cpp -o main.o $(CXXFLAGS) + +Command.o: Command.cpp + $(CPP) -c Command.cpp -o Command.o $(CXXFLAGS) diff --git a/ctest/ctest1.cpp b/ctest/ctest1.cpp index dfb59b3..c159322 100644 --- a/ctest/ctest1.cpp +++ b/ctest/ctest1.cpp @@ -1,7 +1,8 @@ #include #include #include -#include "com.h" +#include "Command.h" +Command command; char buf[255]; void wifi_print(char * s,double num) { @@ -18,24 +19,8 @@ void re_command(float *num) { *num = *num + 1; } -char* str = "abc23"; -char* cmd_id = "abc"; +//void doTarget(char* cmd) { command.scalar(&v, cmd); } main() { - re_command(&v); - char s[strlen(cmd_id)]; - - strncpy(s,str,strlen(cmd_id)); - printf("%s", s); - if(strcmp(cmd_id, s) == 0) - { - printf("%f", v); - v = atof(str+strlen(cmd_id)); - printf("%f", v); - } - -// char* da; -// da = "123456"; -// if (da[0] == '2') - +command.run(); } diff --git a/ctest/main.cpp b/ctest/main.cpp new file mode 100644 index 0000000..b23785b --- /dev/null +++ b/ctest/main.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include "Command.h" +Command command; +char buf[255]; +void wifi_print(char * s,double num) +{ + char str[255]; + char n[255]; + sprintf(n, "%.2f",num); + strcpy(str,s); + strcat(str, n); + strcat(buf+strlen(buf), str); + strcat(buf, ","); +} +float v = 0; +float a = 0; +void re_command(float *num) +{ + *num = *num + 1; +} +void doTarget_v(char* cmd) { command.scalar(&v, cmd); } +void doTarget_a(char* cmd) { command.scalar(&a, cmd); } +main() +{ + command.add("T", doTarget_v); + command.add("A", doTarget_a); + command.run("A2233"); + printf("%.2f",v); + printf("%.2f",a); +} diff --git a/ctest/main.o b/ctest/main.o new file mode 100644 index 0000000..6bcc5b4 Binary files /dev/null and b/ctest/main.o differ diff --git a/ctest/项目1.layout b/ctest/项目1.layout new file mode 100644 index 0000000..50e629d --- /dev/null +++ b/ctest/项目1.layout @@ -0,0 +1,8 @@ +[Editors] +Order=0 +Focused=0 +[Editor_0] +CursorCol=17 +CursorRow=14 +TopLine=8 +LeftChar=1 diff --git a/ctest/项目2.dev b/ctest/项目2.dev new file mode 100644 index 0000000..db0fa83 --- /dev/null +++ b/ctest/项目2.dev @@ -0,0 +1,82 @@ +[Project] +FileName=Ŀ2.dev +Name=Ŀ2 +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=1 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=0 +CompilerSettings=0000000000000000000000000 +UnitCount=3 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit1] +FileName=main.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=Command.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=Command.h +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/ctest/ctest1.exe b/ctest/项目2.exe similarity index 60% rename from ctest/ctest1.exe rename to ctest/项目2.exe index f9108e6..f6b1c6d 100644 Binary files a/ctest/ctest1.exe and b/ctest/项目2.exe differ diff --git a/ctest/项目2.layout b/ctest/项目2.layout new file mode 100644 index 0000000..3842228 --- /dev/null +++ b/ctest/项目2.layout @@ -0,0 +1,8 @@ +[Editors] +Order=0 +Focused=1 +[Editor_0] +CursorCol=2 +CursorRow=20 +TopLine=12 +LeftChar=1