-
Notifications
You must be signed in to change notification settings - Fork 0
1.7 Actuator Code Introduction
Congratulations on reaching this stage in your embedded systems tutorial! In this section, we'll discuss the actuator board code that controls a servo motor for opening and closing operations. This tutorial builds upon the fundamental concepts you've learned so far, including for loops, functions, and analog reading.
The actuator board is designed to control multiple mechanisms, including a grabber, a dropper, and a torpedo. For now, we have physically implemented a grabber/dropper mechanism using a single servo. This functionality is critical for our competition, as we need to be able to physically grab and drop items underwater.
Here's the simplified code for the actuator board:
#include <Servo.h>
#define CURRENT_PIN 16
#define SERVO_PIN 11
Servo servo1; // Create servo object to control a servo
void setup()
{
Serial.begin(9600); // Initialize serial communication
pinMode(CURRENT_PIN, INPUT); // Set current sensing pin as input
servo1.attach(SERVO_PIN); // Attach the servo to the specified pin
}
void loop()
{
// Call open and close functions as needed
open_actuator();
delay(2000); // Wait for 2 seconds
close_actuator();
delay(2000); // Wait for 2 seconds
}
// Function to close the actuator
void close_actuator() {
for (int pos = 0; pos <= 180; pos += 1) { // Move from 0 to 180 degrees
servo1.write(pos); // Move servo to position
print_current(); // Print current reading
delay(15); // Wait for servo to reach position
}
}
// Function to open the actuator
void open_actuator() {
for (int pos = 180; pos >= 0; pos -= 1) { // Move from 180 to 0 degrees
servo1.write(pos); // Move servo to position
print_current(); // Print current reading
delay(15); // Wait for servo to reach position
}
}
// Function to print the current reading
void print_current() {
int currentReading = analogRead(CURRENT_PIN); // Read current
Serial.println(currentReading); // Print current reading to the serial monitor
}
We include the Servo.h library to simplify controlling the servo motor. Libraries provide pre-written code that helps streamline your projects. For more details on using libraries, check out this section.
The setup() function initializes the serial communication for debugging and configures the pin modes. The servo is attached to a specified pin.
The loop() function repeatedly calls the open_actuator() and close_actuator() functions, with a delay in between to allow the servo to complete its movements.
The open_actuator() and close_actuator() functions control the servo's movement from 0 to 180 degrees and vice versa. Each iteration prints the current reading to the serial monitor using the print_current() function.
The print_current() function uses analogRead() to read the current from a specified pin. This pin is essential because when the current reading is high, it indicates that the servo has closed, signaling that it’s time to stop sending commands to further close the servo. For more information on analog reading, refer to this section.
While this code is simplified, it serves as an abstraction of how the actuator board operates. At its core, we are controlling a servo motor and telling it to open and close, allowing us to manipulate objects effectively. As you progress, you'll encounter more complex functions and systems, but understanding these basics will give you a solid foundation for future challenges. If you have any questions or need assistance, please reach out to a lead or senior member of the team.
Happy coding!