Obstacle Avoiding Mobile

Use the following template to help create your post:

  1. What model(s) of servo are you using?
    XC430-W150-T

  2. Describe your control environment. Include the controller or interface, operating system (and version #) of your computer, and how you are powering your robot.
    Window 10, Arduino IDE

  3. For programming posts, state any libraries or example code you are using. For software posts, be clear about which version of software! R+ Task 3.0, R+ Task 2.0, and RoboPlus Task (“1.0”) can have very different troubleshooting steps, as just one example.

I want to make a mobile that avoids obstacle with Dynamixel motor model XC430-W150-T.
I also want to include “HC-SR04 ultra sonic sensor” to detect obstacle. If it detects obstacle, I want to make the mobile stops, and then if obstacle is gone, I want the mobile starts to run.

But I have some trouble to combine those two codes, sonic sensor and Dynamixel motor code.
I’m trying to get information from the sonic sensor and give the information to the motor, but I don’t know how.

below is the code that I want to follow.

int trigPin = 9;
int echoPin = 10;
int revright = 4; //REVerse motion of Right motor
int fwdleft = 7;
int revleft= 6;
int fwdright= 5; //ForWarD motion of Right motor
int c = 0;

void setup() {
//Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:

}

void loop() {
long duration, distance;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;
//Serial.print(distance);
//Serial.println(“CM”);
delay(10);

if((distance>20))
{
digitalWrite(5,HIGH); // If you dont get proper movements of your robot,
digitalWrite(4,LOW); // then alter the pin numbers
digitalWrite(6,LOW); //
digitalWrite(7,HIGH); //
}

else if(distance<20)
{
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,HIGH); //HIGH
digitalWrite(7,LOW);

}

}


below is the different code, using different model of Dynamixel Motor(AX12)

//Robot Car using two Dynamixel Ax-12 Servos, one 9g servo motor,
//Hc-sr04 ultrasonic sensor,and a universal wheel

//Include Libraries
#include <ax12.h> // dynamixel ax-12 library
#include <BioloidController.h> //arbotix-m library
#include <Servo.h> // servo library
//Defining pins
#define trigPin 0 //any instance of trigPin will be replaced with value 0
#define echoPin 1 // any instance of echoPin will be replaced with value 1
//Creating servo object
Servo myservo; // create servo object to control a servo
//Create global variables
int pos = 0; // variable to store the servo position
int fullDelay = 1000; //delay for full turn
int turnDelay = 500;
int scanDelay = 50;
int distanceVal = 20; //20 cm

//Function prototypes
int scan(); //scans and returns distance in centimeters
void forward(); //moves car forward
void Stop(); //stops the car
void turnRight(); //turns right 90 degrees
void turnLeft(); //turns left 90 degrees
void servoStraight(); //servo in straight position
void servoLeft(); //servo left position
void servoRight(); //servo right position

//Robot Car using two Dynamixel Ax-12 Servos, one 9g servo motor,
//Hc-sr04 ultrasonic sensor,and a universal wheel

//Include Libraries
#include <ax12.h> // dynamixel ax-12 library
#include <BioloidController.h> //arbotix-m library
#include <Servo.h> // servo library
//Defining pins
#define trigPin 0 //any instance of trigPin will be replaced with value 0
#define echoPin 1 // any instance of echoPin will be replaced with value 1
//Creating servo object
Servo myservo; // create servo object to control a servo
//Create global variables
int pos = 0; // variable to store the servo position
int fullDelay = 1000; //delay for full turn
int turnDelay = 500;
int scanDelay = 50;
int distanceVal = 20; //20 cm

//Function prototypes
int scan(); //scans and returns distance in centimeters
void forward(); //moves car forward
void Stop(); //stops the car
void turnRight(); //turns right 90 degrees
void turnLeft(); //turns left 90 degrees
void servoStraight(); //servo in straight position
void servoLeft(); //servo left position
void servoRight(); //servo right position

//************* LOOP FUNCTION ***************************************

void loop()
{

frontScan();
scanRight();
frontScan();
scanLeft();

}

//*********** FUNCTION FOR ULTRASONIC SENSOR ********
int scan()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/ 29.1 ; //convert to centimeters
delay(scanDelay);
return distance;
}

//********* CAR FUNCTIONS *************************************
//function for vehicle to move forward
void forward()
{
Serial.println(“going forward”);
ax12SetRegister2(10, AX_GOAL_SPEED_L,1023); //full speed ccw
ax12SetRegister2(11, AX_GOAL_SPEED_L,2047); //full speed cw
}

void Stop()
{
Serial.println(“stop”);
ax12SetRegister2(10, AX_GOAL_SPEED_L,0); // stop motor
ax12SetRegister2(11, AX_GOAL_SPEED_L,0); // stop motor
}

//turn the car 90 degrees to the left
void turnLeft()
{
Serial.println(“turning left”);
ax12SetRegister2(10, AX_GOAL_SPEED_L,2047);
ax12SetRegister2(11, AX_GOAL_SPEED_L,2047);
delay(fullDelay);
Stop();
}

//turn the car 90 degrees to the right
void turnRight()
{
Serial.println(“turning right”);
ax12SetRegister2(10, AX_GOAL_SPEED_L,1023);
ax12SetRegister2(11, AX_GOAL_SPEED_L,1023);
delay(fullDelay);
Stop();
}

//*********** FUNCTIONS FOR 9G SERVO **********************

void servoStraight()
{
myservo.write(90);
delay(500);
}

//function to turn the servo full right
void servoRight()
{
myservo.write(160);
delay(500);
}

//function to turn the servo full left
void servoLeft()
{
myservo.write(20);
delay(500);
}

I think that code is really similar to the code that I want, but it uses different library and it is about the different model of motor(AX 12)

Can you guys help me?

  1. Remember to mark your post Solved when applicable!
1 Like

Hi @Jssub,

Welcomed to this community.

I’m trying to get information from the sonic sensor and give the information to the motor, but I don’t know how.

See the code example below. It would give your insight how to interact with your sensor and motor (DYNAMIXEL in this case). The following code is based on the ROBOTIS interface, DynamixelShield.

// Import your DYNAMIXEL library.
#include <DynamixelShield.h>

const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 2.0;

DynamixelShield dxl;

void setup(){
    
    your_init(); // Your custom setup such as Serial.begin for example
    dynamixel_init(); // Initialize your dynamixel. 
}

void dynamixel_init(){

    // Your DYNAMIXEL Init (TorqueOn, Baudrates, Protocol Setup). See the provided libaray. 
}

int8_t your_distance = 20; 

bool Get_Your_Distance(){

    int8_t distance = $measured_distance_by_hc04 // Set HC-SR04 distance measuring code. 

    if ( distance > your_distance ){ // When triggered by your set condition, return ture
        
        print("obstacle detected")
        
        return true
    }

    return false 
}

bool res; // Get the triggering result of a sensor, as either true or false.

void loop(){

    res = Get_Your_Distance()

    if (res == true){
        dxl.setGoalVelocity(DXL_ID_1, $your_desired_velocity1); // If true, set your desired DYNAMIXEL velocity. 
        dxl.setGoalVelocity(DXL_ID_2, $your_desired_velocity2); // If true, set your desired DYNAMIXEL velocity. 
    }else
    {
        dxl.setGoalVelocity(DXL_ID_1, $your_desired_velocity1); // If true, set your desired DYNAMIXEL velocity. 
        dxl.setGoalVelocity(DXL_ID_2, $your_desired_velocity2); // If true, set your desired DYNAMIXEL velocity. 
    }

}

#2. Regarding the second attatched code utilizing the ax12 library,

Although, I have no chance to look into ax12 library, I am pretty sure its code may not work with your DYNAMIXEL (XC430-W150-T) as it uses differnt packet construction policy. ( a.k.a, DYNAMIXEL Protocol 1.0 vs DYNAMIXEL Protocol 2.0)

It is highly recommended to get DynamixelShield interface for begginer.

1 Like

Thank you so much!
I really appreciate it!
I’ll try to run the code right now :slight_smile:

Thank you!!

1 Like

@Jssub

Sure.

Let us see your robot by showcasing here when you are done with your project !