Controlling the Timing of an Actuators Motion
by Progressive Automations in Circuits > Arduino
1570 Views, 1 Favorites, 0 Comments
Controlling the Timing of an Actuators Motion
In this Instructable, we'll be learning how to control the timing of a linear actuator, using a MegaMoto motor control shield.
For this project, you will need:
- 1x RobotPower MegaMoto board
- 1x Arduino Uno
- 1x Linear Actuator
- 1x 12V power supply (10 amps minimum)
Wiring the Actuator
In this step we'll be connecting all of our hardware together.
1) Connect the MegaMoto shield to the Arduino
2) Attach the two leads of the actuator to the A/B screw terminals on the MegaMoto
3) Connect the positive and negative wires of the power supply to the BAT +/-
4) If you are using a power supply of 12V or less, connect a wire from the BAT + to the Vin pin on the Arduino to power the board. Since the MegaMoto is a shield, the GNDs are already connected. If you are using a bigger power supply, you need to power the Arduino with another power supply. Either 5V directly, or 7-12V on the Vin pin.
Programming the Arduino
Below is an example code showing how to move the actuator. In this example code we have the actuator extending for 10 seconds, then retracting for 10 seconds. This will repeat for 300 seconds.
See Step 3 to set the jumpers on the board.
See Step 4 to learn to modify the code.
/* This code modifies the MegaMoto example code to control an actuator with no potentiometer. The actuator will extend for 10 seconds, and immediately retract for 10 seconds. This process repeats over 300 seconds. Modified by Progressive Automations, using the original example code for the MegaMoto board from: <a href="http://www.robotpower.com/downloads/" rel="nofollow"> http://www.robotpower.com/downloads/</a> Hardware: - 1 RobotPower MegaMoto control board - Arduino Uno Wiring: -Connect the +/- of the actuator to the A/B motor channels -Connect the +/- of the power supply to the +/- BAT connections This example code is in the public domain. */ //Use the jumpers on the board to select which pins will be used int EnablePin1 = 13; int PWMPinA1 = 11; int PWMPinB1 = 3; int extendtime = 10 * 1000; // 10 seconds, times 1000 to convert to milliseconds int retracttime = 10 * 1000; // 10 seconds, times 1000 to convert to milliseconds int timetorun = 300 * 1000; // 300 seconds, times 1000 to convert to milliseconds int duty; int elapsedTime; boolean keepMoving; void setup() { Serial.begin(9600); pinMode(EnablePin1, OUTPUT);//Enable the board pinMode(PWMPinA1, OUTPUT); pinMode(PWMPinB1, OUTPUT);//Set motor outputs elapsedTime = 0; // Set time to 0 keepMoving = true; //The system will move }//end setup void loop() { if (keepMoving) { digitalWrite(EnablePin1, HIGH); // enable the motor pushActuator(); delay(extendtime); stopActuator(); delay(10);//small delay before retracting pullActuator(); delay(retracttime); stopActuator(); elapsedTime = millis();//how long has it been? if (elapsedTime > timetorun) {//if it's been 300 seconds, stop Serial.print("Elapsed time is over max run time. Max run time: "); Serial.println(timetorun); keepMoving = false; } }//end if }//end main loop void stopActuator() { analogWrite(PWMPinA1, 0); analogWrite(PWMPinB1, 0); // speed 0-255 } void pushActuator() { analogWrite(PWMPinA1, 255); analogWrite(PWMPinB1, 0); // speed 0-255 } void pullActuator() { analogWrite(PWMPinA1, 0); analogWrite(PWMPinB1, 255);//speed 0-255 }
MegaMoto Control Pins
In this step we'll be configuring the pins on the MegaMoto, based on the pins chosen in the code.
To choose which pins the Arduino communicates with the on the MegaMoto, the jumpers on the MegaMoto must be set.
See the MegaMoto instructable right here for a detailed description of the jumpers.
For our example, we are using the following pins:
Enable: D13
PWMA/B: D11/D3
SENSOR: Not used, doesn't matter
Testing and Modifying Code
In this step we will explain each section of the code, so you can modify it to your needs.
The first bit of code we can change is the pins used on the MegaMoto. We're using pins 13, 11, and 3. You can change these based on what position the jumpers are in on your board.
Next is the extendtime and retracttime. These values are represented in milliseconds, and tell the motor how long to move forwards and backwards.
timetorun is the total time that your program will run. It is also expressed in milliseconds.
In void setup(), we setup the Arduino board and tell it what is connected to each pin.
In the main loop, void loop(), the first instruction we have is checking if the motor is supposed to move. The keepMoving variable is dependent on timetorun. If the program has already run for greater then the timetorun, the motor will not move.
Inside this loop, the MegaMoto board is enabled. The actuator is pushed via a function, and continues to do so for the extendtime. After that delay, the actuator stops. The small delay of 10 milliseconds gives the motor time to stop rotating, to eliminate a voltage spike caused by rapidly changing direction of the motor.
The actuator is then pulled for the retracttime, and stopped.
Now the time is checked. Millis() is a timer that counts how many milliseconds has passed since the Arduino has been powered up. It is used to check if you have run longer then the timetorun, and will tell the program to stop.
The last check is to see if elapsedTime is greater then the timetorun. If the elapsedTime is smaller, then the program repeats. If the elapsedTime is greater, a message is printed to the serial monitor, and the variable keepMoving is turned false so that the motor no longer moves.
That's the whole program!
Conclusion
By using this example, you can make the actuator extend and retract for any length of time. The actuator will still stop when it reaches the limit switches, but the program will only change the direction of the actuator once the set time has passed.
By modifying this code, you can adjust the speed of the motor, the amount it extends and retracts by modifying the time, and the total time for it to run. These code snippets can be combined with other examples in our other instructables to make your project however you like.
If you'd like to take a look at our selection of linear actuators, motions control systems and microcontrollers then please visit us at http://www.progressiveautomations.com/ for all your actuator needs! We can even build a custom actuator or control system for you based on your own custom specifications with the help of our highly trained staff of engineers! You can learn more about the custom order process right here.