Movement path drawing

Hi, I want to draw the movement path of my robot. I mean I want to draw a path that my robot move in that direction. can any one help me?

Do you want to get the path the robot is following, or do you want to set a path for the robot to follow ?

I want to set a pass for robot to follow.

How do you want to draw the path ?

By swiping on the screen

Do you mean swipe to the right, car turns right and moves forward in that direction, for example ?

Just keep what you want to do close to your chest, let me keep asking questions until we know what it is you want to do...(sarcasm)

I want to draw a curve and the robot follows it.

Here is an example of what you might do. I have simulated the movement of the car in the smaller canvas. You would need a "map" of the area the car operates in, so that the coordinates recorded on the large canvas can be translated to positions for the car to move to. I do not have such a car so do not know how you might program this part. This may be enough to get you going...

drawRouteForCar.aia (4.9 KB)

Others may have better suggestions...

2 Likes

Tank you very much. How can I open the attached file?

In appinventor 2

1 Like

How to design the blocks for this code?

#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

SoftwareSerial BT(13,12);  //RX , TX pins

char state=0;
/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
is 0x40 */ 
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41
int pos1=0;
int pos2=100;
int pos3=200; 
int pos5=150;
const float pos4=80.0/pos5;
/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {
   // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);
  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);
  unsigned int Pos;
  
Serial.begin(9600);
BT.begin(9600);
}

/* Move Forward */
void move_forward() {
  HCPCA9685_1.Servo(2, pos1);
  HCPCA9685_1.Servo(5, pos1);
}

/* Move Backward */
void move_backward() {
  HCPCA9685_1.Servo(2, pos2);
  HCPCA9685_1.Servo(5, pos2);
}

/* Move Right */
void turn_right() {
   HCPCA9685_1.Servo(0, pos1);
  HCPCA9685_1.Servo(3, pos1);
}

/* Move Left */
void turn_left() {
   HCPCA9685_1.Servo(0, pos2);
  HCPCA9685_1 .Servo(3, pos2);
}

/* Stop Move */
void move_stop() {
    HCPCA9685_1.Servo(0, pos2);

}

void loop() {
  if (BT.available() >0) {
    state = BT.read();
    Serial.print(state);
    
    if(state == 'F'){
      move_forward();
  }
    else if(state == 'B'){
      move_backward();
  }
    else if(state == 'R'){
      turn_right();
  }
    else if(state == 'L'){
      turn_left();
  }
//    else if (state == "S") {
//      move_stop();
//  } 
  }
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Hi, I want to draw the movement path of my robot. I mean I want to draw a path that my robot move in that direction. can any one help me? witch blocks should I add?

This is the blocks I have:
ead570f56c554cb5daf9fa604462ec3fac766778_2_178x499
drawRouteForCar.aia (4.9 KB)

And this is the code:

#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

SoftwareSerial BT(13,12);  //RX , TX pins

char state=0;
/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
is 0x40 */ 
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
int delay1;
int delay2 = 500;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {
  /* Initialise the library and set it to 'servo mode' */

  // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);

  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);

  unsigned int Pos;
  
Serial.begin(9600);
BT.begin(9600);
/////  
/* standing */
  HCPCA9685_1.Servo(2, pos4);
  HCPCA9685_1.Servo(5, pos4);
  HCPCA9685_1.Servo(8, pos4);
  HCPCA9685_1.Servo(11, pos4);
  HCPCA9685_1.Servo(14, pos4);
  HCPCA9685_2.Servo(1, pos4);

  HCPCA9685_1.Servo(1, pos4);
  HCPCA9685_1.Servo(4, pos4);
  HCPCA9685_1.Servo(7, pos4);
  HCPCA9685_1.Servo(10, pos4);
  HCPCA9685_1.Servo(13, pos4);
  HCPCA9685_2.Servo(0, pos4);

  HCPCA9685_1.Servo(0, pos2);
  HCPCA9685_1.Servo(3, pos3);
  HCPCA9685_1.Servo(6, pos2);
  HCPCA9685_1.Servo(9, pos2);
  HCPCA9685_1.Servo(12, pos3);
  HCPCA9685_1.Servo(15, pos2);

  HCPCA9685_2.Servo(2, 200);  // sensor movement

  delay(delay2);

  for (int Pos = pos4; Pos < pos5; Pos++)
  {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_1.Servo(13, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }

  delay(delay2);

  for (int Pos = pos4; Pos < pos6; Pos++)
  {
    HCPCA9685_1.Servo(2, Pos);
    HCPCA9685_1.Servo(5, Pos);
    HCPCA9685_1.Servo(8, Pos);
    HCPCA9685_1.Servo(11, Pos);
    HCPCA9685_1.Servo(14, Pos);
    HCPCA9685_2.Servo(1, Pos);
    delay(1);
  }
}

/* Move Forward */
void move_forward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }
}

/* Move Backward */
void move_backward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }

    for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }
}

/* Turn Right */
void turn_right() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }
}

/* Turn Left */
void turn_left() {
/////
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }
}

/* Stop Move */
void move_stop() {
}

void loop() {
  if (BT.available() >0) {
    state = BT.read();
    Serial.print(state);

    if(state == 'F'){
      move_forward();
//      state="";
  }
  
    if(state == 'f'){
      move_stop();
//      state="";
  }
  
    if(state == 'B'){
      move_backward();
//      state="";
  }

    if(state == 'b'){
      move_stop();
//      state="";
  }

    if(state == 'R'){
      turn_right();
//      state="";
  }

    if(state == 'r'){
      move_stop();
//      state="";
  }
  
    if(state == 'L'){
      turn_left();
//      state="";
  }

    if(state == 'l'){
      move_stop();
//      state="";
  }
//    else if (state == "S") {
//      move_stop();
//  } 
  }
}

This looks like a very familiar sketch.
Where's the Project that was used to control this car?

I don't know. TIMAI2 suggested this in this topic. have you any idea for sending data to arduino for these blocks?

You are operating without any feedback from the robot, for example did it collide with something.

All you can hope for in this case is to just collect your moves (transmitted letters) into a list and to slowly play the list items back through BlueTooth one at a time.

The robot has a distance sensor and if there is an obstacle it will change the path.
This is the code:

   ////////////////////////////////////////////////////////
  //          Arduino Obstacle Avoiding Robot v2.0      //             
 //            By Aarav Garg - 2021                    //
////////////////////////////////////////////////////////

//including the libraries
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

//defining pins and variables
#define TRIG_PIN D3 
#define ECHO_PIN D4 
#define MAX_DISTANCE 200

#define turn_amount 500

//defining motors,servo,sensor
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
  
//defining global variables
boolean goesForward=false;
int distance = 100;

#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
const float delay1 = 0.5;
int delay2 = 500;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {
       // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);
  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);
  unsigned int Pos;

/* standing */
  HCPCA9685_1.Servo(2, pos4);
  HCPCA9685_1.Servo(5, pos4);
  HCPCA9685_1.Servo(8, pos4);
  HCPCA9685_1.Servo(11, pos4);
  HCPCA9685_1.Servo(14, pos4);
  HCPCA9685_2.Servo(1, pos4);

  HCPCA9685_1.Servo(1, pos4);
  HCPCA9685_1.Servo(4, pos4);
  HCPCA9685_1.Servo(7, pos4);
  HCPCA9685_1.Servo(10, pos4);
  HCPCA9685_1.Servo(13, pos4);
  HCPCA9685_2.Servo(0, pos4);

  HCPCA9685_1.Servo(0, pos2);
  HCPCA9685_1.Servo(3, pos3);
  HCPCA9685_1.Servo(6, pos2);
  HCPCA9685_1.Servo(9, pos2);
  HCPCA9685_1.Servo(12, pos3);
  HCPCA9685_1.Servo(15, pos2);

  delay(delay2);

  for (int Pos = pos4; Pos < pos5; Pos++)
  {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_1.Servo(13, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }

  delay(delay2);

  for (int Pos = pos4; Pos < pos6; Pos++)
  {
    HCPCA9685_1.Servo(2, Pos);
    HCPCA9685_1.Servo(5, Pos);
    HCPCA9685_1.Servo(8, Pos);
    HCPCA9685_1.Servo(11, Pos);
    HCPCA9685_1.Servo(14, Pos);
    HCPCA9685_2.Servo(1, Pos);
    delay(1);
  }
  /////
  Serial.begin(9600);
  HCPCA9685_2.Servo(2, 220);  
//  myservo.write(90); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
// moveForward();
// goesForward=false;
// moveForward(); 
 
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 Serial.println(distance);
 
 if(distance<=15)
 {
  Serial.println("Object Detected");
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  Serial.print("Distance Right = ");
  Serial.println(distanceR);
  delay(1000);
  distanceL = lookLeft();
  Serial.print("Distance Left = ");
  Serial.println(distanceL);
  delay(1000);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }
  else
  {
    turnLeft();
    moveStop();
  }
 }
 else
 {
  moveForward();
 }

 //reseting the variable after the operations
 distance = readPing();
}

int lookRight()
{
    HCPCA9685_2.Servo(2, 130);
//    myservo.write(0); 
    delay(500);
    int distance = readPing();
    delay(100);
    HCPCA9685_2.Servo(2, 220);
//    myservo.write(90); 
    return distance;
}

int lookLeft()
{
    HCPCA9685_2.Servo(2, 310);
//    myservo.write(180); 
    delay(500);
    int distance = readPing();
    delay(100);
    HCPCA9685_2.Servo(2, 220);
//    myservo.write(90); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {

  } 
  
/* Move Forward */
void moveForward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }
}

/* moveBackward */
void move_backward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }

    for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }
}

/* turn Right */
void turnRight() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }
}

/* turn Left */
void turnLeft() {
/////
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }
}

/* Stop Move */
void moveStop() {
}

Sorry I don't know how to collect the moves in to a list. I don't know how to send the data in blocks to arduino for path drawing. I defined four directions for the robot in my code and I can control the robot with buttons but how can I control the robot when I draw a path? Can you edit the blocks or code?

You will need to think up a way to represent the actual movements of your robot as a data stream for it to send the app via BlueTooth.

For simplicity, you might set up a BT BlueTooth stream in the sketch, and add a BT.println() command to each of your 5 turn, move, and stop procedures to send a distinctive code letter to the app each time that procedure is called.

In your app, set up a fast Clock Timer to check for those incoming codes and add them to a global list moves_list, initially create empty list.

Add to your app a Play button to walk the list and move a Sprite or Ball on a Canvas accordingly.

Do you mean some thing like this code? And these blocks that have clock? you sent these blocks in other topic.

  if (BT.available() >0) {
    state = BT.read();
    Serial.print(state);

    if(state == 'F'){
      move_forward();
//      state="";
  }
  
    if(state == 'f'){
      move_stop();
//      state="";
  }
  
    if(state == 'B'){
      move_backward();
//      state="";
  }

    if(state == 'b'){
      move_stop();
//      state="";
  }

    if(state == 'R'){
      turn_right();
//      state="";
  }

    if(state == 'r'){
      move_stop();
//      state="";
  }
  
    if(state == 'L'){
      turn_left();
//      state="";
  }

    if(state == 'l'){
      move_stop();
//      state="";
  }

But How can I use above blocks for developing the below blocks?

ead570f56c554cb5daf9fa604462ec3fac766778_2_178x499

And about the button you said I want to use 'follow path' button for sending data from app to arduino.