3
3
import rclpy
4
4
import random
5
5
import threading
6
+ import numpy as np
6
7
7
8
from rclpy .node import Node
8
9
9
10
from sensor_msgs .msg import LaserScan
10
11
from mavros_wrapper .ardusub_wrapper import *
11
12
12
13
def laser_scan_cb (msg , ardusub ):
13
- forward_speed = 1.0 # Maximum forward velocity
14
+ forward_speed = 0.3 # Maximum forward velocity
14
15
15
16
# TODO: Do something with the sonar msg in order to make the robot not
16
17
# crash into the walls
@@ -21,18 +22,27 @@ def laser_scan_cb(msg, ardusub):
21
22
# https://github.com/gazebosim/docs/blob/master/citadel/sensors.md#avoid-the-wall
22
23
23
24
# Compute the potential vector
25
+ sonar_data = msg
24
26
angles = np .linspace (sonar_data .angle_min , sonar_data .angle_max , num = len (sonar_data .ranges ))
25
27
potentials_x = np .multiply (1 / np .asarray (sonar_data .ranges )** 2 , np .cos (angles ))
26
28
sum_potentials_x = np .sum (potentials_x )
27
29
30
+ print ("angle_min: " , sonar_data .angle_min )
31
+ print ("angle_max: " , sonar_data .angle_max )
32
+ print ("Angles: " , angles )
33
+
34
+ sum_potentials_angles = np .sum (np .cos (angles ))
35
+ print ("Sum of Cosine of Angles: " , sum_potentials_angles )
36
+
28
37
potentials_y = np .multiply (1 / np .asarray (sonar_data .ranges )** 2 , np .sin (angles ))
29
38
sum_potentials_y = np .sum (potentials_y )
30
39
31
40
p = (sum_potentials_x ,sum_potentials_y )
32
41
33
42
# set some gains
34
- k_p = .3
35
- k_phi = 10
43
+ k_p = 1 / sum_potentials_angles
44
+ # k_p = 1/sum_potentials_x
45
+ k_phi = - 0.5
36
46
37
47
# Function for computing the new velocity vector
38
48
potential_velocity (forward_speed ,(p ),k_p ,k_phi )
@@ -59,6 +69,8 @@ def potential_velocity(max_vel,p,K_p,K_phi):
59
69
# The previous values should be between -1 and 1.
60
70
ardusub .toogle_rc_override (True ) # start overriding RC
61
71
ardusub .set_rc_override_channels (forward = forward_vel , yaw = yaw_vel ) # set values to override
72
+ print ("forward_vel : " ,forward_vel )
73
+ print ("yaw_vel : " ,yaw_vel )
62
74
63
75
if __name__ == '__main__' :
64
76
print ("Starting wall avoidance. Let's swim!" )
@@ -73,8 +85,8 @@ def potential_velocity(max_vel,p,K_p,K_phi):
73
85
74
86
# TODO: Set flight mode to MANUAL, STABILIZE or DEPTH HOLD
75
87
service_timer = ardusub .create_rate (2 )
76
- while ardusub .status .mode != "MANUAL " :
77
- ardusub .set_mode ("MANUAL " )
88
+ while ardusub .status .mode != "ALT_HOLD " :
89
+ ardusub .set_mode ("ALT_HOLD " )
78
90
service_timer .sleep ()
79
91
print ("Manual mode selected" )
80
92
@@ -89,7 +101,7 @@ def potential_velocity(max_vel,p,K_p,K_phi):
89
101
90
102
# TODO: start publishing on /mavros/rc/override
91
103
ardusub .toogle_rc_override (True )
92
- ardusub .set_rc_override_channels (throttle = 0.0 , pitch = 0.0 , forward = 0.12 )
104
+ ardusub .set_rc_override_channels (throttle = 0.0 , pitch = 0.0 , forward = 0.01 )
93
105
# TODO: start moving forward
94
106
95
107
# Sonar subscriber
0 commit comments