Project

General

Profile

Revision e1d66e9a

IDe1d66e9a92e71b3eb67d531aca75e58f7407be40

Added by roboclub about 12 years ago

splitting up teleop into two separate processes, delay is in the wrong place whoops

View differences:

scout/scoutsim/CMakeLists.txt
37 37
#rosbuild_add_executable(draw_square tutorials/draw_square.cpp)
38 38
#rosbuild_add_executable(mimic tutorials/mimic.cpp)
39 39
rosbuild_add_executable(scout_teleop src/scout_teleop.cpp)
40
rosbuild_add_executable(scout_teleop_userinput src/scout_teleop_userinput.cpp)
scout/scoutsim/manifest.xml
40 40
    <depend package="geometry_msgs" />
41 41

  
42 42
    <msgs>
43
      msg/Color.msg  msg/Pose.msg  msg/Velocity.msg 
43
      msg/Color.msg  msg/Pose.msg  msg/Velocity.msg msg/teleop_input.msg
44 44
    </msgs>
45 45
    <srvs>
46 46
      srv/Kill.srv  
scout/scoutsim/src/old_scout_teleop.cpp
1
#include <ros/ros.h>
2
#include <motors/set_motors.h>
3
#include <signal.h>
4
#include <termios.h>
5
#include <stdio.h>
6

  
7
// TODO: add this to a constants.h
8
#define QUEUE_SIZE 10
9

  
10
#define KEYCODE_R 0x43 
11
#define KEYCODE_L 0x44
12
#define KEYCODE_U 0x41
13
#define KEYCODE_D 0x42
14
#define KEYCODE_Q 0x71
15
#define KEYCODE_S 0x73
16

  
17
class TeleopScout
18
{
19
public:
20
    TeleopScout();
21
    void keyLoop();
22

  
23
private:
24
    ros::NodeHandle nh_;
25
    bool fl_set_, fr_set_, bl_set_, br_set_;
26
    int fl_speed_, fr_speed_, bl_speed_, br_speed_;
27
    char last_key;
28
    ros::Publisher motors_pub_;
29
};
30

  
31
TeleopScout::TeleopScout()
32
{
33
    fl_set_ = fr_set_ = bl_set_ = br_set_ = true;
34
    fl_speed_ = fr_speed_ = bl_speed_ = br_speed_ = 0;
35
    motors_pub_ = nh_.advertise<motors::set_motors>(
36
                    "scout1/set_motors", QUEUE_SIZE);
37
}
38

  
39
int kfd = 0;
40
struct termios cooked, raw;
41

  
42
void quit(int sig)
43
{
44
    tcsetattr(kfd, TCSANOW, &cooked);
45
    ros::shutdown();
46
    exit(0);
47
}
48

  
49

  
50
int main(int argc, char** argv)
51
{
52
  ros::init(argc, argv, "teleop_scout");
53
  TeleopScout teleop_scout;
54

  
55
  signal(SIGINT,quit);
56

  
57
  teleop_scout.keyLoop();
58
  
59
  return(0);
60
}
61

  
62

  
63
void TeleopScout::keyLoop()
64
{
65
  char c;
66
  bool dirty=false;
67

  
68
  // get the console in raw mode                                                              
69
  tcgetattr(kfd, &cooked);
70
  memcpy(&raw, &cooked, sizeof(struct termios));
71
  raw.c_lflag &=~ (ICANON | ECHO);
72
  // Setting a new line, then end of file                         
73
  raw.c_cc[VEOL] = 1;
74
  raw.c_cc[VEOF] = 2;
75
  tcsetattr(kfd, TCSANOW, &raw);
76

  
77
  puts("Reading from keyboard");
78
  puts("---------------------------");
79
  puts("Use arrow keys to move the scout.");
80

  
81

  
82
  for(;;)
83
  {
84
    ROS_INFO("loop pass engaged");
85
    // get the next event from the keyboard  
86
    if(read(kfd, &c, 1) < 0)
87
    {
88
      perror("read():");
89
      exit(-1);
90
    }
91

  
92
    //ROS_DEBUG("value: 0x%02X\n", c);
93
    //ROS_INFO("value: 0x%02X\n", c);
94
    switch(c)
95
    {
96
      case KEYCODE_L:
97
        ROS_INFO("LEFT");
98
        fl_speed_ = bl_speed_ = 200;
99
        fr_speed_ = br_speed_ = -200;
100
        dirty = true;
101
        break;
102
      case KEYCODE_R:
103
        ROS_INFO("RIGHT");
104
        fl_speed_ = bl_speed_ = -200;
105
        fr_speed_ = br_speed_ = 200;
106
        dirty = true;
107
        break;
108
      case KEYCODE_U:
109
        ROS_INFO("UP");
110
        fl_speed_ = bl_speed_ = 200;
111
        fr_speed_ = br_speed_ = 200;
112
        dirty = true;
113
        break;
114
      case KEYCODE_D:
115
        ROS_INFO("DOWN");
116
        fl_speed_ = bl_speed_ = -200;
117
        fr_speed_ = br_speed_ = -200;
118
        dirty = true;
119
        break;
120
      default:
121
        ROS_INFO("test");
122
        fl_speed_ = bl_speed_ = 0;
123
        fr_speed_ = br_speed_ = 0;
124
        dirty = true;
125
        break;
126
    }
127
   
128
    motors::set_motors msg;
129
    msg.fl_set = fl_set_;
130
    msg.fr_set = fr_set_;
131
    msg.bl_set = bl_set_;
132
    msg.br_set = br_set_;
133
    msg.fl_speed = fl_speed_;
134
    msg.fr_speed = fr_speed_;
135
    msg.bl_speed = bl_speed_;
136
    msg.br_speed = br_speed_;
137
    
138
    if(dirty == true)
139
    {
140
      if (last_key != c)
141
      {
142
        motors_pub_.publish(msg);
143
      }
144
      dirty=false;
145
    }
146
    last_key = c;
147
  }
148

  
149
  return;
150
}
151

  
152

  
153

  
scout/scoutsim/src/scout_teleop.cpp
1
#include <ros/ros.h>
2
#include <motors/set_motors.h>
3
#include <signal.h>
4
#include <termios.h>
5
#include <stdio.h>
1
#include "scout_teleop.h"
6 2

  
7
// TODO: add this to a constants.h
8
#define QUEUE_SIZE 10
9

  
10
#define KEYCODE_R 0x43 
11
#define KEYCODE_L 0x44
12
#define KEYCODE_U 0x41
13
#define KEYCODE_D 0x42
14
#define KEYCODE_Q 0x71
15
#define KEYCODE_S 0x73
16

  
17
class TeleopScout
18
{
19
public:
20
    TeleopScout();
21
    void keyLoop();
22

  
23
private:
24
    ros::NodeHandle nh_;
25
    bool fl_set_, fr_set_, bl_set_, br_set_;
26
    int fl_speed_, fr_speed_, bl_speed_, br_speed_;
27
    char last_key;
28
    ros::Publisher motors_pub_;
29
};
30

  
31
TeleopScout::TeleopScout()
32
{
33
    fl_set_ = fr_set_ = bl_set_ = br_set_ = true;
34
    fl_speed_ = fr_speed_ = bl_speed_ = br_speed_ = 0;
35
    motors_pub_ = nh_.advertise<motors::set_motors>(
36
                    "scout1/set_motors", QUEUE_SIZE);
37
}
38

  
39
int kfd = 0;
40
struct termios cooked, raw;
41

  
42
void quit(int sig)
3
TeleopScout::TeleopScout(std::string scoutname)
43 4
{
44
    tcsetattr(kfd, TCSANOW, &cooked);
45
    ros::shutdown();
46
    exit(0);
5
    fl_set = fr_set = bl_set = br_set = true;
6
    fl_speed = fr_speed = bl_speed = br_speed = 0;
7
    motors_pub = node.advertise<motors::set_motors>(
8
                    scoutname+"/set_motors", QUEUE_SIZE);
9
    key_input_sub = node.subscribe(scoutname+"/teleop_input",
10
                                        QUEUE_SIZE,
11
                                        &TeleopScout::teleop_input_callback,
12
                                        this);
47 13
}
48 14

  
49

  
50
int main(int argc, char** argv)
15
void TeleopScout::teleop_input_callback(
16
            const scoutsim::teleop_input::ConstPtr& msg)
51 17
{
52
  ros::init(argc, argv, "teleop_scout");
53
  TeleopScout teleop_scout;
54

  
55
  signal(SIGINT,quit);
56

  
57
  teleop_scout.keyLoop();
58
  
59
  return(0);
18
    last_key = msg->last_key;
19
    last_time = msg->header.stamp;
20
    ROS_INFO("received key: 0x%02X ", last_key);
21
    ROS_INFO("at last time: %f\n", last_time.toSec()); 
60 22
}
61 23

  
62

  
63
void TeleopScout::keyLoop()
24
void TeleopScout::keyListenerLoop()
64 25
{
65
  char c;
66
  bool dirty=false;
67

  
68
  // get the console in raw mode                                                              
69
  tcgetattr(kfd, &cooked);
70
  memcpy(&raw, &cooked, sizeof(struct termios));
71
  raw.c_lflag &=~ (ICANON | ECHO);
72
  // Setting a new line, then end of file                         
73
  raw.c_cc[VEOL] = 1;
74
  raw.c_cc[VEOF] = 2;
75
  tcsetattr(kfd, TCSANOW, &raw);
76

  
77
  puts("Reading from keyboard");
78
  puts("---------------------------");
79
  puts("Use arrow keys to move the scout.");
80

  
81

  
82
  for(;;)
83
  {
84
    ROS_INFO("loop pass engaged");
85
    // get the next event from the keyboard  
86
    if(read(kfd, &c, 1) < 0)
26
    while(ros::ok())
87 27
    {
88
      perror("read():");
89
      exit(-1);
28
        if(last_time + MAX_WAIT_TIME < ros::Time::now())
29
        {
30
            switch(last_key)
31
            {
32
                case KEYCODE_L:
33
                    //ROS_INFO("LEFT");
34
                    fl_speed = bl_speed = 200;
35
                    fr_speed = br_speed = -200;
36
                    break;
37
                case KEYCODE_R:
38
                    //ROS_INFO("RIGHT");
39
                    fl_speed = bl_speed = -200;
40
                    fr_speed = br_speed = 200;
41
                    break;
42
                case KEYCODE_U:
43
                    //ROS_INFO("UP");
44
                    fl_speed = bl_speed = 200;
45
                    fr_speed = br_speed = 200;
46
                    break;
47
                case KEYCODE_D:
48
                    //ROS_INFO("DOWN");
49
                    fl_speed = bl_speed = -200;
50
                    fr_speed = br_speed = -200;
51
                    break;
52
                default:
53
                    //don't publish a message yet; no change to key, 
54
                    // and no timeout yet
55
                    ROS_INFO("no time out yet\n");
56
                    ros::spinOnce();
57
                    continue;
58
                    break;
59
            }
60
        }
61
        else if (fl_speed != 0 || fr_speed != 0 || 
62
                 bl_speed != 0 || br_speed != 0)
63
        {
64
            fl_speed = fr_speed = bl_speed = br_speed = 0;
65
        }
66

  
67
        //send command to the motors
68
        motors::set_motors msg;
69
        msg.fl_set = fl_set;
70
        msg.fr_set = fr_set;
71
        msg.bl_set = bl_set;
72
        msg.br_set = br_set;
73
        msg.fl_speed = fl_speed;
74
        msg.fr_speed = fr_speed;
75
        msg.bl_speed = bl_speed;
76
        msg.br_speed = br_speed;
77
        motors_pub.publish(msg);
78
                
79
        ros::spinOnce();
90 80
    }
81
    return;
82
}
91 83

  
92
    //ROS_DEBUG("value: 0x%02X\n", c);
93
    //ROS_INFO("value: 0x%02X\n", c);
94
    switch(c)
95
    {
96
      case KEYCODE_L:
97
        ROS_INFO("LEFT");
98
        fl_speed_ = bl_speed_ = 200;
99
        fr_speed_ = br_speed_ = -200;
100
        dirty = true;
101
        break;
102
      case KEYCODE_R:
103
        ROS_INFO("RIGHT");
104
        fl_speed_ = bl_speed_ = -200;
105
        fr_speed_ = br_speed_ = 200;
106
        dirty = true;
107
        break;
108
      case KEYCODE_U:
109
        ROS_INFO("UP");
110
        fl_speed_ = bl_speed_ = 200;
111
        fr_speed_ = br_speed_ = 200;
112
        dirty = true;
113
        break;
114
      case KEYCODE_D:
115
        ROS_INFO("DOWN");
116
        fl_speed_ = bl_speed_ = -200;
117
        fr_speed_ = br_speed_ = -200;
118
        dirty = true;
119
        break;
120
      default:
121
        ROS_INFO("test");
122
        fl_speed_ = bl_speed_ = 0;
123
        fr_speed_ = br_speed_ = 0;
124
        dirty = true;
125
        break;
126
    }
127
   
128
    motors::set_motors msg;
129
    msg.fl_set = fl_set_;
130
    msg.fr_set = fr_set_;
131
    msg.bl_set = bl_set_;
132
    msg.br_set = br_set_;
133
    msg.fl_speed = fl_speed_;
134
    msg.fr_speed = fr_speed_;
135
    msg.bl_speed = bl_speed_;
136
    msg.br_speed = br_speed_;
84
int main(int argc, char** argv)
85
{
86
    ros::init(argc, argv, "scout_teleop");
87
    TeleopScout teleop_scout = TeleopScout("scout1");
137 88
    
138
    if(dirty == true)
139
    {
140
      if (last_key != c)
141
      {
142
        motors_pub_.publish(msg);
143
      }
144
      dirty=false;
145
    }
146
    last_key = c;
147
  }
89
    teleop_scout.keyListenerLoop();
148 90

  
149
  return;
91
    return(0);
150 92
}
151

  
152

  
153

  

Also available in: Unified diff