Sunday, May 22, 2016

Pulse an IR Laser at Multiple Frequencies with the Raspberry Pi 3


The following C program pulses an Infrared laser at multiple frequencies using the GPIO pin on the Raspberry Pi 3.  The program can be compiled on the Raspberry Pi 3 and is dynamically linked against the Wiring Pi library.

Please note, preemptive time slicing in the stock Raspbian kernel restricts the frequency at which the laser can be pulsed.







/* 
Copyright (c) 2016, Bryan R. Hinton
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

/* set mode of gpio pin on rpi3 */
void gpio_setup(const unsigned int *gpiopin, int mode)
{
pinMode(*gpiopin, mode);
}

/* toggle laser on gpio pin at two different frequencies */
void gpio_toggle_pin(const unsigned int *gpiopin, const unsigned int *delay_a_usec, const unsigned int *delay_b_usec)
{

for(;;) {
/* toggle pin high */
digitalWrite(*gpiopin, HIGH);
/* wait for delay_a_usec microseconds */
delayMicroseconds(*delay_a_usec);
/* toggle pin low */
digitalWrite(*gpiopin, LOW);
/* wait for delay_a_usec microseconds */
delayMicroseconds(*delay_a_usec);

/* toggle pin high */
digitalWrite(*gpiopin, HIGH);
delayMicroseconds(*delay_b_usec);
/* wait for delay_b_usec microseconds */

/* toggle pin low */
digitalWrite(*gpiopin, LOW);
delayMicroseconds(*delay_b_usec);
/* wait for delay_b_usec microseconds */
}

}

int main(int argc, char **argv)
{
/* gpio pin 17 on rpi3 is pin 0 in wiringpi */
const unsigned int gpiopin17 = 0x00;
/* 500000 microseconds */
const unsigned int delay_a_usec = 500000;
/* 225000  microseconds */
const unsigned int delay_b_usec = 225000;

/* no parameters allowed */
if(argc > 1) {
printf("error: argc > 1\n");
return EXIT_FAILURE;
}
/* setup wiring pi library. requires root */
if(wiringPiSetup() < 0) { 

printf("wiringPiSetup() error\n");
return EXIT_FAILURE;
}

/* set gpio pin to mode */
gpio_setup(&gpiopin17, OUTPUT);

/* toggle laser at two different frequencies */
gpio_toggle_pin(&gpiopin17, &delay_a_usec, &delay_b_usec);

return EXIT_SUCCESS;
}

No comments:

Post a Comment