【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 
  实验二百二十五:AS5600磁编码器 磁感应角度测量传感器  12bit高精度模块 
  项目实验之十五:转动42步进电机主轴从AS5600读取实时位置和速度值 
 
实验开源代码 
 
			
			
			- /*
 -   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
 -   实验二百二十五:AS5600磁编码器 磁感应角度测量传感器  12bit高精度模块
 -   项目实验之十五:转动42步进电机主轴从AS5600读取实时位置和速度值
 - */
 - 
 - #include <Wire.h>
 - #include <AS5600.h>
 - #include <SimpleFOC.h>
 - 
 - // 使用SimpleFOC库中的低通滤波器
 - LowPassFilter as5600_filter(0.01);
 - #define AS5600_ADDRESS 0x36
 - #define ANGLE_REG 0x0C
 - 
 - AS5600 encoder;
 - 
 - void setup() {
 -   Serial.begin(115200);
 -   Wire.begin(); // 初始化I2C
 -   Serial.println("完成初始化I2C,准备就绪!");
 - }
 - 
 - void loop() {
 -   // 读取位置
 -   uint16_t position = encoder.getCumulativePosition();
 -   float angle = fmod(position * 0.08789, 360.0);// 将位置值转换为角度(0.08789度/LSB)
 - 
 -   // 计算速度
 -   static float last_angle = 0;
 -   static unsigned long last_time = 0;
 -   unsigned long current_time = millis();
 -   float delta_time = (current_time - last_time) / 1000.0; // 转换为秒
 -   float speed = (angle - last_angle) / delta_time; // 角速度,单位为度/秒
 - 
 -   // 打印位置和速度
 -   Serial.print("角度= ");
 -   Serial.print(angle);
 -   Serial.print(" 度, 速度= ");
 -   Serial.print(speed);
 -   Serial.println(" 度/秒");
 - 
 -   // 更新上一次的角度和时间
 -   last_angle = angle;
 -   last_time = current_time;
 - 
 -   delay(500); // 延迟500毫秒
 - }
 
  复制代码
  
 
 |