Skip to content

Commit 6f93ebd

Browse files
authored
Actuator Embedded Code and Tests #184
Actuator testing
2 parents 0b516a1 + c56c4d4 commit 6f93ebd

File tree

8 files changed

+516
-4
lines changed

8 files changed

+516
-4
lines changed

pio_workspace/src/actuator_main.cpp

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,81 @@
22

33
#include "actuator_main.h"
44

5-
void actuator_setup(){
6-
// put your setup code here, to run once:
5+
#include <Arduino.h>
6+
7+
#include <Servo.h>
8+
9+
#include <ros.h>
10+
#include <std_msgs/UInt16.h>
11+
#include <std_msgs/Bool.h>
12+
13+
// servo pin definition
14+
const int SERVO_PIN = 9;
15+
16+
// create node handle
17+
ros::NodeHandle nh;
18+
19+
// create grabber servo object
20+
Servo grabberServo;
21+
22+
// function prototypes
23+
void servoPosition_CB(const std_msgs::UInt16& msg);
24+
void servoSweep_CB(const std_msgs::Bool& msg);
25+
26+
// subscribe to topics
27+
ros::Subscriber<std_msgs::UInt16> grabberPositionSub("servo/position", &servoPosition_CB);
28+
ros::Subscriber<std_msgs::Bool> grabberSweepSub("servo/sweep", &servoSweep_CB);
29+
30+
// setup function
31+
void actuator_setup() {
32+
// pwm teensy pin connected to grabber
33+
pinMode(SERVO_PIN, OUTPUT);
34+
// attach servo object to teensy pin and check for success
35+
if (grabberServo.attach(SERVO_PIN)) {
36+
// servo attached successfully
37+
} else {
38+
// servo attachment failed - could add LED blink or error handling here
39+
}
40+
// initialize ros node
41+
nh.initNode();
42+
// subscribe to ros topics
43+
nh.subscribe(grabberPositionSub);
44+
nh.subscribe(grabberSweepSub);
45+
}
46+
47+
// check for new messages with 1ms delay
48+
void actuator_loop() {
49+
nh.spinOnce();
50+
delay(1);
51+
}
52+
53+
// move grabber servo to position specified by ros message
54+
void servoPosition_CB(const std_msgs::UInt16& msg) {
55+
// constrain servo position to valid range (0-180 degrees)
56+
int position = constrain(msg.data, 0, 180);
57+
grabberServo.write(position);
758
}
859

9-
void actuator_loop(){
10-
// put your main code here, to run repeatedly:
60+
// sweep servo based on boolean message
61+
void servoSweep_CB(const std_msgs::Bool& msg) {
62+
if (msg.data) {
63+
// sweep from 0 to 180 degrees
64+
for (int pos = 0; pos <= 180; pos += 1) {
65+
grabberServo.write(pos);
66+
delay(15);
67+
// check for new messages during sweep to maintain some responsiveness
68+
nh.spinOnce();
69+
}
70+
}
71+
else {
72+
// sweep from 180 to 0 degrees
73+
for (int pos = 180; pos >= 0; pos -= 1) {
74+
grabberServo.write(pos);
75+
delay(15);
76+
// check for new messages during sweep to maintain some responsiveness
77+
nh.spinOnce();
78+
}
79+
}
1180
}
1281

1382
#endif
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
// PRINTS CURRENT READINGS FROM MOTOR RUNNING OFF J3
3+
4+
#include <Servo.h>
5+
#define CURRENT_PIN 17 //CHANGED FROM 16
6+
#define SERVO_PIN 10
7+
#define GREEN_LED 15
8+
#define RED_LED 14
9+
10+
11+
// variables
12+
int contact_current = 400;
13+
int max_position = 180;
14+
int pos = 0; // variable to store the servo position
15+
float current = 0;
16+
17+
18+
19+
// create servo object
20+
Servo servo1; // create servo object to control a servo
21+
// a maximum of eight servo objects can be created
22+
23+
24+
unsigned long previousMillis = 0; // stores last time the servo was updated
25+
unsigned long previousServoMillis = 0; // stores last time the servo was updated
26+
const long interval = 1000; // interval at which to switch direction (milliseconds)
27+
const long servoInterval = 2;
28+
29+
bool direction = false;
30+
31+
void setup()
32+
{
33+
34+
// set data rate
35+
Serial.begin(9600);
36+
37+
// set i/o pins
38+
pinMode(CURRENT_PIN, INPUT); // pin16 is IC input
39+
pinMode(LED_BUILTIN, OUTPUT); // builtin LED on teesny
40+
servo1.attach(SERVO_PIN); // servo to control grabber
41+
}
42+
43+
44+
void loop()
45+
{
46+
47+
close_actuator();
48+
49+
}
50+
51+
void close_actuator() {
52+
for (pos = 0; pos <= max_position; pos += 1) { // position maximum should be set to whatever would see grabber fully closed
53+
54+
// check current reading
55+
int currentReading = analogRead(CURRENT_PIN);
56+
Serial.println(currentReading);
57+
58+
// use current reading to check if made contact
59+
if(currentReading > contact_current){
60+
61+
stop();
62+
63+
// STOP THE SERVO
64+
}
65+
else{ // servo keeps going
66+
67+
digitalWrite(GREEN_LED, HIGH);
68+
}
69+
70+
// move servo
71+
servo1.write(pos); // tell servo to go to position in variable 'pos'
72+
delay(15); // waits 15ms for the servo to reach the position
73+
}
74+
75+
}
76+
77+
void open_actuator() {
78+
for (pos = max_position; pos >= 0; pos -= 1) { // position maximum should be set to whatever would see grabber fully closed
79+
80+
// check current reading
81+
int currentReading = analogRead(CURRENT_PIN);
82+
Serial.println(currentReading);
83+
84+
// use current reading to check if made contact
85+
if(currentReading > contact_current){
86+
87+
digitalWrite(LED_BUILTIN, HIGH);
88+
}
89+
else{ // servo keeps going
90+
91+
digitalWrite(LED_BUILTIN, LOW);
92+
}
93+
94+
// ros thingie to make happen
95+
// move servo
96+
servo1.write(pos); // tell servo to go to position in variable 'pos'
97+
delay(15); // waits 15ms for the servo to reach the position
98+
}}
99+
100+
void stop() {
101+
for (;;) {
102+
digitalWrite(RED_LED, HIGH);
103+
104+
}
105+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// USING SERVO CONNECTED TO ACTUATOR BOARD
2+
3+
#include <Servo.h>
4+
5+
Servo myservo; // create Servo object to control a servo
6+
// twelve Servo objects can be created on most boards
7+
8+
int pos = 0; // variable to store the servo position
9+
10+
void setup() {
11+
myservo.attach(11); // attaches the servo on pin x to the Servo object
12+
Serial.begin(115200); // connection speed (req field for actuator board)
13+
}
14+
15+
16+
void loop() {
17+
for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to x degrees
18+
// in steps of 1 degree
19+
Serial.println("working");
20+
myservo.write(pos); // tell servo to go to position in variable 'pos'
21+
delay(15); // waits 15 ms for the servo to reach the position
22+
}
23+
24+
//for (pos = 300; pos >= 0; pos -= 1) { // goes from x degrees to 0 degrees
25+
// myservo.write(pos); // tell servo to go to position in variable 'pos '
26+
// delay(15); // waits 15 ms for the servo to reach the position
27+
// }
28+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
// PRINTS CURRENT READINGS FROM MOTOR RUNNING OFF J3
3+
4+
#include <Servo.h>
5+
#define CURRENT_PIN 17 //CHANGED FROM 16
6+
#define SERVO_PIN 10
7+
#define GREEN_LED 15
8+
#define RED_LED 14
9+
10+
11+
// variables
12+
int contact_current = 400;
13+
int max_position = 180;
14+
int pos = 0; // variable to store the servo position
15+
float current = 0;
16+
17+
18+
19+
// create servo object
20+
Servo servo1; // create servo object to control a servo
21+
// a maximum of eight servo objects can be created
22+
23+
24+
unsigned long previousMillis = 0; // stores last time the servo was updated
25+
unsigned long previousServoMillis = 0; // stores last time the servo was updated
26+
const long interval = 1000; // interval at which to switch direction (milliseconds)
27+
const long servoInterval = 2;
28+
29+
bool direction = false;
30+
31+
void setup()
32+
{
33+
34+
// set data rate
35+
Serial.begin(9600);
36+
37+
// set i/o pins
38+
pinMode(CURRENT_PIN, INPUT); // pin16 is IC input
39+
pinMode(LED_BUILTIN, OUTPUT); // builtin LED on teesny
40+
servo1.attach(SERVO_PIN); // servo to control grabber
41+
}
42+
43+
44+
void loop()
45+
{
46+
47+
close_actuator();
48+
49+
}
50+
51+
void close_actuator() {
52+
for (pos = 0; pos <= max_position; pos += 1) { // position maximum should be set to whatever would see grabber fully closed
53+
54+
// check current reading
55+
int currentReading = analogRead(CURRENT_PIN);
56+
Serial.println(currentReading);
57+
58+
// use current reading to check if made contact
59+
if(currentReading > contact_current){
60+
61+
stop();
62+
63+
// STOP THE SERVO
64+
}
65+
else{ // servo keeps going
66+
67+
digitalWrite(GREEN_LED, HIGH);
68+
}
69+
70+
// move servo
71+
servo1.write(pos); // tell servo to go to position in variable 'pos'
72+
delay(15); // waits 15ms for the servo to reach the position
73+
pos = pos;
74+
}
75+
76+
}
77+
78+
79+
80+
81+
void stop() {
82+
for (;;) {
83+
digitalWrite(RED_LED, HIGH);
84+
85+
}
86+
}

0 commit comments

Comments
 (0)