Shadow Robot Distance Measurement with Bluetooth

Introduction

This is a project involving the Shadow Robot. This configuration allows you to control the robot with Bluetooth and have it measure straight line distances. It calculates the distance it has traveled when moving straight forward, and resets every time it moves again after stopping, or after moving in a direction other than forward. This allows you to move your robot anywhere on the ground, reset its distance counter, and move the robot along an object to measure its length. All from your comfortable chair!

Video Demonstration

/media/uploads/vikram3/demo_video_lab_4.mp4

Parts List

Once you have assemble your robot with its motors, you can look at the pin connections.

Pin Connections

mbedPeripheral 1Peripheral 2
uLCD
5 V+5 V
gndGND
p13TX (cable)
p14RX (cable)
p11reset
Bluetooth
gndGND
5VVin
nCRTS
gndCTS
p27TX0
p28RX1
H-BridgeMotor(left and right)
gndGND
3.3V VinVcc
A01, B02+
A02, B01-
5 V externalVmot
p26, p25pwmA, pwmB
p18, p17AIN1, AIN2
p19, p20BIN1, BIN2
encoder(left and right)
3v3Vcc
gndGND
p23, p22signal

Code

main.cpp

#include "mbed.h"
#include "rtos.h"
#include "stdio.h"
#include "uLCD_4DGL.h"
#include "Motor.h"
#include "HALLFX_ENCODER.h"


Serial pc(USBTX, USBRX);
uLCD_4DGL uLCD(p13,p14,p11); // serial tx, serial rx, reset pin; the uLCD pins
Motor mL(p26, p18, p17); // pwm, fwd, rev
Motor mR(p25, p19, p20); // pwm, fwd, rev
Serial blue(p28, p27);
void motorDrive(const char*);
HALLFX_ENCODER encR(p22);
HALLFX_ENCODER encL(p23);
Mutex lcd_mutex;
Mutex print_mutex;

int readR, readL;

volatile const float speed = 0.4;
volatile bool forward = false;

void thread2(void const *args) {
    while (true) {
        lcd_mutex.lock();
        uLCD.cls();
        uLCD.text_width(3); //4X size text
        uLCD.text_height(3);
        uLCD.color(RED);
        uLCD.locate(1,2);
        if (forward) {
            readR = encR.read();
            int dist = readR*23/555;
            uLCD.printf("%i cm",dist);
        } else {
            int dist = 0;
            uLCD.printf("%i cm",dist);
        }
        lcd_mutex.unlock();
        Thread::wait(500);
    }
}

void thread5(void const *args) {
    char bnum = 0;
    char bhit = 0;
    while(1) {
        if (blue.readable()){
            lcd_mutex.lock();
            if (blue.getc()=='!') {
                if (blue.getc()=='B') { //button data
                    bnum = blue.getc(); //button number
                    bhit = blue.getc();
                    if ((bnum>='5')&&(bnum<='8')) //is a number button 1..4
                        printf("\n\rbnum is %c .", bnum);
                        if (bhit == '1') {
                            if (bnum == '1') {
                                motorDrive("Stop");
                            } else if (bnum == '5') {
                                motorDrive("Forward");
                            } else if (bnum == '6') {
                                motorDrive("Backward");
                            } else if (bnum == '7') {
                                motorDrive("Left");
                            } else if (bnum == '8') {
                                motorDrive("Right");
                            }
                        } else {
                            motorDrive("Stop");
                        }
                }
            }
            lcd_mutex.unlock();
        }
        Thread::wait(50);
    }
}

void motorDrive(const char * input) {
    
    if (strcmp(input, "Forward") == 0) {
        encR.reset();
        encL.reset();
        forward = true;
        mL.speed(speed);
        mR.speed(speed);
    } else if (strcmp(input, "Backward") == 0) {
        forward = false;
        mL.speed(-speed);
        mR.speed(-speed);
    } else if (strcmp(input, "Left") == 0) {
        forward = false;
        mL.speed(-speed);
        mR.speed(speed);
    } else if (strcmp(input, "Right") == 0) {
        forward = false;
        mL.speed(speed);
        mR.speed(-speed);
    } else {
        mL.speed(0.0);
        mR.speed(0.0);
    }
}

int main()
{
    printf("\n\r starting main");
    encR.reset();
    encL.reset();;
    
    uLCD.cls();
    
    Thread t2;
    Thread t5;

    t2.start(callback(thread2, (void *)"Th 2"));
    t5.start(callback(thread5, (void *)"Th 5"));
    
    
    while(1) {}
}

Import programShadowDistance

Shadow Robot Distance Measurement with Bluetooth Code


Please log in to post comments.