-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_control.py
executable file
·108 lines (81 loc) · 2.36 KB
/
my_control.py
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
#! /usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist
import os
import pygame
from pygame.locals import *
import sys
from std_msgs import msg
LINEAR_INC = 0.1
ROT_INC = 0.2
msg_to_send = Twist()
msg_to_send.linear.x = 0.0
msg_to_send.linear.y = 0.0
msg_to_send.linear.z = 0.0
msg_to_send.angular.x = 0.0
msg_to_send.angular.y = 0.0
msg_to_send.angular.z = 0.0
def reset_vels():
# stop the motor sending 0 to everything
global msg_to_send
msg_to_send.linear.x = 0.0
msg_to_send.linear.y = 0.0
msg_to_send.linear.z = 0.0
msg_to_send.angular.x = 0.0
msg_to_send.angular.y = 0.0
msg_to_send.angular.z = 0.0
def inc_left():
global msg_to_send
msg_to_send.linear.y += LINEAR_INC
def inc_right():
global msg_to_send
msg_to_send.linear.y -= LINEAR_INC
def inc_forward():
global msg_to_send
msg_to_send.linear.x += LINEAR_INC
def inc_backward():
global msg_to_send
msg_to_send.linear.x -= LINEAR_INC
def inc_rot():
global msg_to_send
msg_to_send.angular.z += ROT_INC
def dec_rot():
global msg_to_send
msg_to_send.angular.z -= ROT_INC
def main():
global msg_to_send
pygame.init()
display = pygame.display.set_mode((300, 300))
rospy.init_node("my_control_node")
pub = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
rate = rospy.Rate(5)
while not rospy.is_shutdown():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
inc_backward()
if event.key == pygame.K_UP:
inc_forward()
if event.key == pygame.K_RIGHT:
inc_right()
if event.key == pygame.K_LEFT:
inc_left()
if event.key == pygame.K_k:
inc_rot()
if event.key == pygame.K_l:
dec_rot()
if event.key == pygame.K_SPACE:
reset_vels()
pub.publish(msg_to_send)
os.system('clear')
print(msg_to_send)
rate.sleep()
if __name__ == "__main__":
try:
main()
except rospy.ROSInterruptException:
pygame.quit()
sys.exit()