This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteractive.ino
More file actions
76 lines (61 loc) · 1.78 KB
/
interactive.ino
File metadata and controls
76 lines (61 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <ros.h>
#include <softgrasp_ros/SetGraspState.h>
#include <softgrasp_ros/SetGraspParams.h>
ros::NodeHandle nh;
using softgrasp_ros::SetGraspState;
using softgrasp_ros::SetGraspParams;
// status indicator
const int LED = 13;
// output
const int TRIGGER= 9;
const int MODE_SEL = 5;
const int OPENING = 10;
const int GRIP = 3;
// input
const int PRESSURE_IN = A1;
void grasp(bool state) {
digitalWrite(TRIGGER, state ? HIGH : LOW);
}
void state_cb(const SetGraspState::Request &req, SetGraspState::Response &res) {
digitalWrite(LED, req.state ? HIGH : LOW);
grasp(req.state);
res.success = true;
res.message = "done";
}
void setparams(float opening, float grip) {
// o = 3.1374*opening + 1.5687
// g = 4.1832*grip + 1.0458
// pin = int(round(o*255/5)) = int(o*51 + 0.5)
opening = 160.0074*opening + 80.5037;
grip = 213.3432*grip + 53.8358;
analogWrite(OPENING, constrain(int(opening), 0, 255));
analogWrite(GRIP, constrain(int(grip), 0, 255));
}
void params_cb(const SetGraspParams::Request &req, SetGraspParams::Response &res) {
setparams(req.opening_amount, req.grip_strength);
res.success = true;
res.message = "done";
}
// service servers
ros::ServiceServer<SetGraspState::Request, SetGraspState::Response> state_server("grasp_state", &state_cb);
ros::ServiceServer<SetGraspParams::Request, SetGraspParams::Response> params_server("grasp_params", ¶ms_cb);
void setup()
{
pinMode(LED, OUTPUT);
pinMode(TRIGGER, OUTPUT);
pinMode(MODE_SEL, OUTPUT);
pinMode(OPENING, OUTPUT);
pinMode(GRIP, OUTPUT);
pinMode(PRESSURE_IN, INPUT);
digitalWrite(MODE_SEL, HIGH);
grasp(false);
setparams(0.5, 0.5);
nh.initNode();
nh.advertiseService(state_server);
nh.advertiseService(params_server);
}
void loop()
{
nh.spinOnce();
delay(33);
}