Skip to content

Commit ab631b2

Browse files
authored
Merge branch 'main' into PowerBoardRos
2 parents d74ec5d + 44e3dcc commit ab631b2

File tree

1,631 files changed

+251011
-15850
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,631 files changed

+251011
-15850
lines changed

Actuator/src/main.cpp

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,140 @@
1-
#include <Arduino.h>
1+
#include <ros.h>
2+
#include <std_msgs/String.h>
3+
#include <Servo.h>
4+
#include <std_msgs/Float32.h>
5+
#include <std_msgs/Bool.h>
26

3-
// put function declarations here:
4-
int myFunction(int, int);
57

6-
void setup() {
7-
// put your setup code here, to run once:
8-
int result = myFunction(2, 3);
8+
#define CURRENT_PIN 16
9+
#define SERVO_PIN 11
10+
11+
// constant data types
12+
bool contact = 0;
13+
bool closing = 0;
14+
bool new_close_msg = 0;
15+
16+
// variables
17+
int contact_current = 20;
18+
int max_position = 180;
19+
int pos = 0; // variable to store the servo position
20+
float current = 0;
21+
22+
// set messagd types
23+
std_msgs::Bool grabber_contact_msg;
24+
std_msgs::Bool close_msg;
25+
26+
// update var from subscribed message
27+
void update_close_msg( const std_msgs::Bool &close){
28+
closing = close.data;
29+
new_close_msg = true;
30+
}
31+
32+
// set ros pub/subscriber
33+
ros::NodeHandle nh;
34+
ros::Publisher grabber_contact("contact", &grabber_contact_msg);
35+
ros::Subscriber<std_msgs::Bool> sub_close("close", &update_close_msg);
36+
37+
// create servo object
38+
Servo servo1; // create servo object to control a servo
39+
// a maximum of eight servo objects can be created
40+
41+
42+
unsigned long previousMillis = 0; // stores last time the servo was updated
43+
unsigned long previousServoMillis = 0; // stores last time the servo was updated
44+
const long interval = 1000; // interval at which to switch direction (milliseconds)
45+
const long servoInterval = 2;
46+
47+
bool direction = false;
48+
49+
void setup()
50+
{
51+
// set ros
52+
nh.initNode();
53+
nh.subscribe(sub_close);
54+
nh.advertise(grabber_contact);
55+
56+
// set data rate
57+
Serial.begin(9600);
58+
59+
// set i/o pins
60+
pinMode(16, INPUT); // pin16 is IC input
61+
pinMode(LED_BUILTIN, OUTPUT); // builtin LED on teesny
62+
servo1.attach(11); // servo to control grabber
63+
}
64+
65+
66+
void loop()
67+
{
68+
//nh.spinOnce(); // happens in function
69+
70+
if (new_close_msg == 1) {
71+
if (closing == 1) {
72+
close_actuator();
73+
} else if (closing == 0) {
74+
open_actuator();
75+
}
76+
new_close_msg = 0; // clear flag
77+
}
78+
nh.spinOnce();
79+
delay(10);
80+
// check for messages
81+
// if statements
82+
983
}
1084

11-
void loop() {
12-
// put your main code here, to run repeatedly:
85+
void close_actuator() {
86+
for (pos = 0; pos <= max_position; pos += 1) { // position maximum should be set to whatever would see grabber fully closed
87+
88+
// check current reading
89+
int currentReading = analogRead(CURRENT_PIN);
90+
Serial.println(currentReading);
91+
92+
// use current reading to check if made contact
93+
if(currentReading > contact_current){
94+
grabber_contact_msg.data = 1; // contact message is TRUE
95+
grabber_contact.publish( &grabber_contact_msg ); // publish contact message
96+
digitalWrite(LED_BUILTIN, HIGH);
97+
// STOP THE SERVO
98+
}
99+
else{ // servo keeps going
100+
grabber_contact_msg.data = 0; // contact message is FALSE
101+
grabber_contact.publish( &grabber_contact_msg ); // publish contact message
102+
digitalWrite(LED_BUILTIN, LOW);
103+
}
104+
105+
// ros thingie to make happen
106+
nh.spinOnce();
107+
// move servo
108+
servo1.write(pos); // tell servo to go to position in variable 'pos'
109+
delay(15); // waits 15ms for the servo to reach the position
110+
}
111+
13112
}
14113

15-
// put function definitions here:
16-
int myFunction(int x, int y) {
17-
return x + y;
18-
}
114+
void open_actuator() {
115+
for (pos = max_position; pos >= 0; pos -= 1) { // position maximum should be set to whatever would see grabber fully closed
116+
117+
// check current reading
118+
int currentReading = analogRead(CURRENT_PIN);
119+
Serial.println(currentReading);
120+
121+
// use current reading to check if made contact
122+
if(currentReading > contact_current){
123+
grabber_contact_msg.data = 1; // contact message is TRUE
124+
grabber_contact.publish( &grabber_contact_msg ); // publish contact message
125+
digitalWrite(LED_BUILTIN, HIGH);
126+
}
127+
else{ // servo keeps going
128+
grabber_contact_msg.data = 0; // contact message is FALSE
129+
grabber_contact.publish( &grabber_contact_msg ); // publish contact message
130+
digitalWrite(LED_BUILTIN, LOW);
131+
}
132+
133+
// ros thingie to make happen
134+
nh.spinOnce();
135+
// move servo
136+
servo1.write(pos); // tell servo to go to position in variable 'pos'
137+
delay(15); // waits 15ms for the servo to reach the position
138+
}
139+
140+
}

0 commit comments

Comments
 (0)