Home > other >  Arduino write what kind of program to control stepping motor fixed distance?
Arduino write what kind of program to control stepping motor fixed distance?

Time:04-13

I recently doing a project car, the car is made up of four step motor control, using the glossy wheel, so I need to write a paragraph in the arduino program to control the step motor walk a fixed distance, at first I wrote a program to implement the basic mobile trolley, vision is by controlling the step motor makes the car forward and backward, and then to the left, and finally to the right back to the starting point, and finally down as a result of the experiment, if not add about sports, the car can only exercise before and after, if add about sports, theory should be able to achieve around before and after the exercise, but the result is the only move around, think for a long time, I think may be the cause of the steps, but don't know how to modify, so ask bosses, hope leaders can point mistakes and the insufficiency, the code is as follows:

#include

//define motor control with a constant
Const int enablePin=8;

Const int x_Step_Pin=2;
Const int x_Dir_Pin=5;
Const int y_Step_Pin=3;
Const int y_Dir_Pin=6;
Const int z_Step_Pin=4;
Const int z_Dir_Pin=7;
Const int a_Step_Pin=12;
Const int a_Dir_Pin=13;

Walk//define how much
Const int moveSteps=1600;

//define a time
Const int delayTime=200;
Int Speed=100;

//object definition motor
AccelStepper stepperx (1, x_Step_Pin, x_Dir_Pin);
AccelStepper steppery (1, y_Step_Pin, y_Dir_Pin);
AccelStepper stepperz (1, z_Step_Pin, z_Dir_Pin);
AccelStepper steppera (1, a_Step_Pin, a_Dir_Pin);

Void setup () {
PinMode (x_Step_Pin, the OUTPUT);
PinMode (x_Dir_Pin, the OUTPUT);
PinMode (y_Step_Pin, the OUTPUT);
PinMode (y_Dir_Pin, the OUTPUT);
PinMode (z_Step_Pin, the OUTPUT);
PinMode (z_Dir_Pin, the OUTPUT);
PinMode (a_Step_Pin, the OUTPUT);
PinMode (a_Dir_Pin, the OUTPUT);

PinMode (enablePin, the OUTPUT);
DigitalWrite (enablePin, LOW);



//set the maximum speed and acceleration
Stepperx. SetMaxSpeed (1000);
Stepperx. SetAcceleration (200);
Steppery. SetMaxSpeed (1000);
Steppery. SetAcceleration (200);
Stepperz. SetMaxSpeed (1000);
Stepperz. SetAcceleration (200);
Steppera. SetMaxSpeed (1000);
Steppera. SetAcceleration (200);
}


Void loop () {

MFord ();

MBack ();

MLeft ();

MRight ();


//motor sports
Stepperx. The run ();
Steppery. The run ();
Stepperz. The run ();
Steppera. The run ();

}

Void MFord () {//forward
If (stepperx currentPosition ()==0) {
Stepperx. MoveTo (moveSteps);
}
If (steppery currentPosition ()==0) {
Steppery. MoveTo (- moveSteps);
}
If (stepperz currentPosition ()==0) {
Stepperz. MoveTo (moveSteps);
}
If (steppera currentPosition ()==0) {
Steppera. MoveTo (- moveSteps);
}
return 0;
}

Void MBack () {//backward
If (stepperx currentPosition ()==moveSteps) {
Stepperx. MoveTo (0);
}
If (steppery currentPosition ()==- moveSteps) {
Steppery. MoveTo (0);
}
If (stepperz currentPosition ()==moveSteps) {
Stepperz. MoveTo (0);
}
If (steppera currentPosition ()==- moveSteps) {
Steppera. MoveTo (0);
}
return 0;
}

Void MLeft () {//to the
If (stepperx currentPosition ()==0) {
Stepperx. MoveTo (- moveSteps);
}
If (steppery currentPosition ()==0) {
Steppery. MoveTo (- moveSteps);
}
If (stepperz currentPosition ()==0) {
Stepperz. MoveTo (moveSteps);
}
If (steppera currentPosition ()==0) {
Steppera. MoveTo (moveSteps);
}
return 0;
}

Void MRight () {//to the right
If (stepperx currentPosition ()==- moveSteps) {
Stepperx. MoveTo (0);
}
If (steppery currentPosition ()==- moveSteps) {
Steppery. MoveTo (0);
}
If (stepperz currentPosition ()==moveSteps) {
Stepperz. MoveTo (0);
}
If (steppera currentPosition ()==moveSteps) {
Steppera. MoveTo (0);
}
return 0;
}
}

In addition I still to go how to control the step motor fixed distance feel confused, the principle of stepping motor, calculation formula of the distance I have access to related data, also saw video of some related, but just don't know how to write a program that would like to ask you, how to write a program?
  • Related