<?xml version="1.0"?>
<package format="2">
<name>aquanaute_gazebo</name>
<version>1.3.0</version>
<description>
Provides templates and examples for integrating the WAM-V model into gazebo with dynamics, sensors, etc.
</description>
<maintainer email="caguero@osrfoundation.org">Carlos Aguero</maintainer>
<maintainer email="jriveroo@osrfoundation.org">Jose Luis Rivero</maintainer>
<license>Apache 2.0</license>
<url type="website">http://wiki.ros.org/wamv_gazebo</url>
<url type="bugtracker">http://github.com/osrf/vrx/issues</url>
<author email="briansbingham@gmail.com">Brian Bingham</author>
<author email="caguero@osrfoundation.org">Carlos Aguero</author>
<buildtool_depend>catkin</buildtool_depend>
<depend>gazebo_plugins</depend>
<depend>hector_gazebo_plugins</depend>
<depend>robot_localization</depend>
<depend>usv_gazebo_plugins</depend>
<depend>velodyne_gazebo_plugins</depend>
<depend>aquanaute_description</depend>
<depend>xacro</depend>
</package>
<?xml version="1.0"?>
<!-- Basic WAM-V with gazebo plugins for dynamics -->
<robot xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:arg name="testing" default="false" />
<!-- Include macros for dynamics plugins -->
<xacro:unless value="$(arg testing)">
<xacro:include filename="$(find aquanaute_gazebo)/urdf/macros.xacro" />
</xacro:unless>
<!-- Defines the base vehicle -->
<xacro:include filename="$(find aquanaute_description)/urdf/aquanaute_description.urdf.xacro"/>
<!--Gazebo References-->
<gazebo reference="base_link">
<material>Gazebo/Grey</material>
</gazebo>
<gazebo reference="boat_link">
<kp>100000.0</kp>
<kd>100000.0</kd>
<mu1>10.0</mu1>
<mu2>10.0</mu2>
<material>Gazebo/Grey</material>
</gazebo>
<gazebo reference="motor_link">
<material>Gazebo/Blue</material>
</gazebo>
<gazebo reference="propeller_link">
<material>Gazebo/White</material>
</gazebo>
<gazebo reference="imu_link">
<sensor name="imu_sensor" type="imu">
<pose>0 0 0 3.141593 0 0</pose>
<always_on>1</always_on>
<update_rate>1000.0</update_rate>
</sensor>
<material>Gazebo/White</material>
</gazebo>
<!-- * * * GAZEBO Plugins * * * -->
<gazebo>
<plugin name="ardupilot_plugin" filename="libArduPilotPlugin.so">
<fdm_addr>127.0.0.1</fdm_addr>
<fdm_port_in>9002</fdm_port_in>
<fdm_port_out>9003</fdm_port_out>
<modelXYZToAirplaneXForwardZDown>0 0 0 3.141593 0 0</modelXYZToAirplaneXForwardZDown>
<gazeboXYZToNED>0 0 0 3.141593 0 0</gazeboXYZToNED>
<imuName>imu_sensor</imuName>
<connectionTimeoutMaxCount>5</connectionTimeoutMaxCount>
<control channel="0">
<type>POSITION</type>
<multiplier>100</multiplier> <!--ARBITRARY VALUES-->
<offset>0</offset>
<p_gain>1.0</p_gain>
<i_gain>0</i_gain>
<d_gain>0</d_gain>
<i_max>0</i_max>
<i_min>0</i_min>
<cmd_max>3.5</cmd_max>
<cmd_min>-3.5</cmd_min>
<jointName>aquanaute::motor_boat_joint</jointName>
</control>
<control channel="2">
<type>VELOCITY</type>
<multiplier>800</multiplier> <!--ARBITRARY VALUES-->
<offset>0</offset>
<p_gain>0.20</p_gain>
<i_gain>0</i_gain>
<d_gain>0</d_gain>
<i_max>0</i_max>
<i_min>0</i_min>
<cmd_max>2.5</cmd_max>
<cmd_min>-2.5</cmd_min>
<jointName>aquanaute::propeller_motor_joint</jointName>
</control>
<!--
-->
</plugin>
</gazebo>
<!-- Attach hydrodynamics plugin -->
<xacro:unless value="$(arg testing)">
<xacro:usv_dynamics_gazebo name="aquanaute_dynamics_plugin"/>
</xacro:unless>
</robot>
<?xml version="1.0"?>
<robot xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:macro name="usv_dynamics_gazebo" params="name">
<!--Gazebo Plugin for simulating WAM-V dynamics-->
<gazebo>
<plugin name="usv_dynamics_${name}" filename="libusv_gazebo_dynamics_plugin.so">
<bodyName>base_link</bodyName>
<!-- Must be same as the ocean model!-->
<waterLevel>0</waterLevel>
<waterDensity>997.8</waterDensity>
<!-- Added mass -->
<xDotU>0.0</xDotU>
<yDotV>0.0</yDotV>
<nDotR>0.0</nDotR>
<!-- Linear and quadratic drag -->
<xU>51.3</xU>
<xUU>72.4</xUU>
<yV>40.0</yV>
<yVV>0.0</yVV>
<zW>500.0</zW>
<kP>50.0</kP>
<mQ>50.0</mQ>
<nR>400.0</nR>
<nRR>0.0</nRR>
<!-- General dimensions -->
<!--<boatArea>2.2</boatArea>-->
<hullRadius>0.19</hullRadius>
<boatWidth>1.8</boatWidth>
<boatLength>3.2</boatLength>
<!-- Length discretization, AKA, "N" -->
<length_n>10</length_n>
<!-- Wave model -->
<wave_model>ocean_waves</wave_model>
</plugin>
</gazebo>
</xacro:macro>
</robot>
<?xml version="1.0"?>
<!-- Includes all macros in this package so they don't need to be included individualy -->
<robot xmlns:xacro="http://ros.org/wiki/xacro"
name="Aquanaute">
<!-- Include dynamics macros -->
<xacro:include filename="$(find aquanaute_gazebo)/urdf/dynamics/aquanaute_gazebo_dynamics_plugin.xacro" />
</robot>
<sdf version='1.6'>
<world name='default'>
<light name='sun' type='directional'>
<cast_shadows>1</cast_shadows>
<pose frame=''>0 0 10 0 -0 0</pose>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
<attenuation>
<range>1000</range>
<constant>0.9</constant>
<linear>0.01</linear>
<quadratic>0.001</quadratic>
</attenuation>
<direction>-0.5 0.1 -0.9</direction>
</light>
<model name='ground_plane'>
<static>1</static>
<link name='link'>
<collision name='collision'>
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<surface>
<contact>
<collide_bitmask>65535</collide_bitmask>
<ode/>
</contact>
<friction>
<ode>
<mu>100</mu>
<mu2>50</mu2>
</ode>
<torsional>
<ode/>
</torsional>
</friction>
<bounce/>
</surface>
<max_contacts>10</max_contacts>
</collision>
<visual name='visual'>
<cast_shadows>0</cast_shadows>
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
</material>
</visual>
<self_collide>0</self_collide>
<enable_wind>0</enable_wind>
<kinematic>0</kinematic>
</link>
</model>
<gravity>0 0 0</gravity> <!--0 0 -9.8-->
<magnetic_field>6e-06 2.3e-05 -4.2e-05</magnetic_field>
<atmosphere type='adiabatic'/>
<physics name='default_physics' default='0' type='ode'>
<max_step_size>0.001</max_step_size>
<real_time_factor>1</real_time_factor>
<real_time_update_rate>1000</real_time_update_rate>
</physics>
<scene>
<ambient>0.4 0.4 0.4 1</ambient>
<background>0.7 0.7 0.7 1</background>
<shadows>1</shadows>
</scene>
<audio>
<device>default</device>
</audio>
<wind/>
<spherical_coordinates>
<surface_model>EARTH_WGS84</surface_model>
<latitude_deg>0</latitude_deg>
<longitude_deg>0</longitude_deg>
<elevation>0</elevation>
<heading_deg>0</heading_deg>
</spherical_coordinates>
<state world_name='default'>
<sim_time>29 253000000</sim_time>
<real_time>29 457037678</real_time>
<wall_time>1594385127 732457775</wall_time>
<iterations>29253</iterations>
<model name='ground_plane'>
<pose frame=''>0 0 0 0 -0 0</pose>
<scale>1 1 1</scale>
<link name='link'>
<pose frame=''>0 0 0 0 -0 0</pose>
<velocity>0 0 0 0 -0 0</velocity>
<acceleration>0 0 0 0 -0 0</acceleration>
<wrench>0 0 0 0 -0 0</wrench>
</link>
</model>
<light name='sun'>
<pose frame=''>0 0 10 0 -0 0</pose>
</light>
</state>
<gui fullscreen='0'>
<camera name='user_camera'>
<pose frame=''>5 -5 2 0 0.275643 2.35619</pose>
<view_controller>orbit</view_controller>
<projection_type>perspective</projection_type>
</camera>
</gui>
</world>
</sdf>
//***********************************************************************//
/*
Aquanaute - PWM interpreter
Code to read a pwm exit of the navio2 to transform it to a byte speed command, to control a continuous drive motor with an absolute encoder.
Made by: Ricardo RICO URIBE - Internt at U2IS during the summer of 2020
Helpers:
- Thibault TORALBA
- Thomas SIMON
*/
//***** CONSTANTS *****//
const byte INTERRUPT_NOISE_THRESHOLD = 10; //microseconds in which the detection of interrupt will ignore a change
const word MOTOR_CENTER = 1000; //motor position center, this was arbitrarily chosen
const word MOTOR_RIGHT = 1300; //300 steps, arbitrarily chosen to indicate full right, this is aprox 90°
const word MOTOR_LEFT = 700; //300 steps, arbitrarily chosen to indicate full left, this is aprox 90°
const byte MOTOR_THRESHOLD = 10; //value to range the motor position to eliminate oscilation
const word MOTOR_RANGE = MOTOR_RIGHT - MOTOR_LEFT;
const word RADIO_CENTER = 1492; //radio "high time" when radio control is centered, experimentaly obtained, directly linked with the trim value of ardupilot params
const word RADIO_RIGHT = 1872; //radio "high time" when radio control is full right, experimentaly obtained, directly linked with the max pwm value of ardupilot params
const word RADIO_LEFT = 1120; //radio "high time" when radio control is full left, experimentaly obtained, directly linked with the min pwm value of ardupilot params
const byte RADIO_THRESHOLD = 10; //value to range the time calculated to eliminate oscilation
const word RADIO_RANGE = RADIO_RIGHT - RADIO_LEFT;
const byte COMM_ALL_STOP = 0;
const byte COMM_STOP = 64;
const byte COMM_FULL_FWD = 127;
const byte COMM_MED_FWD = 100;
const byte COMM_FULL_BCK = 1;
const byte COMM_MED_BCK = 28;
const byte RADIO_SCALE_CENTER = 50;
const byte RADIO_SCALE_LEFT = 0;
const byte RADIO_SCALE_RIGHT = 100;
const byte MAX_ERROR_BEFORE_SPEED_IS_SET_TO_MAX = 30;
const byte ERROR_CENTER_THRESHOLD = 1;
const byte ERROR_MIN = -50;
const byte ERROR_MAX = 50;
//scale (-50,50) to (1-127) f(x) = (b-a)(x - min)/max - min + a
const float SPEED_COMMAND_SCALE_COEFFICIENT = (COMM_FULL_FWD-COMM_FULL_BCK)/(ERROR_MAX - ERROR_MIN)
const byte TOTAL_OF_MESSAGES = 4;
const byte BYTE_CARRY_LEFT = 8;
const byte HEX_CHAR_C =0x43; //Char "C", by design this is the message that the encoder expects to respond with its position
const word FIFTY_HZ = 331; // = (16*10^6) / (50*1024) - 1 (must be <65536) // 311 = 50Hz
//***** Variables *****//
byte speed_command = COMM_ALL_STOP; //Message to send to the Motor Controller
const int _kp = 1; //Controller Proportional Coefficient
const byte interrupt_pin = 20; //Pin to detect interruptions (connected to the yaw channel in the Navio)
static volatile uint32_t delta_us; //time "high"
static volatile uint32_t prev_change_date_us = 0; //record previous time
static volatile uint32_t pos = 0; //motor position
static volatile byte count = 0; //counter for position response bytes (0-3)
static volatile uint32_t recv = 0; //Bytes recieved
bool first = true; //first byte recieved
bool started = false; //setup to center the motor
bool go_print = false; //debug routine
static volatile int scale_pos = 50; //scale of position, initialized as centered
static volatile int scale_radio = 50; //scale of radio controller position, initialized as centered
//***********************************************************************//
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600); //usb pc
Serial1.begin(9600); //sabertooth driver (speed selected physically on the driver)
Serial2.begin(115200); //multiplexer (speed recommended by the manufactor)
Serial1.write(speed_command);
while (!Serial)
{
//wait for pc connection or RP4 power_up
}
cli(); //stop interrupts
//setup for timed interrupts
TCCR4A = 0; // set entire TCCR4A register to 0
TCCR4B = 0; // same for TCCR4B
TCNT4 = 0; //initialize counter value to 0
// set compare match register for 20hz increments
OCR4A = FIFTY_HZ;// = (16*10^6) / (50*1024) - 1 (must be <65536) // 311 = 50Hz // turn on CTC mode
TCCR4B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR4B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK4 |= (1 << OCIE4A);
//Setup for external Interrupt
sei(); //allow interrupts
pinMode(interrupt_pin, INPUT_PULLUP); //activate internal resistor for interrupt pin
attachInterrupt(digitalPinToInterrupt(interrupt_pin), pwm, CHANGE); //set pin to read external interrupts
} //end setup
//***********************************************************************//
//*****Time Interrupt - Request the Motor Position*****//
/*Request the motor position, with a frecuency of 50Hz*/
ISR(TIMER4_COMPA_vect)
{
Serial2.write(HEX_CHAR_C);
count = 0; //reset of message counter
first = true; //reset of first message
}
//***********************************************************************//
//*****Physical Interruption - Read of Navio PWM*****//
/*Read a change from low to high or from high to low in the pwm signal sended by the navio,
and obtain the "high" time to interpret it as duty cycle and to translate it to and scale between 0 and 100,
where 0 is full left, 50 is center, and 100 is full right*/
void pwm()
{
uint32_t curr_date = micros();
bool state_us;
/* Record the previous pin state since it has just changed. */
state_us = !digitalRead(interrupt_pin);
/* Ensure that we have detected a falling edge, i.e. the previous level of the pin was HIGH. */
if (state_us)
{
delta_us = curr_date - prev_change_date_us;
if (delta_us < INTERRUPT_NOISE_THRESHOLD || delta_us < RADIO_LEFT - RADIO_THRESHOLD || delta_us > RADIO_RIGHT + RADIO_THRESHOLD)
return;
if (delta_us <= RADIO_CENTER + RADIO_THRESHOLD && delta_us >= RADIO_CENTER - RADIO_THRESHOLD)
{
scale_radio = RADIO_SCALE_CENTER;
}
else if (delta_us <= RADIO_LEFT + RADIO_THRESHOLD)
{
scale_radio = RADIO_SCALE_LEFT;
}
else if (delta_us >= RADIO_RIGHT - RADIO_THRESHOLD)
{
scale_radio = RADIO_SCALE_RIGHT;
}
else
{
scale_radio = (delta_us - RADIO_LEFT) * 100 / (RADIO_RANGE); //scale the recieved radio to a value between 0-100
}
go_print = true;
}
else
{
prev_change_date_us = curr_date;
}
}
//*****Serial Interrupt - Read Motor Position*****//
/*If the encoder answers with its position, save the 4 bytes of information, translate them to decimal
and do a convertion to be in a scale between 0 and 100, where 0 is full left, 50 is center and 100 is full right.
After that, do a proportial control and send an speed command to the motor, the command is coded in 7 bits,
where 1 is full backward, 64 is stop and 127 is full forward*/
void serialEvent2()
{
while (Serial2.available())
{
if (first) //Save the first byte of the message
{
recv = Serial2.read();
first = false;
}
else //chain all the bytes to have the number that indicates a position
{
recv = (recv << BYTE_CARRY_LEFT) + Serial2.read();
}
count++;
}
if (count == TOTAL_OF_MESSAGES) //when all 4 bytes of the message have been recieved, do the control
{
pos = recv;
count = 0;
speed_command = COMM_ALL_STOP;
first = true;
if (!started) //When the arduino recieves power it will center the motor.
{
if (pos > MOTOR_CENTER + MOTOR_THRESHOLD || pos < MOTOR_CENTER - MOTOR_THRESHOLD)
{
if (pos < MOTOR_LEFT)
speed_command = COMM_FULL_FWD;
else if (pos > MOTOR_RIGHT)
speed_command = COMM_FULL_BCK;
else if (pos < MOTOR_CENTER - MOTOR_THRESHOLD)
speed_command = COMM_MED_FWD;
else if (pos > MOTOR_CENTER + MOTOR_THRESHOLD)
speed_command = COMM_MED_BCK;
}
else
{
Serial.print("centered");
speed_command = COMM_STOP;
started = true;
}
Serial1.write(speed_command);
}
else //Normal use
{
scale_pos = (pos - MOTOR_LEFT) * 100 / (MOTOR_RANGE); //scale the motor position to a range between 0,100
int error_pos = scale_radio - scale_pos;
int v = _kp * error_pos; //Control
if (v >= -ERROR_CENTER_THRESHOLD && v <= ERROR_CENTER_THRESHOLD) //if the error is at 0 with a threshold, send stop to the motor
{
speed_command = COMM_STOP;
}
else if (v >= MAX_ERROR_BEFORE_SPEED_IS_SET_TO_MAX) //if the error (-50,50) is more than "30" (value arbitrarily chosen) send the maximum speed, this is done to minimize the time of the mouvement
{
speed_command = COMM_FULL_FWD;
}
else if (v <= -MAX_ERROR_BEFORE_SPEED_IS_SET_TO_MAX) //if the error (-50,50) is less than "-30" (value arbitrarily chosen) send the maximum speed, this is done to minimize the time of the mouvement
{
speed_command = COMM_FULL_BCK;
}
else //Calculate the speed with this formula that transforms the error (-50,50) into the speed command (1,127)
{
speed_command = SPEED_COMMAND_SCALE_COEFFICIENT * (v - ERROR_MIN) + COMM_FULL_BCK;
}
}
go_print = true;
Serial1.write(speed_command);
}
}
//***********************************************************************//
void loop()
{
//routine for debug. "never print inside an interruption"
if (go_print)
{
//Serial.print("delta time: ");
//Serial.println(delta_us);
//Serial.print("speed command: ");
//Serial.println(speed_command);
}
}
This diff is collapsed.
ACRO_TURN_RATE,120
ARMING_CHECK,0
CRUISE_SPEED,2
CRUISE_THROTTLE,20
FRAME_CLASS,2
SERVO1_FUNCTION,26
SERVO1_MAX,1900
SERVO1_MIN,1100
SERVO1_REVERSED,1
SERVO1_TRIM,1500
SERVO3_FUNCTION,70
SERVO3_MAX,1900
SERVO3_MIN,1100
SERVO3_REVERSED,0
SERVO3_TRIM,1500
SPEED_TURN_GAIN,50
TURN_MAX_G,0.4
WP_SPEED,4