Code for a robotic pet to interact with.

Dependencies:   mbed wave_player Servo Motordriver mbed-rtos 4DGL-uLCD-SE SDFileSystem_OldbutworkswithRTOS HC_SR04_Ultrasonic_Library

main.cpp

Committer:
jprocter3
Date:
2019-04-17
Revision:
5:d7b32a06799d
Parent:
4:9c2c77d61a15
Child:
6:93e21d47480b

File content as of revision 5:d7b32a06799d:

#include "mbed.h"
#include "Motor.h"
#include "rtos.h"
#include "uLCD_4DGL.h"
#include "mpr121.h"
#include "ultrasonic.h"
uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
DigitalOut speaker(p18);
Motor m1(p23, p12, p11); // pwm, fwd, rev
Motor m2(p24, p12, p11); // pwm, fwd, rev
I2C i2c(p9, p10);
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

enum {ROAM, STOP, SLEEP};
volatile unsigned int state = ROAM;

enum {NOT_TOUCHED, TOUCHED_RIGHT, TOUCHED_WRONG};
volatile unsigned int touched = NOT_TOUCHED;

volatile int energy = 70;
volatile bool fed;
volatile bool blocked;

void dist(int distance)
{
    blocked = false;
    //put code here to execute when the distance has changed
    if (state == ROAM && distance < 50) {
        //turn the bot
        blocked = true;
    } else if (state == STOP && distance < 10) {
        //increase food status
        fed = true;
        Thread::wait(3000); // make sure fed state lasts atleast 3 seconds
        fed = false;
    }
}
ultrasonic mu(p6, p7, .1, 1, &dist);

void sonar(void const *argument) {
    while(1) {
        mu.checkDistance();
        Thread::wait(100);
    }
}

void touchpad(void const *argument) {
    while(1) {
        if (mpr121.read(0x00)) { // touchpad has any interaction
            touched = TOUCHED_RIGHT;
            Thread::wait(5000); // make sure touched state lasts 5 seconds
        }
        else { // no touchpad interaction
            touched = NOT_TOUCHED;
        }
        Thread::wait(100); // runs 10 times a second
    }
}

void screen(void const *argument) {
    while(1) {
        Thread::wait(100);    
    }
}

void sound(void const *argument) {
    while(1) {
        Thread::wait(100);    
    }
}

void wheels(void const *argument) {
    while(1) {
        if(state == ROAM) {
            if (blocked) {
                // code to turn bot
            } else {
                m1.speed(1); 
                m2.speed(1);
            }
        } else {
            m1.speed(0);
            m2.speed(0);
        }      
        Thread::wait(100);
    }
}

int main() {
    uLCD.cls();
    uLCD.printf("Change baudrate.....");
    uLCD.baudrate(3000000); //jack up baud rate to max
    uLCD.background_color(BLACK);
    uLCD.cls();
    mu.startUpdates();

    Thread thread1(sonar);
    Thread thread2(touchpad);
    Thread thread3(screen);
    Thread thread4(sound);
    Thread thread5(wheels);
    
    while(1) {
        // Status LEDS
        led1 = state > 0; // no leds on = ROAM, 1 led on = STOP, both on = SLEEP
        led2 = state > 1;
        led3 = touched;
        led4 = fed;
        
        // Update state
        if (energy < 5) { // sleeps for 10 seconds and wake up with 50 more energy
            state = SLEEP;
            Thread::wait(10000);
            energy += 50;
        } else if (energy < 30 || touched != NOT_TOUCHED || fed) {
            state = STOP;
        } else {
            state = ROAM;
        }
        
        // Update energy
        switch(state) {
            case ROAM: energy -= 2;
                break;
            case STOP : energy -= 1;
                break;
        }
        
        // Make sure energy stays in 0-100
        if (energy < 0) {
            energy = 0;
        } else if (energy > 100) {
            energy = 100;
        }
        
        Thread::wait(1000); // runs once per second
    }
}