Cómo hacer un Robo-botones (3 / 3 paso)

Paso 3: Descargar el código fuente.

El código fuente sigue y se une en un archivo de texto:

 /********************************************************************* follow.c ** -------- ** runs on Create Command Module ** cover all but small opening on the front of the IR sensor ** Create will follow a virtual wall (or any IR sending out the ** force-field signal) and hopefully avoid obstacles in the process *********************************************************************/#include interrupt.h>#include io.h>#include#include "oi.h"#define TRUE 1#define FALSE 0#define FullSpeed 0x7FFF#define SlowSpeed 0x0100#define SearchSpeed 0x0100#define ExtraAngle 10#define SearchLeftAngle 125#define SearchRightAngle (SearchLeftAngle - 1000)#define CoastDistance 150#define TraceDistance 250#define TraceAngle 30#define BackDistance 25#define IRDetected (~PINB & 0x01)//states#define Ready 0#define Following 1#define WasFollowing 2#define SearchingLeft 3#define SearchingRight 4#define TracingLeft 5#define TracingRight 6#define BackingTraceLeft 7#define BackingTraceRight 8// Global variablesvolatile uint16_t timer_cnt = 0;volatile uint8_t timer_on = 0;volatile uint8_t sensors_flag = 0;volatile uint8_t sensors_index = 0;volatile uint8_t sensors_in[Sen6Size];volatile uint8_t sensors[Sen6Size];volatile int16_t distance = 0;volatile int16_t angle = 0;volatile uint8_t inRange = 0;// Functionsvoid byteTx(uint8_t value);void delayMs(uint16_t time_ms);void delayAndCheckIR(uint16_t time_ms);void delayAndUpdateSensors(unsigned int time_ms);void initialize(void);void powerOnRobot(void);void baud(uint8_t baud_code);void drive(int16_t velocity, int16_t radius);uint16_t randomAngle(void);void defineSongs(void);int main (void){//state variableuint8_t state = Ready;int found = 0;int wait_counter = 0;// Set up Create and moduleinitialize();LEDBothOff;powerOnRobot();byteTx(CmdStart);baud(Baud28800);byteTx(CmdControl);byteTx(CmdFull);// set i/o for second IR sensorDDRB &= ~0x01; //set cargo bay ePort pin 3 to be an inputPORTB |= 0x01; //set cargo ePort pin3 pullup enabled// program loopwhile(TRUE){// Stop just as a precautiondrive(0, RadStraight);// set LEDsbyteTx(CmdLeds);byteTx(((sensors[SenVWall])?LEDPlay:0x00) | (inRange?LEDAdvance:0x00));byteTx(sensors[SenCharge1]);byteTx(64);IRDetected?LED2On:LED2Off;inRange?LED1On:LED1Off;//looking for user button, check oftendelayAndUpdateSensors(10);delayAndCheckIR(10);if(UserButtonPressed) {delayAndUpdateSensors(1000);//active loopwhile(!(UserButtonPressed)&&(!sensors[SenCliffL])&&(!sensors[SenCliffFL])&&(!sensors[SenCliffFR])&&(!sensors[SenCliffR])) {byteTx(CmdLeds);byteTx(((sensors[SenVWall])?LEDPlay:0x00) | (inRange?LEDAdvance:0x00));byteTx(sensors[SenCharge1]);byteTx(255);IRDetected?LED2On:LED2Off;inRange?LED1On:LED1Off;switch(state) {case Ready:if(sensors[SenVWall]) {//check for proximity to leaderif(inRange) {drive(0,RadStraight);} else {//drive straightdrive(SlowSpeed, RadStraight);state = Following;}} else {//search for the beamangle = 0;distance = 0;wait_counter = 0;found = FALSE;drive(SearchSpeed, RadCCW);state = SearchingLeft;}break;case Following:if(sensors[SenBumpDrop] & BumpRight) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceLeft;} else if(sensors[SenBumpDrop] & BumpLeft) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceRight;} else if(sensors[SenVWall]) {//check for proximity to leaderif(inRange) {drive(0,RadStraight);state = Ready;} else {//drive straightdrive(FullSpeed, RadStraight);state = Following;}} else {//just lost the signal, keep going slowly one cycledistance = 0;drive(SlowSpeed, RadStraight);state = WasFollowing;}break;case WasFollowing:if(sensors[SenBumpDrop] & BumpRight) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceLeft;} else if(sensors[SenBumpDrop] & BumpLeft) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceRight;} else if (sensors[SenVWall]) {//check for proximity to leaderif(inRange) {drive(0,RadStraight);state = Ready;} else {//drive straightdrive(FullSpeed, RadStraight);state = Following;}} else if (distance >= CoastDistance) {drive(0,RadStraight);state = Ready;} else {drive(SlowSpeed, RadStraight);}break;case SearchingLeft:if(found) {if (angle >= ExtraAngle) {drive(SlowSpeed, RadStraight);state = Following;} else {drive(SearchSpeed, RadCCW);}} else if (sensors[SenVWall]) {found = TRUE;angle = 0;if (inRange) {drive(0,RadStraight);state = Ready;} else {drive(SearchSpeed, RadCCW);}} else if (angle >= SearchLeftAngle) {drive(SearchSpeed, RadCW);wait_counter = 0;state = SearchingRight;} else {drive(SearchSpeed, RadCCW);}break;case SearchingRight:if(found) {if (-angle >= ExtraAngle) {drive(SlowSpeed, RadStraight);state = Following;} else {drive(SearchSpeed, RadCW);}} else if (sensors[SenVWall]) {found = TRUE;angle = 0;if (inRange) {drive(0,RadStraight);state = Ready;} else {drive(SearchSpeed, RadCCW);}} else if(wait_counter > 0) {wait_counter -= 20;drive(0,RadStraight);} else if (angle = SearchRightAngle) {drive(0,RadStraight);wait_counter = 5000;angle = 0;} else {drive(SearchSpeed, RadCW);}break;case TracingLeft:if(sensors[SenBumpDrop] & BumpRight) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceLeft;} else if(sensors[SenBumpDrop] & BumpLeft) {drive(0, RadStraight);state=Ready;} else if (sensors[SenVWall]) {//check for proximity to leaderif(inRange) {drive(0,RadStraight);state = Ready;} else {//drive straightdrive(SlowSpeed, RadStraight);state = Following;}} else if (!(distance >= TraceDistance)) {drive(SlowSpeed, RadStraight);} else if (!(-angle >= TraceAngle)) {drive(SearchSpeed, RadCW);} else {distance = 0;angle = 0;drive(SlowSpeed, RadStraight);state = Ready;}break;case TracingRight:if(sensors[SenBumpDrop] & BumpRight) {drive(0, RadStraight);state=Ready;} else if(sensors[SenBumpDrop] & BumpLeft) {distance = 0;angle = 0;drive(-SlowSpeed, RadStraight);state=BackingTraceRight;} else if (sensors[SenVWall]) {//check for proximity to leaderif(inRange) {drive(0,RadStraight);state = Ready;} else {//drive straightdrive(SlowSpeed, RadStraight);state = Following;}} else if (!(distance >= TraceDistance)) {drive(SlowSpeed, RadStraight);} else if (!(angle >= TraceAngle)) {drive(SearchSpeed, RadCCW);} else {distance = 0;angle = 0;drive(SlowSpeed, RadStraight);state = Ready;}break;case BackingTraceLeft:if (sensors[SenVWall] && inRange) {drive(0,RadStraight);state = Ready;} else if (angle >=TraceAngle) {distance = 0;angle = 0;drive(SlowSpeed, RadStraight);state = TracingLeft;} else if (-distance >= BackDistance) {drive (SearchSpeed, RadCCW);} else {drive(-SlowSpeed, RadStraight);}break;case BackingTraceRight:if (sensors[SenVWall] && inRange) {drive(0,RadStraight);state = Ready;} else if (-angle >=TraceAngle) {distance = 0;angle = 0;drive(SlowSpeed, RadStraight);state = TracingRight;} else if (-distance >= BackDistance) {drive (SearchSpeed, RadCW);} else {drive(-SlowSpeed, RadStraight);}break;default://stopdrive(0,RadStraight);state = Ready;break;}delayAndCheckIR(10);delayAndUpdateSensors(10);}//cliff or userbutton detected, allow condition to stabilize (eg, button to be released)drive(0,RadStraight);delayAndUpdateSensors(2000);}}}// Serial receive interrupt to store sensor valuesSIGNAL(SIG_USART_RECV){uint8_t temp;temp = UDR0;if(sensors_flag){sensors_in[sensors_index++] = temp;if(sensors_index >= Sen6Size)sensors_flag = 0;}}// Timer 1 interrupt to time delays in msSIGNAL(SIG_OUTPUT_COMPARE1A){if(timer_cnt)timer_cnt--;elsetimer_on = 0;}// Transmit a byte over the serial portvoid byteTx(uint8_t value){while(!(UCSR0A & _BV(UDRE0))) ;UDR0 = value;}// Delay for the specified time in ms without updating sensor valuesvoid delayMs(uint16_t time_ms){timer_on = 1;timer_cnt = time_ms;while(timer_on) ;}// Delay for the specified time in ms and check second IR detectorvoid delayAndCheckIR(uint16_t time_ms){uint8_t timer_val = 0;inRange = 0;timer_on = 1;timer_cnt = time_ms;while(timer_on) {if(!(timer_val == timer_cnt)) {inRange += IRDetected;timer_val = timer_cnt;}}inRange = (inRange>=(time_ms>>1));}// Delay for the specified time in ms and update sensor valuesvoid delayAndUpdateSensors(uint16_t time_ms){uint8_t temp;timer_on = 1;timer_cnt = time_ms;while(timer_on){if(!sensors_flag){for(temp = 0; temp Sen6Size; temp++)sensors[temp] = sensors_in[temp];// Update running totals of distance and angledistance += (int)((sensors[SenDist1] 8) | sensors[SenDist0]);angle += (int)((sensors[SenAng1] 8) | sensors[SenAng0]);byteTx(CmdSensors);byteTx(6);sensors_index = 0;sensors_flag = 1;}}}// Initialize the Mind Control's ATmega168 microcontrollervoid initialize(void){cli();// Set I/O pinsDDRB = 0x10;PORTB = 0xCF;DDRC = 0x00;PORTC = 0xFF;DDRD = 0xE6;PORTD = 0x7D;// Set up timer 1 to generate an interrupt every 1 msTCCR1A = 0x00;TCCR1B = (_BV(WGM12) | _BV(CS12));OCR1A = 71;TIMSK1 = _BV(OCIE1A);// Set up the serial port with rx interruptUBRR0 = 19;UCSR0B = (_BV(RXCIE0) | _BV(TXEN0) | _BV(RXEN0));UCSR0C = (_BV(UCSZ00) | _BV(UCSZ01));// Turn on interruptssei();}void powerOnRobot(void){// If Create's power is off, turn it onif(!RobotIsOn){while(!RobotIsOn){RobotPwrToggleLow;delayMs(500); // Delay in this stateRobotPwrToggleHigh; // Low to high transition to toggle powerdelayMs(100); // Delay in this stateRobotPwrToggleLow;}delayMs(3500); // Delay for startup}}// Switch the baud rate on both Create and modulevoid baud(uint8_t baud_code){if(baud_code = 11){byteTx(CmdBaud);UCSR0A |= _BV(TXC0);byteTx(baud_code);// Wait until transmit is completewhile(!(UCSR0A & _BV(TXC0))) ;cli();// Switch the baud rate registerif(baud_code == Baud115200)UBRR0 = Ubrr115200;else if(baud_code == Baud57600)UBRR0 = Ubrr57600;else if(baud_code == Baud38400)UBRR0 = Ubrr38400;else if(baud_code == Baud28800)UBRR0 = Ubrr28800;else if(baud_code == Baud19200)UBRR0 = Ubrr19200;else if(baud_code == Baud14400)UBRR0 = Ubrr14400;else if(baud_code == Baud9600)UBRR0 = Ubrr9600;else if(baud_code == Baud4800)UBRR0 = Ubrr4800;else if(baud_code == Baud2400)UBRR0 = Ubrr2400;else if(baud_code == Baud1200)UBRR0 = Ubrr1200;else if(baud_code == Baud600)UBRR0 = Ubrr600;else if(baud_code == Baud300)UBRR0 = Ubrr300;sei();delayMs(100);}}// Send Create drive commands in terms of velocity and radiusvoid drive(int16_t velocity, int16_t radius){byteTx(CmdDrive);byteTx((uint8_t)((velocity >> 8) & 0x00FF));byteTx((uint8_t)(velocity & 0x00FF));byteTx((uint8_t)((radius >> 8) & 0x00FF));byteTx((uint8_t)(radius & 0x00FF));} 

Artículos Relacionados

¿Cómo hacer flores de botones

¿Cómo hacer flores de botones

por lo que usted desea hacer flores de botones, eh?Esta es una guía paso a paso simple para hacer tus propio lindos pequeñas flores de la nada pero botones y pegamento!Paso 1: Preparar tu material. Se necesita:Botones (por lo menos cuatro pequeños y
Cómo hacer Ramos de flores de fieltro con botones y cuentas de madera

Cómo hacer Ramos de flores de fieltro con botones y cuentas de madera

¿Buscas un Ramos de flores? ¿Qué tal estos DIY fieltro Flores? El siguiente tutorial Pandahall le dirá cómo hacer este ramo de flores de fieltro.Paso 1: Materiales y herramientas necesitan en DIY el Ramos de flores de fieltro:joyería que hace fuentes
Cómo hacer productos de los botones

Cómo hacer productos de los botones

tantas cosas se pueden hacer de los botones...Paso 1: Cortan sobre 18 (45,7 cm) de alambre. Lleve uno de los extremos de la misma a través de la parte posterior del primer botón, dejando un 4 – cola de 5″(12cm).Paso 2: Coser el otro extremo del cable
Hacer su propia costumbre botones

Hacer su propia costumbre botones

una de las cosas más frustrantes para mí es no tener el color correcto exacto o el tamaño de adorno para mi proyecto. El agresor más frecuente es botones. Con el color perfecto será demasiado grande. La adecuada serán sólo extraño bastante a ser desa
Botones de monedas

Botones de monedas

hice otro instructivo donde hice conchos y encaje con cuartos.La respuesta fue muy positiva y me llevó a hacer una con botones sólo. He hecho botones fuera de cuartos, Dimes, Nickels y Pennies. Incluso nueva centavos, post 1960 en el que hay una gran
Hacer un Robot inalámbrico con un teléfono con Android

Hacer un Robot inalámbrico con un teléfono con Android

En este Instructable vamos a construir un MouseBot.El MouseBot es un robot pequeño arduino wireless que se puede controlar con un ordenador. No requiere un módulo Wi-Fi. En su lugar utiliza un teléfono con android para conectarse a internet. El teléf
Como hacer el jailbreak (sin ataduras) 3.0 y 2.2.1

Como hacer el jailbreak (sin ataduras) 3.0 y 2.2.1

actualización: el dev team ha liberado una nueva versión de redsn0w y saurik ha lanzado nuevas versiones de sustrato móvil y winterboard para el 3GS!-champster2.2.1 es solo para el ipod touch 2G.Y también los datos que, Cyle/cyleleghorn/A.C.E., he añ
Pendientes de botón - sin utilizar botones cubierta de tela -

Pendientes de botón - sin utilizar botones cubierta de tela -

Aquí es una manera de cómo hacer aretes de botón sin utilizar botones cubierta de tela.Me cosió una falda y no tenía pendientes de conexión. Buscado en internet pero sólo se encuentran Diy´s para hacer aretes con botones cubierta de tela. Me didn´t q
Cómo hacer un simple controlador de juegos de PC programa de prueba

Cómo hacer un simple controlador de juegos de PC programa de prueba

en este instructable, te mostraré cómo hacer un sencillo programa que permite probar los botones de su dispositivo de juego de PC para que sepa que todavía funcionan, o que necesitan ser reemplazados.Debo advertirte, que este instructable es muy prol
Botones de colores de 2

Botones de colores de 2

Yum, botones. Me encanta botones. Cajas llenas de ellos, para ejecutar a través de mis dedos. Especialmente bonito, sustituir aburrido sobre ropa, convertirse en joyas, decorar accesorios con o miren! Sin embargo, a veces, simplemente no puede encont
Hacer un juego de Guitar Hero para PC

Hacer un juego de Guitar Hero para PC

este artículo cubrirá la creación de un kit para tu ordenador que te permite practicar tus habilidades de héroe de la guitarra con software de código abierto y cerca de 30 dólares en piezas.Paso 1: Obtener las piezas de nessisary para Guitarra En pri
Reutilizar una vieja mesa de café para hacer una otomana

Reutilizar una vieja mesa de café para hacer una otomana

este proyecto nace del hecho de que estoy empezando desde cero en mi sala de estar, y yo no tenía ningún lugar para descansar mis pies.Tenía una mesita incrustaciones de latón que mi papá trajo de África. Sí, es bonito, pero es horrible Dios para des
Hacer más rápido tu ordenador instalando Chromium OS

Hacer más rápido tu ordenador instalando Chromium OS

¿Tienen un ordenador viejo que francamente solo no quiere encender ya debido a su falta de velocidad? Bueno, Rejuvenate su computadora antigua, dotándola de Chromium OS, lo que es mucho más rápido y capaz para tareas sencillas como navegar por la web
Botones de ojos de halloween fácil y barato

Botones de ojos de halloween fácil y barato

va a mostrar cómo hacer barato y botones de globo ocular fácil para halloweenm, se necesita.un pincel pequeño.pintura acrílica blanca.pintura actylic verde, azul o marrón.barro viejo regular.un marcador pequeño (del mismo color que la pintura que ele