bobstermyang / arducopter

Automatically exported from code.google.com/p/arducopter
0 stars 0 forks source link

Integrate Audible Alert Code #162

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
With only a 30cent buzzer (see attachment) than can be nicely fit on top of the 
IMU an audible alert capability for low battery (and other) audible alerts can 
be added.

Code is simple:

Index: APM_Config.h
===================================================================
--- APM_Config.h    (revision 2380)
+++ APM_Config.h    (working copy)

+#define PIEZO                         ENABLED
+#define PIEZO_PIN                     AN6
+#define PIEZO_LOW_VOLTAGE             ENABLED

Index: system.pde
===================================================================
--- system.pde  (revision 2380)
+++ system.pde  (working copy)
@@ -188,13 +188,18 @@
    pinMode(SLIDE_SWITCH_PIN, INPUT);   // To enter interactive mode
    pinMode(PUSHBUTTON_PIN, INPUT);     // unused
    DDRL |= B00000100;                  // Set Port L, pin 2 to output for the relay

    #if MOTOR_LEDS == 1
        pinMode(FR_LED, OUTPUT);            // GPS status LED
        pinMode(RE_LED, OUTPUT);            // GPS status LED
        pinMode(RI_LED, OUTPUT);            // GPS status LED
        pinMode(LE_LED, OUTPUT);            // GPS status LED
    #endif
+        
+        #if PIEZO == 1
+          pinMode(PIEZO_PIN,OUTPUT);
+          piezo_beep();
+        #endif

    // Logging:
    // --------
Index: sensors.pde
===================================================================
--- sensors.pde (revision 2380)
+++ sensors.pde (working copy)
@@ -92,7 +92,9 @@
    battery_voltage2 = BATTERY_VOLTAGE(analogRead(BATTERY_PIN2)) * .1 + battery_voltage2 * .9;
    battery_voltage3 = BATTERY_VOLTAGE(analogRead(BATTERY_PIN3)) * .1 + battery_voltage3 * .9;
    battery_voltage4 = BATTERY_VOLTAGE(analogRead(BATTERY_PIN4)) * .1 + battery_voltage4 * .9;

    if(g.battery_monitoring == 1)
        battery_voltage = battery_voltage3; // set total battery voltage, for telemetry stream
    if(g.battery_monitoring == 2)
@@ -105,9 +107,19 @@
    }

    #if BATTERY_EVENT == 1
-       if(battery_voltage < LOW_VOLTAGE)   low_battery_event();
-       if(g.battery_monitoring == 4 && current_total > 
g.pack_capacitylow_battery_event();
-   #endif
+       if ( (battery_voltage < LOW_VOLTAGE) || (g.battery_monitoring == 4 && 
current_total > g.pack_capacity) ) {
+                        low_battery_event();
+                        #if PIEZO_LOW_VOLTAGE == 1
+                        piezo_on();
+                        #endif
+                }
+                else {
+                        #if PIEZO_LOW_VOLTAGE == 1
+                        piezo_off();
+                        #endif
+                }       
+        #endif
+        
 }

 //v: 10.9453, a: 17.4023, mah: 8.2
Index: events.pde
===================================================================
--- events.pde  (revision 2380)
+++ events.pde  (working copy) 

@@ -94,3 +95,24 @@
    PORTL ^= B00000100;
 }

+
+#if PIEZO == 1
+void piezo_on()
+{
+        digitalWrite(PIEZO_PIN,HIGH);
+}
+
+void piezo_off()
+{
+        digitalWrite(PIEZO_PIN,LOW);
+}
+
+void piezo_beep()
+{
+        // Note: This command should not be used in time sensitive loops
+        piezo_on();
+        delay(100);
+        piezo_off();
+}
+#endif
+

Original issue reported on code.google.com by oli...@voltivo.com on 24 May 2011 at 10:20

Attachments:

GoogleCodeExporter commented 9 years ago
Here more info:
http://diydrones.com/profiles/blogs/give-your-arducopter-mega-and?xg_source=msg_
appr_blogpost

Original comment by oli...@voltivo.com on 24 May 2011 at 10:24

GoogleCodeExporter commented 9 years ago
Here the refined code and patch:

Index: ArduCopterMega/APM_Config.h
===================================================================
+#define PIEZO                         ENABLED       //Enables Piezo Code and 
beeps once on Startup to verify operation
+#define PIEZO_PIN                     AN5           //Last pin on the back ADC 
connector
+#define PIEZO_LOW_VOLTAGE             ENABLED       //Enables Tone on reaching 
low battery or current alert
+#define PIEZO_ARMING                  ENABLED       //Two tones on ARM, 1 Tone 
on disarm

Index: ArduCopterMega/motors.pde
===================================================================
--- ArduCopterMega/motors.pde   (revision 2459)
+++ ArduCopterMega/motors.pde   (working copy)
@@ -14,7 +14,10 @@
            if (arming_counter >= ARM_DELAY) {
                motor_armed     = true;
                arming_counter  = ARM_DELAY;
-
+                                #if PIEZO_ARMING == 1
+                                piezo_beep();
+                                piezo_beep();
+                                #endif
                // Remember Orientation
                // ---------------------------
                init_simple_bearing();
@@ -29,7 +32,9 @@
                motor_armed     = false;
                arming_counter  = DISARM_DELAY;
                compass.save_offsets();
-
+                                #if PIEZO_ARMING == 1
+                                piezo_beep();
+                                #endif
            }else{
                arming_counter++;
            }
@@ -144,4 +149,4 @@
                    nav_throttle,
                    angle_boost());
            */
-       //}
\ No newline at end of file
+       //}
Index: ArduCopterMega/system.pde
===================================================================
--- ArduCopterMega/system.pde   (revision 2459)
+++ ArduCopterMega/system.pde   (working copy)
@@ -198,6 +198,11 @@
        pinMode(RI_LED, OUTPUT);            // GPS status LED
        pinMode(LE_LED, OUTPUT);            // GPS status LED
    #endif
+        
+        #if PIEZO == 1
+          pinMode(PIEZO_PIN,OUTPUT);
+          piezo_beep();
+        #endif

    // Logging:
    // --------
Index: ArduCopterMega/sensors.pde
===================================================================
--- ArduCopterMega/sensors.pde  (revision 2459)
+++ ArduCopterMega/sensors.pde  (working copy)
@@ -105,9 +105,26 @@
    }

    #if BATTERY_EVENT == 1
-       if(battery_voltage < LOW_VOLTAGE)   low_battery_event();
-       if(g.battery_monitoring == 4 && current_total > 
g.pack_capacity)    low_battery_event();
-   #endif
+       if ( (battery_voltage < LOW_VOLTAGE) || (g.battery_monitoring == 4 && 
current_total > g.pack_capacity) ) {
+                        low_battery_event();
+                        #if PIEZO_LOW_VOLTAGE == 1
+                        // Only Activate if a battery is connected to avoid 
alarm on USB only
+                        if (battery_voltage1 > 1){ 
+                          piezo_on();
+                        }
+                        else {
+                          piezo_off();
+                        }
+                          
+                        #endif
+                }
+                else {
+                        #if PIEZO_LOW_VOLTAGE == 1
+                          piezo_off();
+                        #endif
+                }       
+        #endif
+        
 }

 //v: 10.9453, a: 17.4023, mah: 8.2
Index: ArduCopterMega/events.pde
===================================================================
--- ArduCopterMega/events.pde   (revision 2459)
+++ ArduCopterMega/events.pde   (working copy)
@@ -94,3 +94,26 @@
    PORTL ^= B00000100;
 }

+
+#if PIEZO == 1
+void piezo_on()
+{
+        digitalWrite(PIEZO_PIN,HIGH);
+        //PORTF |= B00100000;
+}
+
+void piezo_off()
+{
+        digitalWrite(PIEZO_PIN,LOW);
+        //PORTF &= ~B00100000;
+}
+
+void piezo_beep()
+{
+        // Note: This command should not be used in time sensitive loops
+        piezo_on();
+        delay(100);
+        piezo_off();
+}
+#endif
+

Original comment by fueck...@gmail.com on 1 Jun 2011 at 6:46

GoogleCodeExporter commented 9 years ago
Added to latest rev. Thanks!
Jason

Original comment by jasonshort on 19 Sep 2011 at 12:14