Make UR Own

Monday, January 19, 2015

How to use a photoresistor to control on board LED or any device of your choice |Tiva C TM4C123GXL

Components:

Tiva C Launchpad
1 Photoresistor
1k Resistor


Circuit Diagram:





Code:

Note:
you can change the threshold value based on your photoresistor.
In case if you want to control some other device of choice,you can just change the following lines of code in the program

if (voltage>Threshold)
{

//Add the device control program

}

Download the file here  LedExampleDownload

If you need any help, feel free to contact me. Thank you.





Saturday, January 17, 2015

Advanced Obstacle Avoiding Robot

How it is different?
  • It uses interrupt method instead of polling method
  • Fast response and efficient
  • Filters noise, thus gives most accurate distance of the obstacle

Algorithm:




When we start the robot, it moves in a straight direction. This robot continuously scans over a range from 0 to 180 degrees.
90 degree -straight position
0 degree - right 
180 degree - left
45 degree-right diagonal
135 degree- left diagonal

In case the robot encounters any obstacle in the straight path (90 degree), it stops and reverses itself for 10 cm. It then scans to the right(0 degree) then left(180 degree) and chooses the best path.
However, if it encounters an obstacle in the right diagonal(45 degree),  it immediately checks for the left diagonal clearance and takes a turn in that direction. The same method can be applied for the left diagonal. Further, if it encounters a complete blockage over 180 degree then the robot stops.

Components used:
2- continuous servos
1 standard micro servo (To scan)
1- 5 V voltage regulator (LM7805)
1- 9 V battery
2- 10uF capacitor 
1- HCSR04 ultrasonic sensor
1- USB power bank(optional)

Connections:

PC5 - Trigger pin of HCSR04
PC6 - Echo pin of HCSR04
PF1 - PWM5 - Left servo
PF2 - PWM6 - Right servo
PF3 - PWM7 - Standard servo




Power Supply to the board:

Note: You must connect the ground pin of LM7805 with the ground pin of launchpad. It is recommended to use separate power supply for servos and microcontroller as servos draw more amps during switching times which can not be supplied by the launchpad.

Working Video:

Program Logic:

Variables used..
int moveRightFrwd = 10;
int moveRightBcwd = 170;
int moveLeftFrwd = 170;
int moveLeftBcwd = 10;
int stopRight = 90;
int stopLeft = 90;
int Radarstop = 90;
int thresholdLimit = 18;
int diagThresholdLimit = 13;
int leftDistance, rightDistance,diagRightDistance,diagLeftDistance;
long distance;
int straightBlock=0;
int timeout;


Here I am providing the initialization routine that I have used.


void hardwareSetup()
{

//set 40MHz clock..
ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |SYSCTL_OSC_MAIN|SYSCTL_RCC_PWMDIV_64);//PWM Frequency=625kHz

//enable F, B and C peripherals
SYSCTL_RCGC2_R =SYSCTL_RCGC2_GPIOF| SYSCTL_RCGC2_GPIOC|SYSCTL_RCGC2_GPIOB;

//Configure C & F pins..
GPIO_PORTC_DEN_R |= 0x60;
GPIO_PORTC_DIR_R=0x20;
GPIO_PORTC_DIR_R &= ~0x40;
GPIO_PORTF_DIR_R |= 0x0E;
GPIO_PORTF_DEN_R |= 0x0E;
GPIO_PORTF_AFSEL_R |= 0x0E;
GPIO_PORTF_PCTL_R = GPIO_PCTL_PF1_M1PWM5|GPIO_PCTL_PF2_M1PWM6|GPIO_PCTL_PF3_M1PWM7;
 
//Enable the PWM1...
SYSCTL_RCGCPWM_R |= 0x02;
SYSCTL_SRPWM_R = SYSCTL_SRPWM_R1;
SYSCTL_SRPWM_R = 0;//reset pwm
PWM1_2_CTL_R = 0;//block 2 reset
PWM1_3_CTL_R = 0;//block 3 reset
PWM1_2_GENB_R = PWM_1_GENB_ACTCMPBD_ZERO | PWM_1_GENB_ACTLOAD_ONE;//select the mode of PWM
PWM1_3_GENA_R = PWM_1_GENA_ACTCMPAD_ZERO | PWM_1_GENA_ACTLOAD_ONE;
PWM1_3_GENB_R = PWM_1_GENB_ACTCMPBD_ZERO | PWM_1_GENB_ACTLOAD_ONE;

//load the count value  .
PWM1_2_LOAD_R = 11362;
PWM1_3_LOAD_R = 11362;
PWM1_2_CMPB_R = 0;
PWM1_3_CMPB_R = 0;
PWM1_3_CMPA_R = 0;
PWM1_2_CTL_R = PWM_1_CTL_ENABLE;
PWM1_3_CTL_R = PWM_1_CTL_ENABLE;
PWM1_ENABLE_R = PWM_ENABLE_PWM5EN | PWM_ENABLE_PWM6EN | PWM_ENABLE_PWM7EN;//enable PWM5,6,7

//timer 1 configuration
 GPIO_PORTB_AFSEL_R |= 0x10;
 GPIO_PORTB_PCTL_R |=GPIO_PCTL_PB4_T1CCP0;//timer 1
 GPIO_PORTB_DEN_R |= 0x10;

 SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R1;
 TIMER1_CTL_R &= ~TIMER_CTL_TAEN;//reset timer1
 TIMER1_CFG_R =TIMER_CFG_32_BIT_TIMER;
 TIMER1_TAMR_R =TIMER_TAMR_TAMR_PERIOD;
 TIMER1_TAILR_R=0xC3500;//40000000/800000=50Hz 20ms configure
 TIMER1_IMR_R= TIMER_IMR_TATOIM; // enable timer1 interrupt for every 20ms
 NVIC_EN0_R |= 1<<21;//interrupt vector
 TIMER1_CTL_R  |= TIMER_CTL_TAEN; //enable timer1

}

void timer1InterruptRoutineForRobot()
{
//you can write the control program here
}

You must include the timer1InterruptRoutineForRobot() routine in startup_ccs.c file, corresponding to the timer1 interrupt vector. The timer should fire whenever the robot encounters an obstacle.
You can achieve this by checking the condition leftDistance <18|| rightDistance<18 (i.e thresholdLimit)  diagRightDistance<13 || diagLeftDistance<13 (i.e diagThresholdLimit) in the timer routine.

I hope this helps you get started.

Thank you.