[Arduino模块]
EMG49 驱动MD49 24V 电机
电路示意:
代码示例:
- /*********************************************
- * Arduino example code for MD49 *
- * Using LCD03 in serial mode to display data *
- * *
- * By James Henderson, 2012. *
- *********************************************/
-
- #include <SoftwareSerial.h>
-
- // Values of 0 being sent over serial write have to be cast as a byte so they arent misinterpreted as NULL
- // This is a bug with arduino 1.0
- #define CMD (byte)0x00 // MD49 command address of 0
- #define GET_VER 0x29
- #define GET_ENC1 0x23
- #define GET_ENC2 0x24
- #define GET_VI 0x2C
- #define GET_ERROR 0x2D
- #define SET_ACCEL 0x33
- #define SET_SPEED1 0x31
- #define SET_SPEED2 0x32
-
- #define LCD_RX 0x02 // RX and TX pins used for LCD0303 serial port
- #define LCD_TX 0x03
- #define LCD03_HIDE_CUR 0x04
- #define LCD03_HOME 0x01
- #define LCD03_CLEAR 0x0C
- #define LCD03_SET_CUR 0x02
-
- SoftwareSerial lcd03 = SoftwareSerial(LCD_RX, LCD_TX); // Creates a serial port for the LCD03
-
- byte enc1a, enc1b, enc1c, enc1d = 0;
- byte enc2a, enc2b, enc2c, enc2d = 0;
- byte bat_volt, mot1_cur, mot2_cur = 0;
- byte ver = 0;
- byte error = 0;
-
- void setup()
- {
- Serial.begin(38400);
- lcd03.begin(9600);
-
- lcd03.write(LCD03_CLEAR);
- lcd03.write(LCD03_HIDE_CUR);
-
- }
-
- void loop()
- {
- Serial.write(CMD); // command byte
- Serial.write(SET_ACCEL);
- Serial.write(5); // Set accelleration to 5
- Serial.write(CMD);
- Serial.write(SET_SPEED1);
- Serial.write(140); // Set motor 1 speed
- Serial.write(CMD);
- Serial.write(SET_SPEED2);
- Serial.write(140); // Set motor 2 speed
-
- Serial.write(CMD);
- Serial.write(GET_VER); // Recieve version back
- delay(50);
- if(Serial.available() > 0)
- {
- ver = Serial.read();
- }
-
- Serial.write(CMD);
- Serial.write(GET_ERROR); // Recieve error byte back
- delay(50);
- if(Serial.available() > 0)
- {
- error = Serial.read();
- }
-
- Serial.write(CMD);
- Serial.write(GET_VI); // Recieve battery volts and both motor currents back
- delay(50);
- if(Serial.available() > 2)
- {
- bat_volt = Serial.read();
- mot1_cur = Serial.read();
- mot2_cur = Serial.read();
- }
-
- Serial.write(CMD);
- Serial.write(GET_ENC1); // Recieve encoder 1 value
- delay(50);
- if(Serial.available() > 3)
- {
- enc1a = Serial.read();
- enc1b = Serial.read();
- enc1c = Serial.read();
- enc1d = Serial.read();
- }
-
- Serial.write(CMD);
- Serial.write(GET_ENC2); // Recieve encoder 2 value
- delay(50);
- if(Serial.available() > 3)
- {
- enc2a = Serial.read();
- enc2b = Serial.read();
- enc2c = Serial.read();
- enc2d = Serial.read();
- }
-
- lcd03.write(LCD03_HOME); // Return cursor home
-
- lcd03.print("Rev: "); // Display data to the screen
- lcd03.print(ver,DEC);
- lcd03.print(" Battery V: ");
- lcd03.print(bat_volt, DEC);
- lcd03.write(LCD03_SET_CUR); // Set position of cursor
- lcd03.write(21);
-
- delay(5); // Delay to allow LCD03 time to process data
-
- lcd03.print("Encoder 1:");
- lcd03.print(enc1a,HEX);
- lcd03.print(enc1b,HEX);
- lcd03.print(enc1c,HEX);
- lcd03.print(enc1d,HEX);
- lcd03.write(LCD03_SET_CUR);
- lcd03.write(41);
-
- delay(5);
-
- lcd03.write("Encoder 2:");
- lcd03.print(enc2a,HEX);
- lcd03.print(enc2b,HEX);
- lcd03.print(enc2c,HEX);
- lcd03.print(enc2d,HEX);
- lcd03.write(LCD03_SET_CUR);
- lcd03.write(61);
-
- delay(5);
-
- lcd03.print("Mot 1 I:");
- lcd03.print(mot1_cur,DEC);
- lcd03.print(" Mot 2 I:");
- lcd03.print(mot2_cur,DEC);
- lcd03.print("");
-
- delay(5);
-
- }
-
复制代码
|
|
|
|
|
|