Skip to content

Commit dc03945

Browse files
authored
Merge pull request #40 from sasanvakili/ros2
Working on hackaton in simulation
2 parents 1771133 + b65b0a1 commit dc03945

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

scripts/potential_avoidance.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import rclpy
44
import random
55
import threading
6+
import numpy as np
67

78
from rclpy.node import Node
89

910
from sensor_msgs.msg import LaserScan
1011
from mavros_wrapper.ardusub_wrapper import *
1112

1213
def laser_scan_cb(msg, ardusub):
13-
forward_speed = 1.0 # Maximum forward velocity
14+
forward_speed = 0.3 # Maximum forward velocity
1415

1516
# TODO: Do something with the sonar msg in order to make the robot not
1617
# crash into the walls
@@ -21,18 +22,27 @@ def laser_scan_cb(msg, ardusub):
2122
# https://github.com/gazebosim/docs/blob/master/citadel/sensors.md#avoid-the-wall
2223

2324
# Compute the potential vector
25+
sonar_data = msg
2426
angles = np.linspace(sonar_data.angle_min, sonar_data.angle_max, num=len(sonar_data.ranges))
2527
potentials_x = np.multiply(1/np.asarray(sonar_data.ranges)**2, np.cos(angles))
2628
sum_potentials_x = np.sum(potentials_x)
2729

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+
2837
potentials_y = np.multiply(1/np.asarray(sonar_data.ranges)**2, np.sin(angles))
2938
sum_potentials_y = np.sum(potentials_y)
3039

3140
p = (sum_potentials_x,sum_potentials_y)
3241

3342
# 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
3646

3747
# Function for computing the new velocity vector
3848
potential_velocity(forward_speed,(p),k_p,k_phi)
@@ -59,6 +69,8 @@ def potential_velocity(max_vel,p,K_p,K_phi):
5969
# The previous values should be between -1 and 1.
6070
ardusub.toogle_rc_override(True) # start overriding RC
6171
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)
6274

6375
if __name__ == '__main__':
6476
print("Starting wall avoidance. Let's swim!")
@@ -73,8 +85,8 @@ def potential_velocity(max_vel,p,K_p,K_phi):
7385

7486
# TODO: Set flight mode to MANUAL, STABILIZE or DEPTH HOLD
7587
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")
7890
service_timer.sleep()
7991
print("Manual mode selected")
8092

@@ -89,7 +101,7 @@ def potential_velocity(max_vel,p,K_p,K_phi):
89101

90102
# TODO: start publishing on /mavros/rc/override
91103
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)
93105
# TODO: start moving forward
94106

95107
# Sonar subscriber

0 commit comments

Comments
 (0)