当前位置:   article > 正文

编码电机脉冲数统计,测速

编码电机脉冲数统计,测速

 脉冲统计代码

  1. int reducation = 90;//减速比,根据电机参数设置,比如 15 | 30 | 60
  2. int pulse = 11; //编码器旋转一圈产生的脉冲数该值需要参考商家电机参数
  3. int per_round = pulse * reducation * 4;//车轮旋转一圈产生的脉冲数
  4. long start_time = millis();//一个计算周期的开始时刻,初始值为 millis();
  5. long interval_time = 50;//一个计算周期 50ms
  6. double current_vel;
  7. //获取当前转速的函数
  8. void get_current_vel(){
  9. long right_now = millis();
  10. long past_time = right_now - start_time;//计算逝去的时间
  11. if(past_time >= interval_time){//如果逝去时间大于等于一个计算周期
  12. //1.禁止中断
  13. noInterrupts();
  14. //2.计算转速 转速单位可以是秒,也可以是分钟... 自定义即可
  15. current_vel = (double)count / per_round / past_time * 1000 * 60;
  16. //3.重置计数器
  17. count = 0;
  18. //4.重置开始时间
  19. start_time = right_now;
  20. //5.重启中断
  21. interrupts();
  22. Serial.println(current_vel);
  23. }
  24. }
  25. void loop() {
  26. delay(10);
  27. get_current_vel();
  28. }

测速代码

  1. int motor_A = 21;//中端口是2
  2. int motor_B = 20;//中断口是3
  3. volatile int count = 0;//如果是正转,那么每计数一次自增1,如果是反转,那么每计数一次自减1
  4. void count_A(){
  5. //单频计数实现
  6. //手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比
  7. /*if(digitalRead(motor_A) == HIGH){
  8. if(digitalRead(motor_B) == LOW){//A 高 B 低
  9. count++;
  10. } else {//A 高 B 高
  11. count--;
  12. }
  13. }*/
  14. //2倍频计数实现
  15. //手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比 * 2
  16. if(digitalRead(motor_A) == HIGH){
  17. if(digitalRead(motor_B) == HIGH){//A 高 B 高
  18. count++;
  19. } else {//A 高 B 低
  20. count--;
  21. }
  22. } else {
  23. if(digitalRead(motor_B) == LOW){//A 低 B 低
  24. count++;
  25. } else {//A 低 B 高
  26. count--;
  27. }
  28. }
  29. }
  30. //与A实现类似
  31. //4倍频计数实现
  32. //手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比 * 4
  33. void count_B(){
  34. if(digitalRead(motor_B) == HIGH){
  35. if(digitalRead(motor_A) == LOW){//B 高 A 低
  36. count++;
  37. } else {//B 高 A 高
  38. count--;
  39. }
  40. } else {
  41. if(digitalRead(motor_A) == HIGH){//B 低 A 高
  42. count++;
  43. } else {//B 低 A 低
  44. count--;
  45. }
  46. }
  47. }
  48. void setup() {
  49. Serial.begin(57600);//设置波特率
  50. pinMode(motor_A,INPUT);
  51. pinMode(motor_B,INPUT);
  52. attachInterrupt(2,count_A,CHANGE);//当电平发生改变时触发中断函数
  53. //四倍频统计需要为B相也添加中断
  54. attachInterrupt(3,count_B,CHANGE);
  55. }
  56. int reducation = 90;//减速比,根据电机参数设置,比如 15 | 30 | 60
  57. int pulse = 11; //编码器旋转一圈产生的脉冲数该值需要参考商家电机参数
  58. int per_round = pulse * reducation * 4;//车轮旋转一圈产生的脉冲数
  59. long start_time = millis();//一个计算周期的开始时刻,初始值为 millis();
  60. long interval_time = 50;//一个计算周期 50ms
  61. double current_vel;
  62. //获取当前转速的函数
  63. void get_current_vel(){
  64. long right_now = millis();
  65. long past_time = right_now - start_time;//计算逝去的时间
  66. if(past_time >= interval_time){//如果逝去时间大于等于一个计算周期
  67. //1.禁止中断
  68. noInterrupts();
  69. //2.计算转速 转速单位可以是秒,也可以是分钟... 自定义即可
  70. current_vel = (double)count / per_round / past_time * 1000 * 60;
  71. //3.重置计数器
  72. count = 0;
  73. //4.重置开始时间
  74. start_time = right_now;
  75. //5.重启中断
  76. interrupts();
  77. Serial.println(current_vel);
  78. }
  79. }
  80. void loop() {
  81. delay(10);
  82. get_current_vel();
  83. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/656459
推荐阅读
相关标签
  

闽ICP备14008679号