Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
I know that we need PWM signals to drive the servo and that PIC has PWM signal generator. But I can't use it unless I reduce the frequency of my oscillator. If I want to keep using the 20MHz oscillator, can anyone teach me how to generate the PWM?
For the sensor readings, I tried using arrays to store the values. The codes below are the codes I used to test whether I've managed to know what is inside the arrays. I use the datalogging and hyperterminal as I've read in the main website to see the values. But I ended up getting nothing in the hyperterminal screen. Did I do anything wrong in my codes?
while(1){ portb = 0xff; delay_us(1500); portb = 0x00; delay_us(18500);}
Period doesnt have to be exactly 20ms, so instead of waiting using delay function you can do something useful during that time. But it has to be somewhere around 20ms.
//Thesis: Construction of a Closed-loop Vision System//Sensor Scanning System ver.1////Description://- move servoH from -80 to +80 degree//- scan environment at each degree//- if the sensor detects object:// --> for SensorDown @ distance less than 110 cm// --> for SensorUp @ distance less than 80 cm// --> stop the movement and start moving servoV up n down///****************** PIC Setup ******************/#include <16f876a.h>#device adc=8 //8-bit ADC#use delay(clock=20000000)#fuses hs,noprotect,nowdt,nolvp//setup uart for serial communication with PC#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,PARITY=N)#byte PORTA=5#byte PORTB=6#byte PORTC=7#define servoH PIN_C4#define servoV PIN_C3#define STATUS_LED PIN_B7#define WARNING_LED PIN_B6#define SWITCH1 PIN_B0#define SensorDown 0 //Red sensor#define SensorUp 1 //Black sensor#define Num_Measurements 5/***** Variable and function declarations *****/int i, count, stop;long value, UpValue, DownValue;long total[2] = {0};void init_ports();void init_adc(int channel);void read();void scan();void check();void init_values();void MoveServoH();void MoveServoV();/*************** Main Function ***************/void main(){ //initialize ports init_ports(); portb=0xff; //test if pic is working or not output_low(STATUS_LED); //turn on status led (B7) MoveServoH(); //start moving horizontally}//end main/************ Additional Functions ************///initialize portsvoid init_ports(){ set_tris_b(0x01); //all port b as output except b0 set_tris_c(0x80); //all port c as output except c7 //setup port A as analog inputs, Vdd as reference //0-5V --> 0-255 setup_port_a(ALL_ANALOG);}//end init_ports//initialize adc channelvoid init_adc(int channel){ //use PIC clock as ADC clock setup_adc(ADC_CLOCK_INTERNAL); //set adc channel set_adc_channel(channel); delay_us(50);}//end init_adc//read from current adc channelvoid read(){ value = read_adc(); setup_adc(ADC_OFF);}//end read//scan environmentvoid scan(){ //Every around 60 ms if(count%3 == 0 && i < Num_Measurements) { //Get SensorDown data init_adc(SensorDown); //Get measurement read(); total[SensorDown] = total[SensorDown] + value; //Get SensorUp data init_adc(SensorUp); read(); total[SensorUp] = total[SensorUp] + value; i++; }}//end scan//check the adc valuevoid check(){ //if SensorDown detected object at distance less than 110 cm (26) //or SensorUp detected object at distance less than 80 cm (39) if(DownValue > 26 || UpValue > 39) { output_low(WARNING_LED); //light up warning led stop = 1; //stop the current movement }}//end check//reinitialize some valuesvoid init_values(){ i = 0; count = 0; stop = 0; total[SensorDown] = 0; total[SensorUp] = 0; delay_ms(500); output_high(WARNING_LED);}//Move servoH from -80 to +80 degree//initial position is 0 degree then move to +80 then -80void MoveServoH(){ while(1) { init_values(); //initialize all values //Set motor to 0 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(600); //Set horizontal servo control to low output_low(servoH); delay_us(18500); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 20 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(800); //Set horizontal servo control to low output_low(servoH); delay_us(18300); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 40 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(1000); //Set horizontal servo control to low output_low(servoH); delay_us(18100); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 60 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(1200); //Set horizontal servo control to low output_low(servoH); delay_us(17900); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 80 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(1400); //Set horizontal servo control to low output_low(servoH); delay_us(17700); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 60 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(1200); //Set horizontal servo control to low output_low(servoH); delay_us(17900); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 40 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(1000); //Set horizontal servo control to low output_low(servoH); delay_us(18100); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 20 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(800); //Set horizontal servo control to low output_low(servoH); delay_us(18300); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to 0 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(600); //Set horizontal servo control to low output_low(servoH); delay_us(18500); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -20 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(400); //Set horizontal servo control to low output_low(servoH); delay_us(18700); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -40 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(200); //Set horizontal servo control to low output_low(servoH); delay_us(18900); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -60 degree for about 3 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoH); //Set horizontal servo control to low output_low(servoV); delay_us(19100); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -80 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(700); //Set vertical servo control to low output_low(servoH); delay_us(200); //Set horizontal servo control to low output_low(servoV); delay_us(19100); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -60 degree for about 3 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); //Set horizontal servo control to low output_low(servoH); delay_us(19100); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -40 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(200); //Set horizontal servo control to low output_low(servoH); delay_us(18900); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } init_values(); //initialize all values //Set motor to -20 degree for about 2 sec while (count != 100) { //Set horizontal servo control to high output_high(servoH); //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(400); //Set horizontal servo control to low output_low(servoH); delay_us(18700); //delay such that overall period of PWM is around 20 ms count++; scan(); //scan environment //after 5 consecutive measurements (when around 300 ms has passed) if(i == Num_Measurements) { //Get average values of each sensor DownValue = total[SensorDown]/Num_Measurements; UpValue = total[SensorUp]/Num_Measurements; check(); //check whether the values exceed the requirement } if(stop == 1) { moveServoV(); break; } } }//end while}void MoveServoV(){ //initialize count count = 0; //Set servoV to -60 degree for about 3 sec while (count != 150) { //Set vertical servo control to high output_high(servoV); delay_us(700); //Set vertical servo control to low output_low(servoV); delay_us(19300); //delay such that overall period of PWM is around 20 ms count++; } count = 0; //Set servoV to -45 degree for about 3 sec while (count != 150) { //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(19100); //delay such that overall period of PWM is around 20 ms count++; } count = 0; //Set motor to -30 degree for about 3 sec while (count != 150) { //Set vertical servo control to high output_high(servoV); delay_us(1100); //Set vertical servo control to low output_low(servoV); delay_us(18900); //delay such that overall period of PWM is around 20 ms count++; } init_values(); //initialize all values //Set servoV to -45 degree for about 3 sec while (count != 150) { //Set vertical servo control to high output_high(servoV); delay_us(900); //Set vertical servo control to low output_low(servoV); delay_us(19100); //delay such that overall period of PWM is around 20 ms count++; }}