Hur man gör en All-riktning fordonet med Mecanum hjul (7 / 8 steg)
Steg 7: programmering
Hastigheten på fyra Mecanum hjul bestäms av vinkelformig hastighet
och hastigheter i x- och y-axeln av fordonet. En Joystick används för att kontrollera hastigheten på plattformen med vänster spak för tröghetsmassanEn hastighet och rätt pinne för vinkelformig hastighet. Anslut en mig USB-värd modul till port 3 av Orion ombord och Anslut sedan en trådlös modul till mig USB-värd modul.
Hämta Makeblock bibliotek och sätter det under Arduino bibliotek. Arduino koderna är följande:
#include "Wire.h"
#include "SoftwareSerial.h"
#include "MeOrion.h"
MeUSBHost joypad(PORT_3);
MeEncoderMotor motor1 (0x02, SLOT2);
MeEncoderMotor motor2 (0x02, SLOT1);
MeEncoderMotor motor3 (0x0A, SLOT2);
MeEncoderMotor motor4 (0x0A, SLOT1);
flyta linearSpeed = 100;
flyta angularSpeed = 100;
flyta maxLinearSpeed = 200.
flyta maxAngularSpeed = 200.
flyta minLinearSpeed = 30.
flyta minAngularSpeed = 30.
void setup()
{
motor1.BEGIN();
motor2.BEGIN();
motor3.BEGIN();
motor4.BEGIN();
Serial.BEGIN(57600);
joypad.init(USB1_0);
}
void loop()
{
Serial.println("loop:");
setEachMotorSpeed (100, 50, 50, 100);
IF(!joypad.device_online)
{
Serial.println ("enheten offline.");
joypad.probeDevice();
Delay(1000);
}
annat
{
int längd = joypad.host_recv();
parseJoystick (joypad. RECV_BUFFER);
Delay(5);
}
Delay(500);
}
void setEachMotorSpeed (flyta ET1 flyta speed2, flyta speed3, flyta speed4)
{
motor1.runSpeed(Speed1);
motor2.runSpeed(-SPEED2);
motor3.runSpeed(-speed3);
motor4.runSpeed(-speed4);
}
void parseJoystick (unsigned char * buf) //Analytic funktion, skriv ut 8 byte från USB-värd
{
debug joystick
int jag = 0;
för (jag = 0; jag < 7; i ++)
// {
Serial.Print(BUF[i]);
Serial.Print('-');
// }
Serial.println(BUF[7]);
Delay(10);
öka och minska hastigheten
Switch (buf[5])
{
fall 1:
linearSpeed += 5.
om (linearSpeed > maxLinearSpeed)
{
linearSpeed = maxLinearSpeed;
}
bryta;
fall 2:
angularSpeed += 5.
om (angularSpeed > maxAngularSpeed)
{
angularSpeed = maxAngularSpeed;
}
bryta;
fall 4:
linearSpeed-= 5.
om (linearSpeed < minLinearSpeed)
{
linearSpeed = minLinearSpeed;
}
bryta;
mål 8:
angularSpeed-= 5.
om (angularSpeed < minAngularSpeed)
{
angularSpeed = minAngularSpeed;
}
bryta;
standard:
bryta;
}
om ((128! = buf[0]) || (127! = buf[1]) || (128! = buf[2]) || (127! = buf[3]))
{
float x = ((float)(buf[2])-127) / 128;
float y = (127 - (float)(buf[3])) / 128;
flyta a = (127 - (float)(buf[0])) / 128;
mecanumRun (x * linearSpeed, y * linearSpeed, en * angularSpeed);
}
annat
{
Switch (buf[4])
{
fall 0:
mecanumRun (0, linearSpeed, 0);
bryta;
fall 4:
mecanumRun (0, - linearSpeed, 0);
bryta;
fall 6:
mecanumRun (-linearSpeed, 0, 0);
bryta;
fall 2:
mecanumRun (linearSpeed, 0, 0);
bryta;
fall 7:
mecanumRun (-linearSpeed/2, linearSpeed/2, 0);
bryta;
fall 5:
mecanumRun (-linearSpeed/2, - linearSpeed/2, 0);
bryta;
fall 1:
mecanumRun (linearSpeed/2, linearSpeed/2, 0);
bryta;
fall 3:
mecanumRun (linearSpeed/2, - linearSpeed/2, 0);
bryta;
standard:
mecanumRun (0, 0, 0);
bryta;
}
}
}
void mecanumRun (flyter xSpeed, flyta ySpeed, flyta aSpeed)
{
flyta ET1 = ySpeed - xSpeed + aSpeed;
flyta speed2 = ySpeed + xSpeed - aSpeed;
flyta speed3 = ySpeed - xSpeed - aSpeed;
flyta speed4 = ySpeed + xSpeed + aSpeed;
flyta max = ET1;
om (max < speed2) max = speed2;
om (max < speed3) max = speed3;
om (max < speed4) max = speed4;
om (max > maxLinearSpeed)
{
ET1 = ET1 / max * maxLinearSpeed;
SPEED2 = speed2 / max * maxLinearSpeed;
speed3 = speed3 / max * maxLinearSpeed;
speed4 = speed4 / max * maxLinearSpeed;
}
setEachMotorSpeed (ET1 speed2, speed3, speed4);
}