forked from dusty-nv/jetson-reinforcement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoverPlugin.h
118 lines (95 loc) · 2.85 KB
/
RoverPlugin.h
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
/*
* http://github.com/dusty-nv/jetson-reinforcement
*/
#ifndef __GAZEBO_ROVER_PLUGIN_H__
#define __GAZEBO_ROVER_PLUGIN_H__
#include "deepRL.h"
#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include <iostream>
#include <gazebo/transport/TransportTypes.hh>
#include <gazebo/msgs/MessageTypes.hh>
#include <gazebo/common/Time.hh>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include <ctype.h>
#include <stdbool.h>
#include <math.h>
#include <inttypes.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include "devInput.h"
namespace gazebo
{
/**
* RoverPlugin
*/
class RoverPlugin : public ModelPlugin
{
public:
RoverPlugin();
virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/);
virtual void OnUpdate(const common::UpdateInfo & /*_info*/);
float resetPosition( uint32_t dof ); // center servo positions
bool createAgent();
bool updateAgent();
bool configJoint( const char* name );
bool updateJoints();
void onCameraMsg(ConstImageStampedPtr &_msg);
void onCollisionMsg(ConstContactsPtr &contacts);
static const uint32_t DOF = 2; // FWD/BACK, LEFT/RIGHT
private:
float vel[DOF]; // joint velocity control
float dT[3]; // IK delta theta
enum OperatingMode
{
USER_MANUAL,
/*USER_TRAIN,*/
AGENT_LEARN,
AGENT_RESET
/*,AGENT_AUTO*/
} opMode;
rlAgent* agent; // AI learning agent instance
//OpMode opMode; // robot operating mode
bool newState; // true if a new frame needs processed
bool newReward; // true if a new reward's been issued
bool endEpisode; // true if this episode is over
float rewardHistory; // value of the last reward issued
Tensor* inputState; // pyTorch input object to the agent
void* inputBuffer[2]; // [0] for CPU and [1] for GPU
size_t inputBufferSize;
size_t inputRawWidth;
size_t inputRawHeight;
float actionVelDelta; // amount of velocity offset caused to a joint by an action
int maxEpisodeLength; // maximum number of frames to win episode (or <= 0 for unlimited)
int episodeFrames; // frame counter for the current episode
int episodesCompleted;
int episodesWon;
int lastAction;
float lastGoalDistance;
float avgGoalDelta;
int runHistoryIdx;
int runHistoryMax;
bool runHistory[20];
physics::ModelPtr model;
math::Pose originalPose;
InputDevices* HID;
event::ConnectionPtr updateConnection;
std::vector<physics::JointPtr> joints;
physics::JointController* j2_controller;
gazebo::transport::NodePtr cameraNode;
gazebo::transport::SubscriberPtr cameraSub;
gazebo::transport::NodePtr collisionNode;
gazebo::transport::SubscriberPtr collisionSub;
};
}
#endif