-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoystick_control.cpp
144 lines (124 loc) · 4.85 KB
/
joystick_control.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include "joystick.hpp"
#include "servos.hpp"
namespace igm = ignition::math;
namespace gzp = gazebo::physics;
namespace gzc = gazebo::common;
namespace gazebo
{
class ModelPush : public ModelPlugin
{
public:
gazebo::common::BatteryPtr bat;
Joystick joy;
igm::Angle Target;
igm::Angle twist;
double Wheels;
double Turn;
ServosController servos;
//gazebo::physics::JointController *pj1;
public: void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
{
gzLogInit("server-", "gzserver.log");
gzlog << "Start Load" << std::endl;
std::cout << "Logging in " << gzLogDirectory() << std::endl;
// Store the pointer to the model
model = _parent;
//bat->UpdateParameters(_sdf);
std::string linkName = _sdf->Get<std::string>("link_name");
std::string batteyName = _sdf->Get<std::string>("battery_name");
std::cout << "Battery set up" << std::endl;
bat = model->GetLink(linkName)->Battery(batteyName);
GZ_ASSERT(bat, "Battery was NULL");
double powerLoad = _sdf->Get<double>("power_load");
uint32_t consumerId = bat->AddConsumer();
bool success = bat->SetPowerLoad(consumerId, powerLoad);
printf("Hello Rover from Model's SDF PID\n");
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&ModelPush::OnUpdate, this, _1));
joy.start("/dev/input/js0");
printf("Joystick started\n");
Target = 0;
twist = 0;
Wheels = 0;
Turn = 0;
std::string ArmsServo = _sdf->Get<std::string>("ArmsServo");
std::cout << "ArmsServo : " << ArmsServo << std::endl;
//---------------------------------------------------------------------
servos.SetModel(_parent);
servos.SetBattery(bat);//currently only one battery for all servos, will evolve per servo
servos.SetServo("rover::j_right_leg",ArmsServo);
servos.SetServo("rover::j_left_leg",ArmsServo);
servos.SetServo("rover::j_right_arm",ArmsServo);
servos.SetServo("rover::j_left_arm",ArmsServo);
servos.SetPositionTarget("rover::j_right_leg",-Target.Radian());
servos.SetPositionTarget("rover::j_left_leg",-Target.Radian());
servos.SetPositionTarget("rover::j_right_arm",Target.Radian());
servos.SetPositionTarget("rover::j_left_arm",Target.Radian());
}
// Called by the world update start event
public: void OnUpdate(const common::UpdateInfo & _info)
{
//std::cout << "OnUpdate" << std::endl;
bool isUpdated = false;
if(joy.update())//multiple events will be filtered, only last would appear afterwards
{
//joy.printUpdates();
isUpdated = true;
}
bool going_up = false;
JAxis &getup_axis = joy.getAxis(5);
if(getup_axis.isUpdated())
{
Target = igm::Angle::Pi;//init to Pi
Target *= ((getup_axis.getValue()+1)/2);// 0->1
//std::cout << "Target : " << Target << std::endl;
going_up = true;
}
JAxis &turnup_axis = joy.getAxis(2);
if(turnup_axis.isUpdated() && !going_up)
{
Target = igm::Angle::Pi;//init to Pi
Target *= -((turnup_axis.getValue()+1)/2);// 0->1
std::cout << "Target : " << Target << std::endl;
}
JAxis &twist_axis = joy.getAxis(1);
if(twist_axis.isUpdated())
{
twist = igm::Angle::Pi;//init to Pi
twist *= twist_axis.getValue();
std::cout << "Twist : " << twist << std::endl;
}
JAxis &move_axis = joy.getAxis(4);
if(move_axis.isUpdated())
{
Wheels = move_axis.getValue();
std::cout << "Wheels : " << Wheels << std::endl;
}
joy.consumeAll();
igm::Angle Target_Front = twist - Target;
igm::Angle Target_Rear = twist + Target;
servos.SetPositionTarget("rover::j_right_leg",Target_Front.Radian());
servos.SetPositionTarget("rover::j_left_leg",Target_Front.Radian());
servos.SetPositionTarget("rover::j_right_arm",Target_Rear.Radian());
servos.SetPositionTarget("rover::j_left_arm",Target_Rear.Radian());
servos.update(_info.simTime.Double());
model->GetJoint("j_left_arm_wheel")->SetForce(0,Wheels);
model->GetJoint("j_right_arm_wheel")->SetForce(0,Wheels);
model->GetJoint("j_left_leg_wheel")->SetForce(0,Wheels);
model->GetJoint("j_right_leg_wheel")->SetForce(0,Wheels);
}
// Pointer to the model
private: physics::ModelPtr model;
// Pointer to the update event connection
private: event::ConnectionPtr updateConnection;
};
// Register this plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}