Dependencies:   mbed Motor mbed-rtos

Useless Robot

Useless Robot Overview

Project by Austin Bartmess and Jeremy Deremer

This is a robot developed on an ARM Microcontroller (LPC 1768). It uses a servo, two DC motors both powered by an external power source, nine red LEDs, two RBG LEDs, a speaker, a toggle switch and a transistor.

The goal of this robot is to stay off. However, the user can turn it on without harm. It attempts to stay off via turning a toggle switch off with an arm attached to a servo. If the robot is turned on too much, it will progressively get angry. It starts by not smiling and changing eye colors. Following that, the robot will frown and yell at the user. Finally, the robot will drive around randomly while screaming and looking angry via its LEDs.

Required Parts

  • mbed Microcontroller x1
  • USB to mini USB cable x1
  • Servo x1
  • H-Bridge Breakout Board x1
  • DC Motors x2
  • DC Motor Wheels x4
  • Red LEDs x9
  • RGB LEDs x2
  • 0.5W Speaker x1
  • Toggle Switch x1
  • BJTransistor x1
  • Jumper Cables Kit x1
  • Breadboard x2

Pinout and Connections

MbedH-BridgeDC MotorsServoTransistorSpeakerLEDsBarrel JackToggle Switch
GNDGND(-)Emitter(-)GND(-)(-)
Vu
VinVm(+)(+)
VoutVcc, /STBY(+)
p5AIN1
p6AIN2
p10LED+
p11LED+
p12LED+
p15Control
p21PWMA
p22Collector
p23Blue
p24Green
p25Red
p26Control
AO1(+)
AO2(-)

/media/uploads/jderemer3/capture.png

Demo Clip

Source Code

Import program4180_FinaProject

Committer:
arb
Date:
Thu Apr 27 03:53:55 2017 +0000
Revision:
2:69a3683834c7
Parent:
1:0402db7e91f5
Child:
3:a7bd2f5946ed
v2 Skeleton code/examples for all the threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jderemer3 0:4931c25e1cf7 1 #include "mbed.h"
jderemer3 0:4931c25e1cf7 2 #include "rtos.h"
arb 2:69a3683834c7 3 #include "Servo.h"
jderemer3 0:4931c25e1cf7 4 #include "RGBLed.h"
arb 2:69a3683834c7 5 #include "wave_player.h"
arb 2:69a3683834c7 6 #include "SDFileSystem.h"
arb 2:69a3683834c7 7 #include "Motor.h"
jderemer3 0:4931c25e1cf7 8 #include "uLCD_4DGL.h"
jderemer3 0:4931c25e1cf7 9
jderemer3 0:4931c25e1cf7 10 DigitalOut led1(LED1);
jderemer3 0:4931c25e1cf7 11 DigitalOut led2(LED2);
jderemer3 0:4931c25e1cf7 12 DigitalOut led3(LED3);
jderemer3 0:4931c25e1cf7 13 DigitalOut led4(LED4);
jderemer3 0:4931c25e1cf7 14
jderemer3 0:4931c25e1cf7 15 // RGBLed myRGBled(p21,p22,p23); //RGB PWM pins
jderemer3 0:4931c25e1cf7 16
arb 1:0402db7e91f5 17 InterruptIn Switch(p15);
arb 1:0402db7e91f5 18
arb 2:69a3683834c7 19 //SERVO for arm
arb 2:69a3683834c7 20 Servo arm(p22);
arb 2:69a3683834c7 21
arb 2:69a3683834c7 22 //LEDS
jderemer3 0:4931c25e1cf7 23 PwmOut red(p21);
jderemer3 0:4931c25e1cf7 24 PwmOut green(p22);
jderemer3 0:4931c25e1cf7 25 PwmOut blue(p23);
jderemer3 0:4931c25e1cf7 26
arb 2:69a3683834c7 27 //SD card/audio
arb 2:69a3683834c7 28 PwmOut PWMout(p25);
arb 2:69a3683834c7 29 AnalogOut DACout(p18);
arb 2:69a3683834c7 30 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
arb 2:69a3683834c7 31 wave_player waver(&DACout,&PWMout);
arb 2:69a3683834c7 32 char buffer[20];
arb 2:69a3683834c7 33 FILE *wave_file;
arb 2:69a3683834c7 34
arb 2:69a3683834c7 35 //motor for wheels
arb 2:69a3683834c7 36 Motor wheelF(p21, p6, p5); // pwm, fwd, rev
arb 2:69a3683834c7 37 Motor wheelB(p21, p6, p5); // pwm, fwd, rev
jderemer3 0:4931c25e1cf7 38
jderemer3 0:4931c25e1cf7 39 Thread t1; // Arm Response
jderemer3 0:4931c25e1cf7 40 Thread t2; // LED Color
jderemer3 0:4931c25e1cf7 41 Thread t3; // Speaker Music / Audio
jderemer3 0:4931c25e1cf7 42 Thread t4; // Motor Control
jderemer3 0:4931c25e1cf7 43
arb 1:0402db7e91f5 44 Mutex state;
arb 1:0402db7e91f5 45 int angerlevel;
arb 1:0402db7e91f5 46 bool pushFlag = false;
arb 1:0402db7e91f5 47
arb 2:69a3683834c7 48 void ArmThread() {
arb 1:0402db7e91f5 49 //basic if switch is toggled then use arm to turn it off
arb 2:69a3683834c7 50 //TODO: setup angle/speed for reasonable opening according to angerlevel
arb 1:0402db7e91f5 51 //Want to add delay depedning on anger level
arb 1:0402db7e91f5 52 //Potentially need a queue system
arb 1:0402db7e91f5 53 if (pushFlag)
arb 1:0402db7e91f5 54 {
arb 1:0402db7e91f5 55 //start servo
arb 2:69a3683834c7 56 while (1)
arb 2:69a3683834c7 57 {
arb 2:69a3683834c7 58 for(float p=0; p<1.0; p += 0.1)
arb 2:69a3683834c7 59 {
arb 2:69a3683834c7 60 arm = p;
arb 2:69a3683834c7 61 wait(0.4);
arb 2:69a3683834c7 62 }
arb 2:69a3683834c7 63 for(float p=1; p>0.0; p -= 0.1)
arb 2:69a3683834c7 64 {
arb 2:69a3683834c7 65 arm = p;
arb 2:69a3683834c7 66 wait(0.4);
arb 2:69a3683834c7 67 }
arb 2:69a3683834c7 68 }
arb 1:0402db7e91f5 69 state.lock(); //lock to update the anger level
arb 1:0402db7e91f5 70 angerlevel++;
arb 1:0402db7e91f5 71 state.unlock();
jderemer3 0:4931c25e1cf7 72 }
jderemer3 0:4931c25e1cf7 73 }
jderemer3 0:4931c25e1cf7 74
arb 1:0402db7e91f5 75 void LEDThread() {
arb 2:69a3683834c7 76 //TODO: Add colors according to angerlevel
arb 1:0402db7e91f5 77 state.lock(); //lock to read the angerlevel
arb 2:69a3683834c7 78 switch(angerlevel){
arb 1:0402db7e91f5 79 case 1:
arb 1:0402db7e91f5 80 state.unlock();
arb 1:0402db7e91f5 81 //turn on leds
arb 1:0402db7e91f5 82 break;
arb 1:0402db7e91f5 83 case 2:
arb 1:0402db7e91f5 84 state.unlock();
arb 1:0402db7e91f5 85 //LEDS/AUDIO
arb 1:0402db7e91f5 86 break;
arb 1:0402db7e91f5 87 case 3:
arb 1:0402db7e91f5 88 state.unlock();
arb 1:0402db7e91f5 89 //LEDS/AUDIO/motor
arb 1:0402db7e91f5 90 break;
arb 1:0402db7e91f5 91 default:
arb 1:0402db7e91f5 92 state.unlock();
arb 1:0402db7e91f5 93 //case 0 do nothing
arb 1:0402db7e91f5 94 break;
arb 2:69a3683834c7 95 }
arb 1:0402db7e91f5 96 state.unlock();
jderemer3 0:4931c25e1cf7 97 }
arb 1:0402db7e91f5 98
arb 1:0402db7e91f5 99 void AudioThread() {
arb 1:0402db7e91f5 100 //PWM/AMP? to play audio?
arb 2:69a3683834c7 101 //TODO: get sound on SD card and confirm this will work
arb 2:69a3683834c7 102 //mabye multiple sounds according to what angerlevel
arb 1:0402db7e91f5 103 state.lock(); //lock to read the angerlevel
arb 2:69a3683834c7 104 switch(angerlevel){
arb 1:0402db7e91f5 105 case 2:
arb 1:0402db7e91f5 106 state.unlock();
arb 1:0402db7e91f5 107 //AUDIO
arb 2:69a3683834c7 108 for (int a=1;a<40; a++){
arb 2:69a3683834c7 109 sprintf(buffer,"/sd//voice%d.wav",a);
arb 2:69a3683834c7 110 wave_file=fopen(buffer,"r");
arb 2:69a3683834c7 111 waver.play(wave_file);
arb 2:69a3683834c7 112 fclose(wave_file);
arb 2:69a3683834c7 113 }
arb 1:0402db7e91f5 114 break;
arb 1:0402db7e91f5 115 case 3:
arb 1:0402db7e91f5 116 state.unlock();
arb 1:0402db7e91f5 117 //AUDIO/MOTOR
arb 1:0402db7e91f5 118 break;
arb 1:0402db7e91f5 119 default:
arb 1:0402db7e91f5 120 state.unlock();
arb 1:0402db7e91f5 121 //Nothing to do here
arb 1:0402db7e91f5 122 break;
arb 2:69a3683834c7 123 }
arb 1:0402db7e91f5 124 state.unlock();
arb 1:0402db7e91f5 125 }
arb 1:0402db7e91f5 126
arb 1:0402db7e91f5 127 void WheelsThread() {
arb 1:0402db7e91f5 128 //Drive around randomly
arb 2:69a3683834c7 129 //TODO: add random driving for both motors(current:example for 1 motor)
arb 1:0402db7e91f5 130 state.lock(); //lock to read the angerlevel
arb 2:69a3683834c7 131 switch(angerlevel){
arb 1:0402db7e91f5 132 case 3:
arb 1:0402db7e91f5 133 state.unlock();
arb 2:69a3683834c7 134 for (float s= -1.0; s < 1.0 ; s += 0.01) {
arb 2:69a3683834c7 135 wheelF.speed(s);
arb 2:69a3683834c7 136 wait(0.04);
arb 2:69a3683834c7 137 }
arb 2:69a3683834c7 138 for (float s= 1.0; s > -1.0 ; s -= 0.01) {
arb 2:69a3683834c7 139 wheelF.speed(s);
arb 2:69a3683834c7 140 wait(0.04);
arb 2:69a3683834c7 141 }
arb 1:0402db7e91f5 142 break;
arb 1:0402db7e91f5 143 default:
arb 1:0402db7e91f5 144 state.unlock();
arb 1:0402db7e91f5 145 //nothing
arb 1:0402db7e91f5 146 break;
arb 2:69a3683834c7 147 }
arb 1:0402db7e91f5 148 state.unlock();
arb 1:0402db7e91f5 149 }
arb 1:0402db7e91f5 150
jderemer3 0:4931c25e1cf7 151
arb 1:0402db7e91f5 152 void fallInterrupt(){
arb 1:0402db7e91f5 153 pushFlag=true;
arb 1:0402db7e91f5 154 }
arb 1:0402db7e91f5 155
arb 1:0402db7e91f5 156 void init(){
arb 2:69a3683834c7 157 Switch.fall(&fallInterrupt);
arb 2:69a3683834c7 158 Switch.mode(PullUp);
arb 2:69a3683834c7 159
arb 2:69a3683834c7 160
arb 2:69a3683834c7 161 //setup PWM hardware for a Class D style audio output
arb 2:69a3683834c7 162 PWMout.period(1.0/400000.0);
arb 2:69a3683834c7 163
arb 1:0402db7e91f5 164
arb 1:0402db7e91f5 165 }
jderemer3 0:4931c25e1cf7 166
jderemer3 0:4931c25e1cf7 167 int main() {
arb 1:0402db7e91f5 168 init();
arb 1:0402db7e91f5 169
arb 1:0402db7e91f5 170 t1.start(ArmThread);
arb 1:0402db7e91f5 171 t2.start(LEDThread);
arb 1:0402db7e91f5 172 t3.start(AudioThread);
arb 1:0402db7e91f5 173 t4.start(WheelsThread);
jderemer3 0:4931c25e1cf7 174
jderemer3 0:4931c25e1cf7 175 }