Project

General

Profile

Revision 86222358

ID86222358584a445dd6698755ad159fad9d6347ac

Added by roboclub about 12 years ago

really really fixing teleop this time

View differences:

scout/scoutsim/msg/teleop_input.msg
1
Header header
2
char last_key
scout/scoutsim/src/scout_teleop.h
1
#ifndef _SCOUT_TELEOP_
2
#define _SCOUT_TELEOP_
3

  
4
#include <ros/ros.h>
5
#include <motors/set_motors.h>
6
#include <signal.h>
7
#include <termios.h>
8
#include <stdio.h>
9

  
10
#include <scoutsim/teleop_input.h>
11

  
12
#define QUEUE_SIZE 10
13

  
14
#define KEYCODE_R 0x43 
15
#define KEYCODE_L 0x44
16
#define KEYCODE_U 0x41
17
#define KEYCODE_D 0x42
18
#define KEYCODE_Q 0x71
19
#define KEYCODE_S 0x73
20

  
21
// a second
22
#define MAX_WAIT_TIME ros::Duration(1, 0)
23

  
24
class TeleopScout
25
{
26
    public:
27
        TeleopScout(std::string scoutname);
28
        void keyListenerLoop();
29

  
30
    private:
31
        ros::NodeHandle node;
32
        bool fl_set, fr_set, bl_set, br_set;
33
        int fl_speed, fr_speed, bl_speed, br_speed;
34
        char last_key;
35
        ros::Time last_time;
36
        void teleop_input_callback(const scoutsim::teleop_input::ConstPtr& msg);
37

  
38
        ros::Publisher motors_pub;
39
        ros::Subscriber key_input_sub;
40
};
41

  
42
#endif
scout/scoutsim/src/scout_teleop_userinput.cpp
1
#include "scout_teleop_userinput.h"
2

  
3
TeleopInput::TeleopInput(std::string scoutname)
4
{
5
    input_pub = node.advertise<scoutsim::teleop_input>(scoutname+"/teleop_input", QUEUE_SIZE);
6
    kfd = 0;
7

  
8
    // get the console in raw mode
9
    tcgetattr(kfd, &cooked);
10
    memcpy(&raw, &cooked, sizeof(struct termios));
11
    raw.c_lflag &=~ (ICANON | ECHO);
12
    // Setting a new line, then end of file                         
13
    raw.c_cc[VEOL] = 1;
14
    raw.c_cc[VEOF] = 2;
15
    tcsetattr(kfd, TCSANOW, &raw);
16

  
17
    //TeleopInput::keyLoop();
18
    return;
19
}
20

  
21
TeleopInput::~TeleopInput()
22
{
23
    // reset console mode to no longer be raw
24
    tcsetattr(kfd, TCSANOW, &cooked);
25
    return;
26
}
27

  
28
void TeleopInput::keyLoop()
29
{
30
    ROS_INFO("press keys to start moving!");
31
    char pressed_key;
32

  
33
    while (ros::ok())
34
    {
35
        // get the next event from the keyboard  
36
        if(read(kfd, &pressed_key, 1) < 0)
37
        {
38
          perror("read():");
39
          exit(-1);
40
        }
41

  
42
	// note: printing this normally slows things the hell down
43
        ROS_DEBUG("pressed key 0x%02X at time: %f\n", pressed_key, ros::Time::now().toSec());
44

  
45
        scoutsim::teleop_input msg;
46
        msg.last_key = pressed_key;
47
	msg.header.stamp = ros::Time::now();
48
        input_pub.publish(msg);    
49
        
50
        ros::spinOnce();
51
    }
52
    return;
53
}
54

  
55
int main(int argc, char** argv)
56
{
57
    ros::init(argc, argv, "scout_teleop_userinput");
58
    TeleopInput teleop_input = TeleopInput("scout1");
59
    teleop_input.keyLoop();
60

  
61
    return(0);
62
}
63

  
scout/scoutsim/src/scout_teleop_userinput.h
1
#ifndef _TELEOP_INPUT_
2
#define _TELEOP_INPUT_
3

  
4
#include <ros/ros.h>
5
#include <signal.h>
6
#include <termios.h>
7
#include <stdio.h>
8

  
9
#include <scoutsim/teleop_input.h>
10

  
11
#define QUEUE_SIZE 10
12

  
13
class TeleopInput{
14
    public:
15
        TeleopInput(std::string scoutname);
16
        ~TeleopInput();
17
        void keyLoop();
18
    private:
19
        int kfd;
20
        struct termios cooked, raw;
21
        ros::Publisher input_pub;
22
        ros::NodeHandle node;
23
};
24

  
25
#endif
26

  

Also available in: Unified diff