Este es un proyecto que he estado realizando...es un poco complejo ya que lo estoy haciendo con el material que tengo en casa.
Caracterizticas que podria tener el Robot:
-Controlado via RF con joystick o Teclado
-GLCD
-Memoria MMC
-Autonomo: usando Sensores infrarojos.
-Sguidor de linea
El algoritmo apenas lo estoy realizando pero para que vean el avanze aqui lo muestro:
Donde el codigo anterior es el Codigo Princial o de control.
El que va a controlar la etapa de entradas y salidas, como los sensores infrarojos y motores respectivamente es el siguiente:
Caracterizticas que podria tener el Robot:
-Controlado via RF con joystick o Teclado
-GLCD
-Memoria MMC
-Autonomo: usando Sensores infrarojos.
-Sguidor de linea
El algoritmo apenas lo estoy realizando pero para que vean el avanze aqui lo muestro:
Código:
////////////////////////////////////////////////////////
// PROYECTO CON SANDWOROT ////
//Autor: george.manson.69 ////
//Lugar: Mexico ////
//Compilador: HI TECH PIC18 (LITE MODE) ////
////////////////////////////////////////////////////////
#include<htc.h>
#include<stdio.h>
/////////////////////////////////////////////////////////////
//Configuracion para trabajar Con oscilador interno de 8Mhz
__CONFIG(1,INTIO & FCMDIS & IESODIS & PLLDIV5 & PLLPOSTDIV2 & CPUDIV1 & USBOSC);
/////////////////////////////////////////////////////////////
__CONFIG(2,VREGDIS & PWRTEN & BORDIS & WDTDIS & BORV45 & WDTPS32K);
__CONFIG(3,PBDIGITAL & LPT1DIS & MCLREN);
__CONFIG(4,STVRDIS & LVPDIS & ICPORTDIS & DEBUGDIS);
__CONFIG(5,UNPROTECT);
__CONFIG(6,UNPROTECT);
__CONFIG(7,UNPROTECT);
//////////////////////////////
//Frecuencia FOSC 8Mhz
//////////////////////////////
#define _XTAL_FREQ 8000000
#include"libSDHARD/MMChardware.h"
#include"intro_image.h"
#include"libGLCD/glcd.h" //Libreria para trabajar con GLCD
#include"softuart/softserial.h"
//////////////////////////////
//DEFINICIONES GLOBALES ///
//////////////////////////////
#define IMAGEN_LOGO 100
#define IMAGEN_SELECCION 102
//////////////////////////////
//Variable Globales //
//////////////////////////////
volatile unsigned char BUFF[512]@0x400;//Accedemos a 1kbyte del USB
//////////////////////////////
//FUNCION DE 1 SEG //
//////////////////////////////
void DELAY1S(void){
unsigned char time;
for(time=0;time<100;time++){
__delay_ms(10);
}
}
////////////////////////////////////////////////////
//FUNCION QUE TOMA DE LA TARJETA LAS IMAGENES ///
//Y DESPUES LAS PONE EN EL GLCD ///
//YA QUE UNA IMAGEN PUEDE TENER 1024 BYTE, Y SOLO///
//LA TARJETA PUEDE TOMAR 512 BYTES ENTONCES ///
//SE PIDE PRIMERO LOS 512 BYTE Y DESPUES EL RESTO///
////////////////////////////////////////////////////
unsigned char GLCD_MMC_image(unsigned long dir,unsigned char color){
unsigned char i,j,error;
volatile unsigned char *ptr;
signed char k;
for(unsigned char s=0;s<2;s++){
if(!SDCard_read_block(dir+s,&BUFF)){
return 1;
}
ptr=&BUFF;
for(i=s*32;i<(s*32)+32;i++){
for(j=0;j<16;j++){
for(k=7;k>-1;--k){
if((*ptr&(unsigned char)(1<<(7-k)))!=0){
GLCD_point((j*8)+k,i,color);
error=0;
}
}
*ptr++;
}
}
}
return error;
}
/////////////////////////////////////////////////
//Funcion de interrupcion
//Si no se usa simplemente no hacemos nada...
//Esto sirve para direccionar lo los datos
//en un lugar muy cercano al Inicio de la memoria
//de datos
////////////////////////////////////////////////
static void interrupt
isr(void){}
//////////////////////////////
//FUNCION PRINCIPAL
//////////////////////////////
void main(void){
OSCCON=0x70;//Oscilador a 8Mhz
NOP();NOP();NOP();NOP();
//////////////////////////////////////////////////////////////////////
// Configuracion de Puertos ///
//////////////////////////////////////////////////////////////////////
TRISA=0b00011101;//RA0->RX RA1->TX
TRISB=0x01;//SDI,CLK
TRISC=0; //SD0,CS
TRISD=0; //DB0~DB7
TRISE=0; //
PORTB=0;
ADCON0=0;
ADCON1=0x0F;
CMCON=0x07;
/*---------------------Fin de Conf. Puertos-------------------------*/
//////////////////////////////////////////////////////////////////////
// Configuracion del USART por Software ///
//////////////////////////////////////////////////////////////////////
SOFTUSART_init();
/*---------------------Fin de Conf. UART----------------------------*/
//////////////////////////////////////////////////////////////////////
// Configuracion de SPI ///
//////////////////////////////////////////////////////////////////////
OpenSPI(SPI_FOSC_4 , //Modo Master,clock=Fosc/4
MODE_11 , //modo de L_H
SMPMID);
/*---------------------Fin de Conf. SPI-----------------------------*/
printf("SANDWOROT V2.0\r\n");
/////////////////////////////////////////////////////////////////////
//Inicializa GLCD Y MMC /////
/////////////////////////////////////////////////////////////////////
//iNICIALIZA GLCD
GLCD_init(1);
GLCD_clear(1);
//Dibuja imagen
GLCD_image(intro_image,0);
DELAY1S();
GLCD_clear(1);
//INICIALIZA MMC
if(!SDCard_init()){
GLCD_text(0,0,(unsigned char*) "MMC ERROR",1,0);
while(1);
}else
GLCD_text(0,0,(unsigned char*)"MMC SUCCESSFUL",1,0);
DELAY1S();
//MANDA A DIBUJAR LAS IMAGENES A MOSTRAR EN EL GLCD
GLCD_clear(1);
if(GLCD_MMC_image(IMAGEN_LOGO,0)){
GLCD_clear(1);
GLCD_text(0,0,(unsigned char*)"ERROR AL CARGAR",1,0);
while(1);
}
DELAY1S();
/*----------------------Fin de Conf. Inicializacion---------------*/
/////////////////////////////////////////////////////////////////////
//ETAPA PARA SELECCION QUE VA HACER EL ROBOT /////
//1.-COMO SEGUIDOR DE LINEA /////
//2.-AUTONOMO /////
//3.-CONTROLADO POR RADIOFRECUENCIA /////
/////////////////////////////////////////////////////////////////////
//Manda imagen para seleccionar funcion
while(1){
GLCD_clear(0);
if(GLCD_MMC_image(IMAGEN_SELECCION,1)){
GLCD_clear(1);
GLCD_text(0,0,(unsigned char*)"ERROR AL CARGAR",1,1);
while(1);
}
while((PORTA&0b00011100)>>2==0x07)continue; //Espera la seleccion
if(!RA2){
GLCD_box(1,20,125,33,1);
GLCD_text(2,21,(unsigned char*)"1.-SEGUIDOR DE LINEA",1,0);
}
if(!RA3){
GLCD_box(1,34,125,47,1);
GLCD_text(2,38,(unsigned char*)"2.-AUTONOMO",1,0);
}
if(!RA4){
GLCD_box(1,48,125,61,1);
GLCD_text(2,52,(unsigned char*)"3.-CONTROLADO POR RF",1,0);
}
DELAY1S();
}
/*----------------------Fin de Conf. SELECCION---------------*/
while(1);
}
Donde el codigo anterior es el Codigo Princial o de control.
El que va a controlar la etapa de entradas y salidas, como los sensores infrarojos y motores respectivamente es el siguiente:
Código:
///////////////////////////////////////////////////////////
//TITULO: Control de entradas y salidas ///
//AUTOR: george.manson.69
//COMPILADOR: HI TECH (LITE MODE)
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include<htc.h> //Incluimos libreria del micro a usar
#include<stdio.h>
__CONFIG(UNPROTECT & WDTDIS & PWRTEN & INTIO & MCLREN & LVPDIS);
__CONFIG(DEBUGDIS & BORDIS);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//UNPROTECT = Codigo no Potegido
//WDTDIS = Watch Dog Timer Desactivado
//PWRTEN = Power Activado
//INTIO = Osiclador interno
//MCLREN = Activamos Master Clear
//LVPDIS = Low Voltage Porgramming Desactivado
//DEBUGDIS = Desactiva Debug
//BOR = Desactivado
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define _XTAL_FREQ 8000000 //Oscilador Interno de 8MHZ
#include"libUSART/usartdr.h" //Libreria creada para uso del usart
#include"libADC/adcdr.h"//Inluimos libreria de trabajo con ADC
#include"timer1/timer1dr.h" //Libreria para uso del timer1
#include"timer2/timer2.h"//LLama la libreria de usar Timer2
#include"pwmdr/pwm.h"//LLama libreria para usar PWM
////////////////////////////////
//Definiciones de Trabajo
////////////////////////////////
#define bit_set(var,b) var |= (1<<b)
#define bit_clear(var,b) var &= ~(1<<b)
////////////////////////////////
//Variables Globales
////////////////////////////////
unsigned char dato[10];
const unsigned char SYNDATA[]={0X18,0X10,0X30,0X20,0X60,0X40,0XC0,0X80,0X08,0X0C,0X04,0X06,0X02,0X03,0X01,0};
const unsigned int RESULDAT[]={0xFFFF,0xE0FF,0xC1FF,0xA2FF,0x83FF,0x64FF,0x45FF,0x26FF,0xFFE0,0xFFC1,0xFFA2,0xFF83,0xFF64,0xFF45,0xFF26,0x0707};
////////////////////////////////
//Definiciones de variables de trabajo
////////////////////////////////
#define CMD dato[0]
#define FOLLOWING_LINE '1'
////////////////////////////////
//Funcion de Pedir DATOS SENSORES
////////////////////////////////
unsigned char get_SIN(unsigned char choice){
unsigned char data=0;
PORTB=~(choice<<2);
for(unsigned char x=0;x<=7;x++){
PORTB=(x<<4)|(PORTB&0x0F);
__delay_us(125);
if((PORTB&choice)==choice){
bit_set(data,x);
}
else{
bit_clear(data,x);
}
}
PORTB=(choice<<2);
PORTB&=0b10001111;
return data;
}
////////////////////////////////////////
//Funcion de Controlar MOtores ////
////////////////////////////////////////
void set_motors(unsigned char dir,unsigned char Motor_left_duty,unsigned char Motor_right_duty){
PORTC=((dir&0X03)<<4);
PwmDuty(Motor_left_duty);
PwmDuty2(Motor_right_duty);
}
/////////////////////////////////////////////////
//Funcion de interrupcion
//Si no se usa simplemente no hacemos nada...
//Esto sirve para direccionar lo los datos
//en un lugar muy cercano al Inicio de la memoria
//de datos
////////////////////////////////////////////////
static void interrupt
isr(void){
if(RCIF){
RCIF=0;
CMD=getch();
}
}
//////////////////////////////////////////////
//Funcion Principal
//////////////////////////////////////////////
void main(void){
///////////////////////////////////
//Registro para trabajar a 8MHZ
///////////////////////////////////
OSCCON=0x70;
while(HTS==0); //Es estable la frecuencia ?
TRISA=0x07;
TRISC=0x80; //RB1=RX,RB2=TX
PORTC=0;
TRISB=0x03;
PORTB=0x0F;
//Configuracion del USART
OpenUSART(51,OFF,OFF); //value=(FOSC/(baud*16))-1
//SIN INTERRUPCIONES
//a 9600 baudios
//Configuracion del ADC
OpenADC(FOSC_FRC,AN0,OFF);
//Oscilador interno
//AN0 como entrada analogo
//PORTB sin analoga
//OpenTIMER1(pr_8,ON);
//Interrupcion timpo=(1/(FOSC/4))*preescalar*(256-timer1)
// 200mS=tiempo maximo
// timer1=+65536-{tiempo/[(1/(FOSC/4))*preescalar]}
//TMR1LOAD(15550);
//Configuramos TIMER 2 para PWM
OpenTimer2(PRESCALE_16);
//Usamos libreria PWM
OpenPwm(124);
//PWM period = [(period ) + 1] x 4 x Tosc x TMR2 prescaler
//PWM period = [(255)+1]x(4/4Mhz)x16
// [.001s/((4/8Mhz)*16)]-1=period
// [1/(f*(4/Tosc)*preescalar)]-1=period
PwmDuty(0); //512=100% 255=50% de duty
PwmDuty2(0);
GIE=1; //INTERRUPCIONES GLOBALES DesACTIVADAS
PEIE=1; //DesACTIVA INTERURPCIONES POR PERIFERICOS
while(1){
/*OUR CODE HERE*/
CMD=getch();
if(CMD==FOLLOWING_LINE){
CMD=0x00;
RCIE=1;//Activamos interrupcion por Recepcion
while(CMD==0x00){
dato[1]=get_SIN(1);
for(unsigned char x=0;x<sizeof(SYNDATA);x++){
if(dato[1]==SYNDATA[x]){
dato[2]=(unsigned char)(RESULDAT[x]>>8);
dato[3]=(unsigned char)(RESULDAT[x]&0X00FF);
break;
}
}
set_motors(0,dato[2],dato[3]);
}
RCIE=0;
set_motors(0,0,0);
}
}
}