From: Lukáš Jiřiště Date: Tue, 5 Nov 2024 09:01:58 +0000 (+0100) Subject: Add servotest subproject X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=e306936d772096f84e67f90f799cdf1ee4ed8725;p=Servomatic.git Add servotest subproject 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. --- diff --git a/servotest/Makefile b/servotest/Makefile new file mode 100644 index 0000000..769edfa --- /dev/null +++ b/servotest/Makefile @@ -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 index 0000000..786b0a8 --- /dev/null +++ b/servotest/servotest.ino @@ -0,0 +1,28 @@ +#include + +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(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); +}