Add Valve class, change Makefile and Servomatic.ino
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 5 Nov 2024 14:22:14 +0000 (15:22 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 5 Nov 2024 14:26:26 +0000 (15:26 +0100)
The main function needs to have init() before the declaration of first
Valve, because the Valve constructor needs the arduino initialized. One
cannot declare Valve before calling init().
Due to this and me liking variable declaration at the start of a
function, I've written the actual contents of main to a new function.

Makefile
Servomatic.ino
Valve.h [new file with mode: 0644]
Valve.ino [new file with mode: 0644]

index 9adcd9eecc8348b5122e36817c2b770933b2f596..3bf682cb3bbb16bc5cc572669bbe9d55607c0f96 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,6 +14,7 @@ endif
 
 SRCS :=        Servomatic.ino          \
                PressureSensor.ino      \
+               Valve.ino                       \
 
 
 NAME := Servomatic
index 466b06aae47c00abd252ec0f5b2c30817d549bf7..f69b09251919878a259a69ecc732526932e49e13 100644 (file)
@@ -1,15 +1,26 @@
 #include "PressureSensor.h"
+#include "Valve.h"
 
 int    main()
 {
-       PressureSensor  test{A5};
-
        init();
        Serial.begin(9600);
+
+       main_loop();
+       return (0);
+}
+
+void   main_loop()
+{
+       PressureSensor  test{A5, 10};
+       Valve                   test_valve{6, 760, 2400};
+       int                             usec;
+
        while (1)
        {
-               Serial.println(test.get_pressure());
-               delay(500);
+               delay(5000);
+               test_valve.open(10);
+               delay(5000);
+               test_valve.close(100);
        }
-       return (0);
 }
diff --git a/Valve.h b/Valve.h
new file mode 100644 (file)
index 0000000..ab4a915
--- /dev/null
+++ b/Valve.h
@@ -0,0 +1,31 @@
+#ifndef VALVE_H
+# define VALVE_H
+
+# include <Servo.h>
+
+class Valve
+{
+       private:
+               typedef int pin;
+
+               const Servo     m_servo;
+               const int       m_usec_opened;
+               const int       m_usec_closed;
+               int                     m_usec_current;
+
+               void    write_usec_timed(int usec_wanted, float seconds);
+               void    update_pos();
+
+       public:
+               Valve() = delete;
+               Valve(const Valve &other) = delete;
+               Valve(pin pwm_pin, int usec_opened, int isec_closed);
+               ~Valve();
+
+               Valve   &operator=(const Valve &other) = delete;
+
+               void    open(float seconds = 5);
+               void    close(float seconds = 5);
+};
+
+#endif // VALVE_H
diff --git a/Valve.ino b/Valve.ino
new file mode 100644 (file)
index 0000000..19542c1
--- /dev/null
+++ b/Valve.ino
@@ -0,0 +1,64 @@
+#include "Valve.h"
+
+Valve::Valve(pin pwm_pin, int usec_opened, int usec_closed)
+       : m_servo{}
+       , m_usec_opened{usec_opened}
+       , m_usec_closed{usec_closed}
+       , m_usec_current{usec_closed}
+{
+       m_servo.attach(pwm_pin);
+       update_pos();
+}
+
+Valve::~Valve()
+{
+}
+
+void   Valve::write_usec_timed(int usec_wanted, float seconds)
+{
+       float   msec_delay;
+       int             step;
+
+       if (m_usec_current < usec_wanted)
+               step = 1;
+       else if (m_usec_current > usec_wanted)
+               step = -1;
+       else
+               return ;
+       msec_delay = step * seconds * (1.e3 / (usec_wanted - m_usec_current));
+       if (seconds <= 0 || msec_delay < 0.5)
+       {
+               m_usec_current = usec_wanted;
+               update_pos();
+               return ;
+       }
+       while (msec_delay < 10)
+       {
+               step *= 2;
+               msec_delay *= 2;
+       }
+       while ((step > 0 && usec_wanted > m_usec_current) || (step < 0 &&
+               usec_wanted < m_usec_current))
+       {
+               m_usec_current += step;
+               update_pos();
+               delay(static_cast<unsigned long>(msec_delay));
+       }
+}
+
+void   Valve::open(float seconds)
+{
+       write_usec_timed(m_usec_opened, seconds);
+       //m_servo.writeMicroseconds(760);
+}
+
+void   Valve::close(float seconds)
+{
+       write_usec_timed(m_usec_closed, seconds);
+       //m_servo.writeMicroseconds(2400);
+}
+
+void   Valve::update_pos()
+{
+       m_servo.writeMicroseconds(m_usec_current);
+}