You are on page 1of 41

Name : Israt Jahan

ID:-02191916
[Here I have included Recitation 1-5, and all the source codes. For rest of the
recitations that were not updated , I have to send the scanned copy . ( Recitation 3 and
4 are missing) that I will include in another word document with my scanned copy with
your signature. Please grade these as Professor told me to submit. Thank you.]

Code for recitation 1 :


/* Recitation 1 Hello, world
my name is Israt */

printf("Hello, world\n");
printf("my name is Israt\n");

Code for recitation 2:


#include<stdio.h>
main()
{
char first_name[10], last_name[10], full_name[20], i, j;

printf("Enter your first name");


scanf("%s", first_name);
//*if it scans only one character thn u'll hv to use a
loop. in tht case let me knw. i dnt remember actually. bt hope it'll work :) *//
printf("Enter your last name");
scanf("%s", last_name);

for(i=0; first_name[i]!='\0'; ++i)


{
full_name[i]=first_name[i];
}
full_name[i]='\0';

for(i=0; full_name[i]!='\0'; ++i);


for(j=0; last_name[j]!='\0'; ++j, ++i)
{
full_name[i]=last_name[j];
}
full_name[i]='\0';

printf("Your full name is: %s",full_name);

system("pause");
}
Code for recitation 5 :
#include<stdio.h>
#include<string.h>

#include <stdlib.h>

#define size 100

int main(void)
{
char v_source[size],r_source[size],c;
double v_src,r_src;
double ca[size];
double pa[size],r_load[size];
double max_power=0;
int max_index;

double temp1, temp2;


int i=0;

printf("Enter source voltage: ");


gets(v_source);
v_src = atof(v_source);
printf("You entered %f volts as Vs. \n", v_src);

i=0;
printf("Enter source resistance: ");
gets(r_source);
r_src = atof(r_source);
printf("You entered %f Ohms as Rs. \n", r_src);

i=0;
printf("\n\nPlotting pa[] vs ca[] arrays:\n");
for(i=0;i<size;i++)
{
ca[i]=rand()%9+1;
r_load[i]=ca[i]*r_src;

temp1=v_src*v_src*r_load[i];
temp2=r_load[i]+r_src;
temp2*=temp2;
pa[i]=temp1/temp2;

printf("Power: %f

->

ca[%d] = %f\n",pa[i],i,r_load[i]);

if(pa[i]>max_power)
{
max_power=pa[i];
max_index=i;
}

}
}

Source code 1:
#include "derivative.h" /* include peripheral declarations */
#include "TFC\TFC.h"
#include <math.h>

#define steering_gain 1.2


#define steering_limit_high 0.6
#define steering_limit_low -0.6
//---------------------------------------------------------// Load the Notch Template Values (global array)
// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18
// number of elements in the Notch Template
float notch[Nnotch]; // Notch Template (vector should be zero mean and
normalized....)
void set_template_values(){
notch[0] = 0.334462147117700;
notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;
notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;
notch[14] = 0.133004013367428;
notch[15] = 0.231390543803607;
notch[16] = 0.315721855606047;
notch[17] = 0.334462147117700;
}
//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;
int i,j;
Nnotch_center = round(Nnotch/2);
vpeak_index = -1;
vpeak
= -1000;
//---------------------------------// Go through camera line data
//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];

vsum
+= vcomp[j]; // start the mean processing
}
vmean = vsum/((float)Nnotch); // compute the mean of the segment
//--------------------------// Subtract mean
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum
+= (vcomp[j]*vcomp[j]); // start the L2 norm
processing
}
vdenom = sqrt(vsum); // compute L2 norm of segment
if(vdenom != 0){
vrecip = 1/vdenom; // take reciprocal so we minimize
expensive divisions
//--------------------------// Normalize (if not zero)
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum
+= vcomp[j]*notch[j]; // perform the
inner product for L2 norm
}
vnorm = vsum; // the L2 norm
}else{
vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1; // add offset so that
the center is at index 64
}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;
vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain; // implement steering gain to control
how fast it turns to center
if(vturn > steering_limit_high){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_high;
}
if(vturn < steering_limit_low){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_low;
}
}

return vturn;

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
finding the line....

// load the line following template for

for(;;)
{

//TFC_Task must be called in your main loop.


certain processing happy (I.E. Serial port queue check
TFC_Task();

This keeps

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{
default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//-------------------------------------

--------

// Upload linescan data (as bytes) to

Matlab

//-------------------------------------

--------

for(i=0;i<128;i++){
TERMINAL_PRINTF("%X",LineScanImage0[i]); // send raw binary....
}
TERMINAL_PRINTF("\r\n");
//------------------------------------// Compute and set steering angle
//------------------------------------TFC_SetServo(0,compute_steering_angle());
}
//-----------------------------------------------

// Control power to rear motors via switch 1


//--------------------------------------------if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;

TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot0());
via potentiometer (constant speed)

// speed is set

}else{
motors are off

TFC_SetMotorPWM(0,0); //Make sure


TFC_HBRIDGE_DISABLE;

}
//put a pattern on the LEDs....
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 1:
the on-board potentiometers

//Demo mode 1 will just move the servos with


if(TFC_Ticker[0]>=20)
{
TFC_Ticker[0] = 0; //reset the Ticker
//Every 20 mSeconds, update the Servos
TFC_SetServo(0,TFC_ReadPot0());
TFC_SetServo(1,TFC_ReadPot1());
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
TFC_SetMotorPWM(0,0); //Make sure motors are

off

TFC_HBRIDGE_DISABLE;
break;
case 2 :

motors move

//Demo Mode 2 will use the Pots to make the


TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot1());

//Let's put a pattern on the LEDs


if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 3 :
//Demo Mode 3 will be in Freescale Garage Mode.
It will beam data from the Camera to the
//Labview Application
if(TFC_Ticker[0]>100 && LineScanImageReady==1)
{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");
if(t==0)
t=3;
else
t--;
TFC_SetBatteryLED_Level(t);
for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);
}
TERMINAL_PRINTF("\r\n");
/*

for(i=0;i<128;i++)
{

TERMINAL_PRINTF("%X",LineScanImage1[i]);

if(i==127)

TERMINAL_PRINTF("\r\n",LineScanImage1[i]);
else
TERMINAL_PRINTF(",",LineScanImage1[i]);
}

*/

break;

}
}

return 0;

Source code 2 :
#include "derivative.h" /* include peripheral declarations */
#include "TFC\TFC.h"
#include <math.h>
#define steering_gain 1.2
#define steering_limit_high 0.6
#define steering_limit_low -0.6
//---------------------------------------------------------// Load the Notch Template Values (global array)
// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18
// number of elements in the Notch Template
float notch[Nnotch]; // Notch Template (vector should be zero mean and
normalized....)
void set_template_values(){
notch[0] = 0.334462147117700;
notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;
notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;
notch[14] = 0.133004013367428;
notch[15] = 0.231390543803607;
notch[16] = 0.315721855606047;
notch[17] = 0.334462147117700;
}
//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;

int i,j;
Nnotch_center = round(Nnotch/2);
vpeak_index = -1;
vpeak
= -1000;
//---------------------------------// Go through camera line data
//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];
vsum
+= vcomp[j]; // start the mean processing
}
vmean = vsum/((float)Nnotch); // compute the mean of the segment
//--------------------------// Subtract mean
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum
+= (vcomp[j]*vcomp[j]); // start the L2 norm
processing
}
vdenom = sqrt(vsum); // compute L2 norm of segment
if(vdenom != 0){
vrecip = 1/vdenom; // take reciprocal so we minimize
expensive divisions
//--------------------------// Normalize (if not zero)
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum
+= vcomp[j]*notch[j]; // perform the
inner product for L2 norm
}
vnorm = vsum; // the L2 norm
}else{
vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1; // add offset so that
the center is at index 64
}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;

vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain;
how fast it turns to center
if(vturn > steering_limit_high){
wheels don't hit the car
vturn = steering_limit_high;
}
if(vturn < steering_limit_low){
wheels don't hit the car
vturn = steering_limit_low;
}
}

// implement steering gain to control


// limit the turn angle so that the

// limit the turn angle so that the

return vturn;

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
finding the line....

// load the line following template for

for(;;)
{
//TFC_Task must be called in your main loop.
certain processing happy (I.E. Serial port queue check
TFC_Task();

This keeps

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{
default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//-------------------------------------------Matlab

// Upload linescan data (as bytes) to


//-------------------------------------

--------

for(i=0;i<128;i++){

TERMINAL_PRINTF("%X",LineScanImage0[i]); // send raw binary....


}
TERMINAL_PRINTF("\r\n");
//------------------------------------// Compute and set steering angle

//------------------------------------TFC_SetServo(0,compute_steering_angle());
}
//----------------------------------------------

// Control power to rear motors via switch 1


//---------------------------------------------

--

if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot0());
// speed is set
via potentiometer (constant speed)
}else{
TFC_SetMotorPWM(0,0); //Make sure
motors are off
TFC_HBRIDGE_DISABLE;
}
//put a pattern on the LEDs....
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 1:
//Demo mode 1 will just move the servos with
the on-board potentiometers

if(TFC_Ticker[0]>=1)
{
TFC_Ticker[0] = 0; //reset the Ticker
//Every 20 mSeconds, update the Servos
TFC_SetServo(0,TFC_ReadPot0());
TFC_SetServo(1,TFC_ReadPot1());
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}

TFC_SetMotorPWM(0,0); //Make sure motors are


off

TFC_HBRIDGE_DISABLE;
break;
case 2 :

motors move

//Demo Mode 2 will use the Pots to make the


TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot1());
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 3 :

//Demo Mode 3 will be in Freescale Garage Mode.


It will beam data from the Camera to the
//Labview Application
if(TFC_Ticker[0]>100 && LineScanImageReady==1)
{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");
if(t==0)
t=3;
else
t--;
TFC_SetBatteryLED_Level(t);
for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);

/*

}
TERMINAL_PRINTF("\r\n");
for(i=0;i<128;i++)

{
TERMINAL_PRINTF("%X",LineScanImage1[i]);

if(i==127)

TERMINAL_PRINTF("\r\n",LineScanImage1[i]);
else
TERMINAL_PRINTF(",",LineScanImage1[i]);

*/

break;
}

return 0;
}

Source code 3 :
#include "derivative.h" /* include peripheral declarations */
#include "TFC\TFC.h"
#include <math.h>
#define steering_gain 1.2
#define steering_limit_high 0.6
#define steering_limit_low -0.6
//---------------------------------------------------------// Load the Notch Template Values (global array)
// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18
// number of elements in the Notch Template
float notch[Nnotch]; // Notch Template (vector should be zero mean and
normalized....)
void set_template_values(){
notch[0] = 0.334462147117700;
notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;
notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;

notch[14]
notch[15]
notch[16]
notch[17]

=
=
=
=

0.133004013367428;
0.231390543803607;
0.315721855606047;
0.334462147117700;

}
//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;
int i,j;
Nnotch_center = round(Nnotch/2);
vpeak_index = -1;
vpeak
= -1000;
//---------------------------------// Go through camera line data
//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];
vsum
+= vcomp[j]; // start the mean processing
}
vmean = vsum/((float)Nnotch); // compute the mean of the segment
//--------------------------// Subtract mean
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum
+= (vcomp[j]*vcomp[j]); // start the L2 norm
processing
}
vdenom = sqrt(vsum); // compute L2 norm of segment
if(vdenom != 0){
vrecip = 1/vdenom; // take reciprocal so we minimize
expensive divisions
//--------------------------// Normalize (if not zero)
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum
+= vcomp[j]*notch[j]; // perform the
inner product for L2 norm
}
vnorm = vsum; // the L2 norm

}else{

vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1;
the center is at index 64
}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;
vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain; // implement
how fast it turns to center
if(vturn > steering_limit_high){
// limit the
wheels don't hit the car
vturn = steering_limit_high;
}
if(vturn < steering_limit_low){
// limit the
wheels don't hit the car
vturn = steering_limit_low;
}
}

// add offset so that

steering gain to control


turn angle so that the

turn angle so that the

return vturn;

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
finding the line....

// load the line following template for

for(;;)
{
//TFC_Task must be called in your main loop.
certain processing happy (I.E. Serial port queue check
TFC_Task();

This keeps

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{
default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;

LineScanImageReady=0;
//-------------------------------------

--------

// Upload linescan data (as bytes) to

Matlab

//-------------------------------------

--------

for(i=0;i<128;i++){
TERMINAL_PRINTF("%X",LineScanImage0[i]); // send raw binary....
}
TERMINAL_PRINTF("\r\n");
//------------------------------------// Compute and set steering angle
//------------------------------------TFC_SetServo(0,compute_steering_angle());
}
//----------------------------------------------

// Control power to rear motors via switch 1


//---------------------------------------------

--

if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot(0));
// speed is set
via potentiometer (constant speed)
}else{
TFC_SetMotorPWM(0,0); //Make sure
motors are off
TFC_HBRIDGE_DISABLE;
}
//put a pattern on the LEDs....
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 1:
//Demo mode 1 will just move the servos with
the on-board potentiometers

if(TFC_Ticker[0]>=1)
{

TFC_Ticker[0] = 0; //reset the Ticker


//Every 20 mSeconds, update the Servos
TFC_SetServo(0,TFC_ReadPot0());
TFC_SetServo(1,TFC_ReadPot1());
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
TFC_SetMotorPWM(0,0); //Make sure motors are
off

TFC_HBRIDGE_DISABLE;
break;
case 2 :

motors move

//Demo Mode 2 will use the Pots to make the


TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot(0),TFC_ReadPot(1));
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 3 :

//Demo Mode 3 will be in Freescale Garage Mode.


It will beam data from the Camera to the
//Labview Application
if(TFC_Ticker[0]>100 && LineScanImageReady==1)
{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");

if(t==0)
t=3;
else
t--;
TFC_SetBatteryLED_Level(t);
for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);
}
TERMINAL_PRINTF("\r\n");
/*

for(i=0;i<128;i++)
{

TERMINAL_PRINTF("%X",LineScanImage1[i]);

if(i==127)

TERMINAL_PRINTF("\r\n",LineScanImage1[i]);
else
TERMINAL_PRINTF(",",LineScanImage1[i]);

*/

break;
}

return 0;
}

Source code 4:
#include "derivative.h" /* include peripheral declarations */
#include "TFC\TFC.h"
#include <math.h>
#define steering_gain 1.2
#define steering_limit_high 0.6
#define steering_limit_low -0.6
//---------------------------------------------------------// Load the Notch Template Values (global array)
// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18
// number of elements in the Notch Template
float notch[Nnotch]; // Notch Template (vector should be zero mean and
normalized....)
void set_template_values(){
notch[0] = 0.334462147117700;

notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;
notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;
notch[14] = 0.133004013367428;
notch[15] = 0.231390543803607;
notch[16] = 0.315721855606047;
notch[17] = 0.334462147117700;

//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;
int i,j;
Nnotch_center = round(Nnotch/2);
vpeak_index = -1;
vpeak
= -1000;
//---------------------------------// Go through camera line data
//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];
vsum
+= vcomp[j]; // start the mean processing
}
vmean = vsum/((float)Nnotch); // compute the mean of the segment
//--------------------------// Subtract mean
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum
+= (vcomp[j]*vcomp[j]); // start the L2 norm
processing
}
vdenom = sqrt(vsum); // compute L2 norm of segment

if(vdenom != 0){
vrecip = 1/vdenom; // take reciprocal so we minimize
expensive divisions
//--------------------------// Normalize (if not zero)
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum
+= vcomp[j]*notch[j]; // perform the
inner product for L2 norm
}
vnorm = vsum; // the L2 norm
}else{
vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1; // add offset so that
the center is at index 64
}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;
vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain; // implement steering gain to control
how fast it turns to center
if(vturn > steering_limit_high){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_high;
}
if(vturn < steering_limit_low){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_low;
}
return vturn;
}

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
finding the line....
for(;;)
{

// load the line following template for

//TFC_Task must be called in your main loop.


certain processing happy (I.E. Serial port queue check
TFC_Task();

This keeps

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{
default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//--------------------------------------------

// Upload linescan data (as bytes) to

Matlab

//-------------------------------------

--------

for(i=0;i<120;i++){

TERMINAL_PRINTF("%X",LineScanImage0[i]); // send raw binary....


}
TERMINAL_PRINTF("\r\n");
//------------------------------------// Compute and set steering angle
//------------------------------------TFC_SetServo(0,compute_steering_angle());
}
--

//--------------------------------------------// Control power to rear motors via switch 1


//---------------------------------------------

--

if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;

TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot(0));
// speed is set
via potentiometer (constant speed)
}else{
TFC_SetMotorPWM(0,0); //Make sure
motors are off
TFC_HBRIDGE_DISABLE;
}
//put a pattern on the LEDs....
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{

t=0;
}
TFC_SetBatteryLED_Level(t);

break;
case 1:
//Demo mode 1 will just move the servos with
the on-board potentiometers

off

if(TFC_Ticker[0]>=1)
{
TFC_Ticker[0] = 0; //reset the Ticker
//Every 20 mSeconds, update the Servos
TFC_SetServo(0,TFC_ReadPot0());
TFC_SetServo(1,TFC_ReadPot1());
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
TFC_SetMotorPWM(0,0); //Make sure motors are
TFC_HBRIDGE_DISABLE;
break;

case 2 :
//Demo Mode 2 will use the Pots to make the
motors move

TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot(0),TFC_ReadPot(1));
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;

case 3 :
//Demo Mode 3 will be in Freescale Garage Mode.
It will beam data from the Camera to the
//Labview Application
if(TFC_Ticker[0]>100 && LineScanImageReady==1)
{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");
if(t==0)
t=3;
else
t--;
TFC_SetBatteryLED_Level(t);
for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);

}
TERMINAL_PRINTF("\r\n");

/*

for(i=0;i<128;i++)
{

TERMINAL_PRINTF("%X",LineScanImage1[i]);
if(i==127)
TERMINAL_PRINTF("\r\n",LineScanImage1[i]);

else

TERMINAL_PRINTF(",",LineScanImage1[i]);
}

*/

break;

}
}

return 0;

Source Code 5:
#include "derivative.h" /* include peripheral declarations */

#include "TFC\TFC.h"
#include <math.h>
#define steering_gain 1.2
#define steering_limit_high 0.6
#define steering_limit_low -0.6
//---------------------------------------------------------// Load the Notch Template Values (global array)
// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18
// number of elements in the Notch Template
float notch[Nnotch]; // Notch Template (vector should be zero mean and
normalized....)
void set_template_values(){
notch[0] = 0.334462147117700;
notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;
notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;
notch[14] = 0.133004013367428;
notch[15] = 0.231390543803607;
notch[16] = 0.315721855606047;
notch[17] = 0.334462147117700;
}
//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;
int i,j;
Nnotch_center = round(Nnotch/2);
vpeak_index = -1;
vpeak
= -1000;
//---------------------------------// Go through camera line data
//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//---------------------------

vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];
vsum
+= vcomp[j]; // start the mean processing
}
vmean = vsum/((float)Nnotch); // compute the mean of the segment
//--------------------------// Subtract mean
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum
+= (vcomp[j]*vcomp[j]); // start the L2 norm
processing
}
vdenom = sqrt(vsum); // compute L2 norm of segment
if(vdenom != 0){
vrecip = 1/vdenom; // take reciprocal so we minimize
expensive divisions
//--------------------------// Normalize (if not zero)
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum
+= vcomp[j]*notch[j]; // perform the
inner product for L2 norm
}
vnorm = vsum; // the L2 norm
}else{
vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1; // add offset so that
the center is at index 64
}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;
vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain; // implement steering gain to control
how fast it turns to center
if(vturn > steering_limit_high){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_high;
}
if(vturn < steering_limit_low){
// limit the turn angle so that the
wheels don't hit the car
vturn = steering_limit_low;
}

return vturn;

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
finding the line....

// load the line following template for

for(;;)
{
//TFC_Task must be called in your main loop.
certain processing happy (I.E. Serial port queue check
TFC_Task();

This keeps

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{
default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//--------------------------------------------

// Upload linescan data (as bytes) to

Matlab

//-------------------------------------

--------

for(i=0;i<120;i++){

TERMINAL_PRINTF("%X",LineScanImage0[i]); // send raw binary....


}
TERMINAL_PRINTF("\r\n");
//------------------------------------// Compute and set steering angle
//------------------------------------TFC_SetServo(0,compute_steering_angle());
}
--

//--------------------------------------------// Control power to rear motors via switch 1


//---------------------------------------------

--

if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;

TFC_SetMotorPWM(TFC_ReadPot0(),TFC_ReadPot(0));
// speed is set
via potentiometer (constant speed)
}else{
TFC_SetMotorPWM(0,0); //Make sure
motors are off
TFC_HBRIDGE_DISABLE;
}
//put a pattern on the LEDs....
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 1:
//Demo mode 1 will just move the servos with
the on-board potentiometers

off

if(TFC_Ticker[0]>=1)
{
TFC_Ticker[0] = 0; //reset the Ticker
//Every 20 mSeconds, update the Servos
TFC_SetServo(0,TFC_ReadPot(0));
TFC_SetServo(1,TFC_ReadPot(1));
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
TFC_SetMotorPWM(0,0); //Make sure motors are
TFC_HBRIDGE_DISABLE;
break;

case 2 :
//Demo Mode 2 will use the Pots to make the
motors move

TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot(0),TFC_ReadPot(1));
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;
case 3 :
//Demo Mode 3 will be in Freescale Garage Mode.
It will beam data from the Camera to the
//Labview Application
if(TFC_Ticker[0]>100 && LineScanImageReady==1)
{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");
if(t==0)
t=3;
else
t--;
TFC_SetBatteryLED_Level(t);
for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);

}
TERMINAL_PRINTF("\r\n");

/*

for(i=0;i<128;i++)
{

TERMINAL_PRINTF("%X",LineScanImage1[i]);
if(i==127)
TERMINAL_PRINTF("\r\n",LineScanImage1[i]);

else

TERMINAL_PRINTF(",",LineScanImage1[i]);
}
}

*/

break;
}

return 0;
}

Final code for the race day:


#include "derivative.h" /* include peripheral declarations */
#include "TFC\TFC.h"
#include <math.h>

#define steering_gain 1.2


#define steering_limit_high

0.6

#define steering_limit_low

-0.6

//---------------------------------------------------------// Load the Notch Template Values (global array)


// (Cut and paste the C code that Matlab generated below)
//---------------------------------------------------------#define Nnotch 18

// number of elements in the Notch Template

float notch[Nnotch];
normalized....)

// Notch Template (vector should be zero mean and

void set_template_values(){
notch[0] = 0.334462147117700;
notch[1] = 0.273556199704827;
notch[2] = 0.179854742146561;
notch[3] = 0.062727920198728;
notch[4] = -0.030973537359538;
notch[5] = -0.143415286429457;

notch[6] = -0.227746598231897;
notch[7] = -0.312077910034337;
notch[8] = -0.335503274423903;
notch[9] = -0.330818201545990;
notch[10] = -0.255857035499377;
notch[11] = -0.176210796574851;
notch[12] = -0.068454120382844;
notch[13] = 0.015877191419595;
notch[14] = 0.133004013367428;
notch[15] = 0.231390543803607;
notch[16] = 0.315721855606047;
notch[17] = 0.334462147117700;
}

//-------------------------------------------------------------------// Compute what the steering angle should be from the camera data
//-------------------------------------------------------------------float compute_steering_angle(){
float vcomp[Nnotch];
float vsum,vmean,vdenom,vrecip,vnorm,vpeak,vturn;
float vturn1;
int offset_index;
int vpeak_index;
int Nnotch_center;
int i,j;

Nnotch_center = round(Nnotch/2);
vpeak_index = -1;

vpeak

= -1000;

//---------------------------------// Go through camera line data


//---------------------------------for(i=0; i<128-Nnotch+1; i++){
//--------------------------// Grab segment of line data
//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] = LineScanImage0[i+j];
vsum

+= vcomp[j];

// start the mean processing

}
vmean = vsum/((float)Nnotch);

// compute the mean of the segment

//--------------------------// Subtract mean


//--------------------------vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] -= vmean;
vsum

+= (vcomp[j]*vcomp[j]);

// start the L2 norm processing

}
vdenom = sqrt(vsum);

// compute L2 norm of segment

if(vdenom != 0){
vrecip = 1/vdenom;

// take reciprocal so we minimize expensive

divisions
//--------------------------// Normalize (if not zero)
//---------------------------

vsum = 0;
for(j=0; j<Nnotch; j++){
vcomp[j] *= vrecip;
vsum

+= vcomp[j]*notch[j];

// perform the inner

product for L2 norm


}
vnorm = vsum; // the L2 norm
}else{
vnorm = 0;
}
//-----------------------------------// Find the maximum cross-correlation
//-----------------------------------if(vpeak < vnorm){
vpeak = vnorm;
vpeak_index = i + Nnotch_center - 1;
center is at index 64

// add offset so that the

}
}
//-----------------------------------// Calculate how much to turn....
//-----------------------------------offset_index = vpeak_index-64;
vturn1 = (float)offset_index*0.6;
vturn = vturn1/64 * steering_gain;
how fast it turns to center

// implement steering gain to control

if(vturn > steering_limit_high){


wheels don't hit the car

// limit the turn angle so that the

vturn = steering_limit_high;
}

if(vturn < steering_limit_low){


wheels don't hit the car

// limit the turn angle so that the

vturn = steering_limit_low;
}

return vturn;
}

int main(void)
{
uint32_t t;
int i=0;
TFC_Init();
set_template_values();
the line....

// load the line following template for finding

for(;;)
{
//TFC_Task must be called in your main loop.
processing happy (I.E. Serial port queue check

This keeps certain

TFC_Task();

//This Demo program will look at the middle 2 switch to


select one of 4 demo modes.
//Let's look at the middle 2 switches
switch((TFC_GetDIP_Switch()>>1)&0x03)
{

default:
case 0 :
if(TFC_Ticker[0]>100 && LineScanImageReady==1){
TFC_Ticker[0] = 0;
LineScanImageReady=0;

//--------------------------------------------// Upload linescan data (as bytes) to Matlab


//--------------------------------------------for(i=0;i<128;i++){
TERMINAL_PRINTF("%X",LineScanImage0[i]);
// send raw binary....
}
TERMINAL_PRINTF("\r\n");

//------------------------------------// Compute and set steering angle


//------------------------------------TFC_SetServo(0,compute_steering_angle());

//----------------------------------------------// Control power to rear motors via switch 1


//----------------------------------------------if(TFC_GetDIP_Switch()&0x01){
TFC_HBRIDGE_ENABLE;

TFC_SetMotorPWM(TFC_ReadPot(0),TFC_ReadPot(0));
// speed is set via potentiometer (constant speed)
}else{
TFC_SetMotorPWM(0,0); //Make sure motors are off
TFC_HBRIDGE_DISABLE;
}

//put a pattern on the LEDs....


if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}

break;

case 1:

//Demo mode 1 will just move the servos with the onboard potentiometers
if(TFC_Ticker[0]>=20)
{
TFC_Ticker[0] = 0; //reset the Ticker
//Every 20 mSeconds, update the Servos

TFC_SetServo(0,TFC_ReadPot(0));
TFC_SetServo(1,TFC_ReadPot(1));
}
//Let's put a pattern on the LEDs
if(TFC_Ticker[1] >= 125)
{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}

TFC_SetMotorPWM(0,0); //Make sure motors are off


TFC_HBRIDGE_DISABLE;

break;

case 2 :

//Demo Mode 2 will use the Pots to make the motors


move
TFC_HBRIDGE_ENABLE;
TFC_SetMotorPWM(TFC_ReadPot(0),TFC_ReadPot(1));

//Let's put a pattern on the LEDs

if(TFC_Ticker[1] >= 125)


{
TFC_Ticker[1] = 0;
t++;
if(t>4)
{
t=0;
}
TFC_SetBatteryLED_Level(t);
}
break;

case 3 :

//Demo Mode 3 will be in Freescale Garage Mode.


will beam data from the Camera to the
//Labview Application

if(TFC_Ticker[0]>100 && LineScanImageReady==1)


{
TFC_Ticker[0] = 0;
LineScanImageReady=0;
//TERMINAL_PRINTF("\r\n");
//TERMINAL_PRINTF("L:");

if(t==0)
t=3;
else

It

t--;

TFC_SetBatteryLED_Level(t);

for(i=0;i<128;i++)
{
TERMINAL_PRINTF("%X",LineScanImage0[i]);
}
TERMINAL_PRINTF("\r\n");

/*

for(i=0;i<128;i++)
{

TERMINAL_PRINTF("%X",LineScanImage1[i]);
if(i==127)
TERMINAL_PRINTF("\r\n",LineScanImage1[i]);
else
TERMINAL_PRINTF(",",LineScanImage1[i]);
}

break;
}

*/

return 0;
}