command完成

master
45coll 2021-10-09 16:07:38 +08:00
parent 3bca145d5f
commit b4dbfe735f
17 changed files with 281 additions and 28 deletions

View File

@ -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;
}

View File

@ -0,0 +1,17 @@
#include <Arduino.h>
// 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
};

View File

@ -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:
}

28
arduino/main/Command.cpp Normal file
View File

@ -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;
}

17
arduino/main/Command.h Normal file
View File

@ -0,0 +1,17 @@
#include <Arduino.h>
// 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
};

View File

@ -7,6 +7,7 @@ Deng's FOC 闭环速度控制例程 测试库SimpleFOC 2.1.1 测试硬件:
PID GB6010 使PID PID GB6010 使PID
*/ */
#include <SimpleFOC.h> #include <SimpleFOC.h>
#include "Command.h"
#include <WiFi.h> #include <WiFi.h>
#include <AsyncUDP.h> //引用以使用异步UDP #include <AsyncUDP.h> //引用以使用异步UDP
#include <Kalman.h> // Source: https://github.com/TKJElectronics/KalmanFilter #include <Kalman.h> // 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_velocity = 0;
double target_angle = 91; double target_angle = 91;
double target_voltage = 0; double target_voltage = 0;

View File

@ -1,8 +1,11 @@
#include "com.h" #include "Command.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Command::run(char* str){ void Command::run(char* str){
for(int i=0; i < call_count; i++){ for(int i=0; i < call_count; i++){
if(call_ids[i]){ if(isSentinel(call_ids[i],str)){ // case : call_ids = "T2" str = "T215.15"
call_list[i](&str); call_list[i](str+strlen(call_ids[i])); // get 15.15 input function
break; break;
} }
} }
@ -15,10 +18,11 @@ void Command::add(char* id, CommandCallback onCommand){
void Command::scalar(float* value, char* user_cmd){ void Command::scalar(float* value, char* user_cmd){
*value = atof(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)); strncpy(s,str,strlen(ch));
s[strlen(ch)] = '\0'; //strncpy need add end '\0'
if(strcmp(ch, s) == 0) if(strcmp(ch, s) == 0)
return true; return true;
else else

View File

@ -5,12 +5,12 @@ class Command
public: public:
void add(char* id , CommandCallback onCommand); void add(char* id , CommandCallback onCommand);
void run(char* str); void run(char* str);
void Command::scalar(float* value, char* user_cmd); void scalar(float* value, char* user_cmd);
bool Commander::isSentinel(char* ch); bool isSentinel(char* ch,char* str);
private: private:
// Subscribed command callback variables // Subscribed command callback variables
CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number
char* call_ids[20]; //!< added callback commands char* call_ids[20]; //!< added callback commands
int call_count = 0;//!< number callbacks that are subscribed int call_count;//!< number callbacks that are subscribed
} };

BIN
ctest/Command.o Normal file

Binary file not shown.

31
ctest/Makefile.win Normal file
View File

@ -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)

View File

@ -1,7 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "com.h" #include "Command.h"
Command command;
char buf[255]; char buf[255];
void wifi_print(char * s,double num) void wifi_print(char * s,double num)
{ {
@ -18,24 +19,8 @@ void re_command(float *num)
{ {
*num = *num + 1; *num = *num + 1;
} }
char* str = "abc23"; //void doTarget(char* cmd) { command.scalar(&v, cmd); }
char* cmd_id = "abc";
main() main()
{ {
re_command(&v); command.run();
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')
} }

32
ctest/main.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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);
}

BIN
ctest/main.o Normal file

Binary file not shown.

8
ctest/项目1.layout Normal file
View File

@ -0,0 +1,8 @@
[Editors]
Order=0
Focused=0
[Editor_0]
CursorCol=17
CursorRow=14
TopLine=8
LeftChar=1

82
ctest/项目2.dev Normal file
View File

@ -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=

8
ctest/项目2.layout Normal file
View File

@ -0,0 +1,8 @@
[Editors]
Order=0
Focused=1
[Editor_0]
CursorCol=2
CursorRow=20
TopLine=12
LeftChar=1