Appendix
#include <stdint.h>
#include <stdbool.h>
// Actuator Pins
#define MOTOR_FWD_PIN 8
#define MOTOR_BWD_PIN 9
#define MOTOR_ENA_PIN 10
typedef struct ActuatorOut {
uint8_t fwd_pin;
uint8_t bwd_pin;
uint8_t ena_pin;
} AcuatorOut;
const ActuatorOut MOTOR_PINS = {MOTOR_FWD_PIN, MOTOR_BWD_PIN, MOTOR_ENA_PIN};
#define POTENTIOMETER_PIN A5
//------------ActuatorInit-----------
// Function to initialize the direction and enable pins of an actuator.
// Inputs: actuator struct representing actuator configuration
// Outputs: none
void ActuatorInit(const ActuatorOut *actuator) {
pinMode((*actuator).fwd_pin, OUTPUT);
pinMode((*actuator).bwd_pin, OUTPUT);
pinMode((*actuator).ena_pin, OUTPUT);
}
//------------ClipValue-----------
// Function to bound values between two limits. If value is higher/lower than a limit, it is set to that limit.
// Inputs: *val pointer to current value
// upper_lim upper limit for value
// lower_lim lower limit for value
// Outputs: none
void ClipValue(int16_t *val, int16_t lower_lim, int16_t upper_lim) {
(*val) = (*val > upper_lim) ? upper_lim : *val;
(*val) = (*val < lower_lim) ? lower_lim : *val;
}
//------------SetActuator-----------
// Function to set the direction and magnitude of an actuator. Negative magnitude implies
// Counter-Clockwise rotation for a motor and opposing polarity for an electromagnet
// Inputs: mag magnitude of actuator effort between -255 and 255
// actuator struct representing actuator configuration
// Outputs: none
void SetActuator(int16_t mag, const ActuatorOut *actuator) {
// Clip to 8-bit value
ClipValue(&mag, -255, 255);
if (mag > 0) { // Forward Direction
digitalWrite((*actuator).bwd_pin, LOW);
digitalWrite((*actuator).fwd_pin, HIGH);
}
else if (mag < 0) { // Backward Direction
digitalWrite((*actuator).fwd_pin, LOW);
digitalWrite((*actuator).bwd_pin, HIGH);
}
else { // Zero Magnitude
digitalWrite((*actuator).fwd_pin, LOW);
digitalWrite((*actuator).bwd_pin, LOW);
}
// Send actuator command
analogWrite((*actuator).ena_pin, abs(mag));
}
void setup() {
ActuatorInit(&MOTOR_PINS);
}
void loop() {
SetActuator((analogRead(POTENTIOMETER_PIN) - 512)/2, &MOTOR_PINS);
}
HAND WRITTEN WORK
Welcome to the University Wiki Service! Please use your IID (yourEID@eid.utexas.edu) when prompted for your email address during login or click here to enter your EID. If you are experiencing any issues loading content on pages, please try these steps to clear your browser cache.