Add servotest subproject
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 5 Nov 2024 09:01:58 +0000 (10:01 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 5 Nov 2024 09:01:58 +0000 (10:01 +0100)
This small project aims to get the precise values for PWM of servos.
A potentiometer is used to control the servo signal through the arduino,
and the arduino reports the microseconds value to the serial output.

servotest/Makefile [new file with mode: 0644]
servotest/servotest.ino [new file with mode: 0644]

diff --git a/servotest/Makefile b/servotest/Makefile
new file mode 100644 (file)
index 0000000..769edfa
--- /dev/null
@@ -0,0 +1,53 @@
+RM := rm -rf
+
+MAINCMD := arduino-cli compile --upload --verify
+
+FQBN := arduino:avr:uno
+PORT := /dev/ttyACM0
+
+MAINCMD += --port $(PORT)
+MAINCMD += --fqbn $(FQBN)
+
+ifneq ("$(wildcard .verbose)","")
+       MAINCMD += --verbose
+endif
+
+SRCS :=        servotest.ino           \
+
+
+NAME := servotest
+
+all : $(NAME)
+
+$(NAME) : .upload
+       arduino-cli monitor --port $(PORT)
+
+.upload : .compile
+       $(MAKE) upload
+       touch .upload
+
+upload : .compile
+       arduino-cli upload --port $(PORT) --verify
+
+.compile : $(SRCS)
+       $(MAKE) compile
+       touch .compile
+
+compile : $(SRCS)
+       arduino-cli compile --fqbn $(FQBN) $<
+
+verbose : .verbose
+
+noverbose :
+       if [ -f .verbose ]; then rm .verbose && $(MAKE) re; fi
+
+re : fclean
+       $(MAKE)
+
+clean :
+
+fclean : clean
+       $(RM) .compile .upload
+
+.% :
+       if [ ! -f $@ ]; then touch $@ && $(MAKE) re; fi
diff --git a/servotest/servotest.ino b/servotest/servotest.ino
new file mode 100644 (file)
index 0000000..786b0a8
--- /dev/null
@@ -0,0 +1,28 @@
+#include <Servo.h>
+
+static const int       ANALOG_CONTROL_PIN = A5;
+static const int       SERVO_PIN = 6;
+static const int       USEC_MIN = 400;
+static const int       USEC_MAX = 2700;
+
+static_assert(USEC_MIN <= USEC_MAX, "USEC_MIN cannot be greater then USEC_MAX!");
+
+int    main()
+{
+       Servo   servo;
+       float   signal;
+       int             usec;
+
+       init();
+       Serial.begin(9600);
+       servo.attach(SERVO_PIN);
+       while (1)
+       {
+               signal = static_cast<float>(analogRead(ANALOG_CONTROL_PIN)) / ((1 << 10) - 1);
+               usec = signal * (USEC_MAX - USEC_MIN) + USEC_MIN;
+               servo.writeMicroseconds(usec);
+               Serial.println(usec);
+               delay(200);
+       }
+       return (0);
+}