Commit 967ba5fe authored by Ricardo Rico Uribe's avatar Ricardo Rico Uribe
Browse files

corrections

parent d2cc0dae
This diff is collapsed.
This diff is collapsed.
......@@ -130,7 +130,7 @@
<joint name="motor_boat_joint" type="revolute">
<parent link="boat_link"/>
<child link="motor_link"/>
<origin xyz="-0.91 0.0 -0.09" rpy="0 0 0"/>
<origin xyz="-0.905 0.0 0.115" rpy="0 0 0"/>
<axis xyz="0 0 1"/>
<limit effort="-1" velocity="-1" lower="-0.9" upper="0.9"/>
<dynamics damping="0.0000001"/>
......
//timer setup for timer4
//For arduino Mega
//timer4 will interrupt at 20Hz
//***********************************************************************//
/*
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
//storage variable
const byte INTERRUPT_NOISE_THRESHOLD = 5 ;
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 MOTOR_CENTER = 1000 ;
const word MOTOR_RIGHT = 1300 ;
const word MOTOR_LEFT = 700 ;
const byte MOTOR_THRESHOLD = 10 ;
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 word RADIO_CENTER = 1500 ;
const word RADIO_RIGHT = 1900 ;
const word RADIO_LEFT = 1100 ;
const byte RADIO_THRESHOLD = 10 ;
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;
//***** Variables *****//
byte speed_command = COMM_ALL_STOP; //Message to send to the Motor Controller
const int _kp = 1;
const int _kp = 1; //Controller Proportional Coefficient
const byte interrupt_pin = 2 ;
static volatile uint32_t delta_us ;
static volatile uint32_t prev_change_date_us = 0 ;
static volatile uint32_t pos = 0;
static volatile byte count = 0;
static volatile uint32_t recv = 0;
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;
bool started = false;
static volatile int scale_pos = 50;
static volatile int scale_radio = 50;
byte speed_command = 0;
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
Serial2.begin(115200); //multiplexer
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){
while (!Serial)
{
//wait for pc connection or RP4 power_up
}
cli(); //stop interrupts
//set timer4 interrupt at 20Hz
TCCR4A = 0;// set entire TCCR4A register to 0
TCCR4B = 0;// same for TCCR4B
TCNT4 = 0;//initialize counter value to 0
//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 = 311/1;// = (16*10^6) / (20*1024) - 1 (must be <65536) // 780 = 200hz / // turn on CTC mode
OCR4A = 311 / 1; // = (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);
TCCR4B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK4 |= (1 << OCIE4A);
sei();//allow interrupts
attachInterrupt(digitalPinToInterrupt(interrupt_pin),pwm,CHANGE);
//delay(5);
}//end setup
ISR(TIMER4_COMPA_vect){
//Serial.print("radio: ");
//Serial.println(scale_radio);
Serial2.write(0x43);
count = 0;
first = true;
}
//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
//***********************************************************************//
void loop(){
//do other things here
//*****Time Interrupt - Request the Motor Position*****//
/*Request the motor position, with a frecuency of 50Hz*/
ISR(TIMER4_COMPA_vect)
{
Serial2.write(0x43); //Char "C", by design this is the message that the encoder expects to respond with its position
count = 0; //reset of message counter
first = true; //reset of first message
}
//***********************************************************************//
void pwm() {
//Serial.print("change");
uint32_t curr_date = micros () ;
bool state_us ;
delta_us = curr_date - prev_change_date_us ;
if (delta_us < INTERRUPT_NOISE_THRESHOLD) return ;
//*****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) {
//Serial.print("radio: ");
//Serial.println(delta_us);
if (delta_us <= RADIO_CENTER + RADIO_THRESHOLD && delta_us >= RADIO_CENTER - RADIO_THRESHOLD){
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 = 50;
}
else if (delta_us <= RADIO_LEFT + RADIO_THRESHOLD){
else if (delta_us <= RADIO_LEFT + RADIO_THRESHOLD)
{
scale_radio = 0;
}
else if (delta_us >= RADIO_RIGHT - RADIO_THRESHOLD){
else if (delta_us >= RADIO_RIGHT - RADIO_THRESHOLD)
{
scale_radio = 100;
} else {
}
else
{
scale_radio = (delta_us - RADIO_LEFT) * 100 / (RADIO_RANGE);
}
//Serial.print("scale_radio: ");
//Serial.println(scale_radio);
go_print = true;
}
else
{
prev_change_date_us = curr_date;
}
/* Change the date anyway. */
prev_change_date_us = curr_date ;
}
//***********************************************************************//
void serialEvent2() {
while(Serial2.available()){
if (first) {
//*****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 {
else //chain all the bytes to have the number that indicates a position
{
recv = (recv << 8) + Serial2.read();
}
count++;
}
if (count == 4) {
if (count == 4) //when all 4 bytes of the message have been recieved, do the control
{
pos = recv;
//Serial.print("scale_pos: ");
//Serial.println(pos);
count = 0;
speed_command = 64;
speed_command = COMM_ALL_STOP;
first = true;
if (!started) {
if (pos > MOTOR_CENTER + MOTOR_THRESHOLD || pos < MOTOR_CENTER - MOTOR_THRESHOLD){
if(pos < MOTOR_LEFT) speed_command = 127;
else if(pos > MOTOR_RIGHT) speed_command = 1;
else if(pos < MOTOR_CENTER - MOTOR_THRESHOLD) speed_command = 100;
else if(pos > MOTOR_CENTER + MOTOR_THRESHOLD) speed_command = 28;
} else {
//Serial.print("centered");
speed_command = 64;
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 {
scale_pos = (pos - MOTOR_LEFT)* 100/(MOTOR_RANGE);
//Serial.print("scale_pos: ");
//Serial.println(scale_pos);
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;
if (v >= -1 && v <= 1){
speed_command = 64;
int v = _kp * error_pos; //Control
if (v >= -1 && v <= 1) //if the error is at 0 with a threshold, send stop to the motor
{
speed_command = COMM_STOP;
}
else if (v >= 30) {
speed_command = 127;
else if (v >= 30) //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 <= -30) {
speed_command = 1;
else if (v <= -30) //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 {
else //Calculate the speed with this formula that transforms the error (-50,50) into the speed command (1,127)
{
speed_command = (1.26) * (v - 50) + 127;
}
//Serial.print("v: ");
//Serial.println(v);
//Serial.print("speed_command: ");
//Serial.println(speed_command);
}
go_print = true;
Serial1.write(speed_command);
}
}
//***********************************************************************//
void serialEvent(){
started = true;
Serial1.write(0);
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.
......@@ -23,7 +23,7 @@
1 1 AHRS_WIND_MAX 0 2
1 1 AHRS_YAW_P 0.200000002980232239 9
1 1 ARMING_ACCTHRESH 0.750000000000000000 9
1 1 ARMING_CHECK 1 6
1 1 ARMING_CHECK 0 6
1 1 ARMING_MIS_ITEMS 0 6
1 1 ARMING_REQUIRE 1 2
1 1 ARMING_RUDDER 2 2
......@@ -178,7 +178,7 @@
1 1 COMPASS_USE3 1 2
1 1 CRASH_ANGLE 0 2
1 1 CRUISE_SPEED 2.000000000000000000 9
1 1 CRUISE_THROTTLE 50 2
1 1 CRUISE_THROTTLE 25 2
1 1 EK2_ABIAS_P_NSE 0.004999999888241291 9
1 1 EK2_ACC_P_NSE 0.600000023841857910 9
1 1 EK2_ALT_M_NSE 3.000000000000000000 9
......@@ -252,7 +252,7 @@
1 1 FS_THR_VALUE 910 4
1 1 FS_TIMEOUT 1.500000000000000000 9
1 1 GCS_PID_MASK 0 4
1 1 GND_ABS_PRESS 100404.390625000000000000 9
1 1 GND_ABS_PRESS 99481.273437500000000000 9
1 1 GND_ABS_PRESS2 0.000000000000000000 9
1 1 GND_ABS_PRESS3 0.000000000000000000 9
1 1 GND_ALT_OFFSET 0.000000000000000000 9
......@@ -261,7 +261,7 @@
1 1 GND_PRIMARY 0 2
1 1 GND_PROBE_EXT 0 6
1 1 GND_TEMP 0.000000000000000000 9
1 1 GPS_AUTO_CONFIG 1 2
1 1 GPS_AUTO_CONFIG 0 2
1 1 GPS_AUTO_SWITCH 1 2
1 1 GPS_BLEND_MASK 5 2
1 1 GPS_BLEND_TC 10.000000000000000000 9
......@@ -322,9 +322,9 @@
1 1 INS_GYR3OFFS_Y 0.000000000000000000 9
1 1 INS_GYR3OFFS_Z 0.000000000000000000 9
1 1 INS_GYR3_ID 0 6
1 1 INS_GYROFFS_X 0.008792726323008537 9
1 1 INS_GYROFFS_Y -0.007072331383824348 9
1 1 INS_GYROFFS_Z 0.005735731683671474 9
1 1 INS_GYROFFS_X 0.008933579549193382 9
1 1 INS_GYROFFS_Y -0.002940948121249676 9
1 1 INS_GYROFFS_Z 0.007573616690933704 9
1 1 INS_GYRO_FILTER 4 4
1 1 INS_GYR_CAL 1 2
1 1 INS_GYR_ID 2359554 6
......@@ -363,7 +363,7 @@
1 1 MIS_DONE_BEHAVE 0 2
1 1 MIS_OPTIONS 0 4
1 1 MIS_RESTART 0 2
1 1 MIS_TOTAL 0 4
1 1 MIS_TOTAL 3 4
1 1 MNT_ANGMAX_PAN 4500 4
1 1 MNT_ANGMAX_ROL 4500 4
1 1 MNT_ANGMAX_TIL 4500 4
......@@ -393,13 +393,13 @@
1 1 MODE4 4 2
1 1 MODE5 4 2
1 1 MODE6 10 2
1 1 MODE_CH 5 2
1 1 MODE_CH 0 2
1 1 MOT_PWM_FREQ 16 2
1 1 MOT_PWM_TYPE 0 2
1 1 MOT_SAFE_DISARM 0 2
1 1 MOT_SLEWRATE 100 4
1 1 MOT_SPD_SCA_BASE 1.000000000000000000 9
1 1 MOT_THR_MAX 100 2
1 1 MOT_THR_MAX 50 2
1 1 MOT_THR_MIN 0 2
1 1 MOT_THST_EXPO 0.000000000000000000 9
1 1 MOT_VEC_THR_BASE 0.000000000000000000 9
......@@ -478,13 +478,13 @@
1 1 RC16_REVERSED 0 2
1 1 RC16_TRIM 1500 4
1 1 RC1_DZ 30 4
1 1 RC1_MAX 1986 4
1 1 RC1_MIN 966 4
1 1 RC1_MAX 1875 4
1 1 RC1_MIN 1026 4
1 1 RC1_OPTION 0 4
1 1 RC1_REVERSED 0 2
1 1 RC1_TRIM 1537 4
1 1 RC1_TRIM 1502 4
1 1 RC2_DZ 0 4
1 1 RC2_MAX 1911 4
1 1 RC2_MAX 1912 4
1 1 RC2_MIN 1135 4
1 1 RC2_OPTION 0 4
1 1 RC2_REVERSED 0 2
......@@ -496,11 +496,11 @@
1 1 RC3_REVERSED 0 2
1 1 RC3_TRIM 1105 4
1 1 RC4_DZ 30 4
1 1 RC4_MAX 1873 4
1 1 RC4_MAX 1874 4
1 1 RC4_MIN 1119 4
1 1 RC4_OPTION 0 4
1 1 RC4_REVERSED 0 2
1 1 RC4_TRIM 1491 4
1 1 RC4_TRIM 1492 4
1 1 RC5_DZ 0 4
1 1 RC5_MAX 1874 4
1 1 RC5_MIN 1017 4
......@@ -733,7 +733,7 @@
1 1 SERIAL3_PROTOCOL 5 2
1 1 SERIAL4_BAUD 38 6
1 1 SERIAL4_OPTIONS 0 4
1 1 SERIAL4_PROTOCOL 5 2
1 1 SERIAL4_PROTOCOL -1 2
1 1 SERIAL5_BAUD 57 6
1 1 SERIAL5_OPTIONS 0 4
1 1 SERIAL5_PROTOCOL -1 2
......@@ -845,15 +845,15 @@
1 1 SR0_RAW_SENS 2 4
1 1 SR0_RC_CHAN 2 4
1 1 SR1_ADSB 0 4
1 1 SR1_EXTRA1 1 4
1 1 SR1_EXTRA2 1 4
1 1 SR1_EXTRA3 1 4
1 1 SR1_EXT_STAT 1 4
1 1 SR1_EXTRA1 10 4
1 1 SR1_EXTRA2 10 4
1 1 SR1_EXTRA3 3 4
1 1 SR1_EXT_STAT 2 4
1 1 SR1_PARAMS 10 4
1 1 SR1_POSITION 1 4
1 1 SR1_POSITION 3 4
1 1 SR1_RAW_CTRL 1 4
1 1 SR1_RAW_SENS 1 4
1 1 SR1_RC_CHAN 1 4
1 1 SR1_RAW_SENS 2 4
1 1 SR1_RC_CHAN 2 4
1 1 SR2_ADSB 0 4
1 1 SR2_EXTRA1 1 4
1 1 SR2_EXTRA2 1 4
......@@ -876,10 +876,10 @@
1 1 SR3_RC_CHAN 1 4
1 1 SRTL_ACCURACY 2.000000000000000000 9
1 1 SRTL_POINTS 300 4
1 1 STAT_BOOTCNT 1 4
1 1 STAT_FLTTIME 0 6
1 1 STAT_RESET 0 6
1 1 STAT_RUNTIME 0 6
1 1 STAT_BOOTCNT 66 4
1 1 STAT_FLTTIME 3569 6
1 1 STAT_RESET 143816064 6
1 1 STAT_RUNTIME 397972 6
1 1 STICK_MIXING 0 2
1 1 SYSID_ENFORCE 0 2
1 1 SYSID_MYGCS 255 4
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment