2013-12-18 14:46:35 [显示全部楼层]
5545浏览
查看: 5545|回复: 1

[转载] [Arduino模块] MD25 串行驱动RD02电机

[复制链接]
[Arduino模块]
MD25 串行驱动RD02电机

电路示意:



代码示例:
  1. /****************************************************************
  2. *                    Arduino MD25 example code                  *
  3. *                   MD25 running in serial mode                 *
  4. *                                                               *
  5. *                     by James Henderson 2012                   *
  6. *****************************************************************/
  7. #include <SoftwareSerial.h>
  8. // Values of 0 being sent using serial.write() have to be cast as a byte to stop them being misinterpereted as NULL
  9. // This is a bug with arduino 1
  10. #define CMD                 (byte)0x00              //  MD25 command byte of 0
  11. #define WRITESP1            0x31                    // Byte writes speed to motor 1
  12. #define WRITEACCEL          0x33                    // Byte writes a value of acceleration
  13. #define RESETREG            0x35                    // Byte resets encoders
  14. #define SETMODE             0x34                    // Byte sets mode
  15. #define READIVS             0x2C                    // Byte reads motor currents and battery voltage        
  16. #define READENCS            0x25                    // Byte reads both encoders
  17. #define GET_VER             0x29
  18. #define LCD_RX              0x02                    // RX and TX pins used for LCD0303 serial port
  19. #define LCD_TX              0x03
  20. #define LCD03_HIDE_CUR      0x04
  21. #define LCD03_CLEAR         0x0C
  22. #define LCD03_SET_CUR       0x02
  23. SoftwareSerial lcd03 = SoftwareSerial(LCD_RX, LCD_TX);          // Define the serial port for the LCD03
  24. long encValue = 0;
  25. byte softwareRev = 0;
  26. void setup(){
  27.   Serial.begin(38400);
  28.   lcd03.begin(9600);
  29.   
  30.   Serial.write(CMD);                                            // Set MD25 accelleration value
  31.   Serial.write(WRITEACCEL);
  32.   Serial.write(10);
  33.   delayMicroseconds(10);                                        // Wait for this to be processed
  34.   Serial.write(CMD);                                            // Reset the encoder registers to 0
  35.   Serial.write(RESETREG);         
  36.   Serial.write(CMD);                                            // Set mode to 2, Both motors controlled by writing to speed 1
  37.   Serial.write(SETMODE);
  38.   Serial.write(2);   
  39.   
  40.   Serial.write(CMD);                                            // Get software version of MD25
  41.   Serial.write(GET_VER);
  42.   while(Serial.available() < 1){}                               // Wait for byte to become available         
  43.   softwareRev = Serial.read();  
  44.   
  45.   lcd03.write(LCD03_CLEAR);                                     // Clear the LCD03 screen
  46.   lcd03.write(LCD03_HIDE_CUR);                                  // Hide the LCD03 cursor
  47. }
  48. void loop(){
  49.   while(encValue < 3000){               // While encoder 1 value less than 3000 move forward
  50.     Serial.write(CMD);                  // Set motors to drive forward at full speed
  51.     Serial.write(WRITESP1);
  52.     Serial.write(150);
  53.     encValue = readEncoder();
  54.     readVolts();
  55.   }
  56.   delay(100);
  57.   while(encValue > 100){
  58.     Serial.write(CMD);                  // Drive motors reverse at full speed
  59.     Serial.write(WRITESP1);
  60.     Serial.write(100);
  61.     encValue = readEncoder();
  62.     readVolts();
  63.   }
  64.   delay(100);
  65. }
  66. long readEncoder(){                        // Function to read and display the value of both encoders, returns value of first encoder
  67.   long result1 = 0;
  68.   long result2 = 0;
  69.   Serial.write(CMD);
  70.   Serial.write(READENCS);
  71.   while(Serial.available() < 8){}          // Wait for 8 bytes, first 4 encoder 1 values second 4 encoder 2 values
  72.   result1 = Serial.read();                 // First byte for encoder 1, HH.
  73.   result1 <<= 8;
  74.   result1 += Serial.read();                // Second byte for encoder 1, HL
  75.   result1 <<= 8;
  76.   result1 += Serial.read();                // Third byte for encoder 1, LH
  77.   result1 <<= 8;
  78.   result1  += Serial.read();               // Fourth byte for encoder 1, LL
  79.   result2 = Serial.read();
  80.   result2 <<= 8;
  81.   result2 += Serial.read();
  82.   result2 <<= 8;
  83.   result2 += Serial.read();
  84.   result2 <<= 8;
  85.   result2 += Serial.read();
  86.   lcd03.write(LCD03_SET_CUR);              // Set the LCD03 cursor position
  87.   lcd03.write(21);
  88.   lcd03.print("Encoder 1:");               // Displays data to the LCD03 screen
  89.   lcd03.print(result1,DEC);
  90.   lcd03.print(" ");                        // Print a blank space to clear any unwanted characters that are leftover on the LCD03 display
  91.   
  92.   delay(5);                                // Delay for LCD03 to process data
  93.   
  94.   lcd03.write(LCD03_SET_CUR);
  95.   lcd03.write(41);
  96.   lcd03.print("Encoder 2:");
  97.   lcd03.print(result2,DEC);
  98.   lcd03.print(" ");
  99.   return result1;                                   
  100. }
  101.   
  102. void readVolts(){                                                 // Function reads current for both motors and battery voltage
  103.   byte batteryVolts, mot1_current, mot2_current = 0;
  104.   Serial.write(CMD);
  105.   Serial.write(READIVS);                                          // Send byte to readbattery voltage and motor currents
  106.   while(Serial.available() < 3){}                                 // Wait for the 3 bytes to become available then get them
  107.   batteryVolts = Serial.read();
  108.   mot1_current = Serial.read();
  109.   mot2_current = Serial.read();
  110.   lcd03.write(LCD03_SET_CUR);
  111.   lcd03.write(61);
  112.   lcd03.print("Mot1 I:");
  113.   lcd03.print(mot1_current,DEC);
  114.   lcd03.print(" Mot2 I:");
  115.   lcd03.print(mot2_current,DEC);
  116.   lcd03.print(" ");
  117.   
  118.   delay(5);
  119.   
  120.   lcd03.write(LCD03_SET_CUR);
  121.   lcd03.write(1);
  122.   lcd03.print("Rev:");
  123.   lcd03.print(softwareRev, DEC);
  124.   lcd03.print(" ");
  125.   lcd03.print("Volts:");
  126.   lcd03.print(batteryVolts/10,DEC);                               // Seperate whole number and descimal place of battery voltage and display
  127.   lcd03.print(".");  
  128.   lcd03.print(batteryVolts%10,DEC);
  129.   lcd03.print(" ");   
  130. }
复制代码




20060606  高级技匠

发表于 2020-8-25 05:27:44

图好像一直加载不出来
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail