pot_cap_charge.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include <pigpio.h>
00004 
00005 /*
00006    Measure how long a capacitor takes to charge through a resistance.
00007 
00008    A potentimeter is used to vary the resistance.
00009 
00010    The time taken will be proportional to the resistance.
00011 
00012    3V3 ----- Potentiometer --+-- Capacitor ----- Ground
00013                              |
00014                              +-- gpio
00015 
00016 
00017   gcc -o pot_cap_charge pot_cap_charge.c -lpigpio -lpthread -lrt
00018   sudo ./pot_cap_charge
00019 
00020 */
00021 
00022 #define GPIO 25
00023 
00024 #define MAX_READING 1000
00025 
00026 static uint32_t rechargeTick = 0;
00027 
00028 void callback(int gpio, int level, uint32_t tick)
00029 {
00030    static uint32_t smooth = 0;
00031    static int reading = 0;
00032 
00033    uint32_t raw;
00034 
00035    if (level == 1) /* measure recharge time */
00036    {
00037       ++reading;
00038 
00039       if (rechargeTick)
00040       {
00041          raw = tick - rechargeTick; /* set in main */
00042 
00043          if (raw < MAX_READING) /* ignore outliers */
00044          {
00045             /* smooth using 0.8 * smooth + 0.2 * raw */
00046 
00047             smooth = (raw + (4 * smooth)) / 5;
00048 
00049             printf("%d %d %d\n", reading, raw, smooth);
00050          }
00051          else
00052          {
00053             /* ignore outlier, set dot at fixed position */
00054             printf("%d %d %d\n", reading, 40, smooth);
00055          }
00056       }
00057       else
00058       {
00059          /* ignore reschedule, set dot at fixed position */
00060          printf("%d %d %d\n", reading, 20, smooth);
00061       }
00062    }
00063 }
00064 
00065 int main (int argc, char *argv[])
00066 {
00067    uint32_t t1, t2;
00068    int tDiff;
00069 
00070    if (gpioInitialise()<0) return 1;
00071 
00072    gpioSetAlertFunc(GPIO, callback); /* callback when GPIO changes state */
00073     
00074    while (1)
00075    {
00076       gpioWrite(GPIO, PI_OFF); /* drain capacitor */
00077 
00078       gpioDelay(200); /* microseconds */
00079 
00080       t1 = gpioTick();
00081 
00082       gpioSetMode(GPIO, PI_INPUT); /* start capacitor recharge */
00083 
00084       t2 = gpioTick();
00085 
00086       /* dump reading if rechargeTick not accurate to 3 micros */
00087 
00088       if ((t2 - t1) < 3) rechargeTick = t1; else rechargeTick = 0;
00089 
00090       gpioDelay(5000); /* microseconds, nominal 200 readings per second */
00091    }
00092 
00093    gpioTerminate();
00094 }


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57