ESP32 Code zur Laser-Steuerung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.0KB

  1. /* Hello World Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_system.h"
  12. #include "esp_spi_flash.h"
  13. #include "driver/ledc.h"
  14. // some servo-specific constants, those also depend on the way the servos are attached to the horns
  15. #define UPDOWN_STRAIGHT 2550
  16. #define UPDOWN_DOWN 1280
  17. #define LEFTRIGHT_LEFT 3200 // 3276
  18. #define LEFTRIGHT_MIDDLE 1920
  19. #define LEFTRIGHT_RIGHT 900
  20. /*
  21. * The position of the laser relative to the projection surface is such that the laser is centered horizontally,
  22. * but sits on the top edge of the surface, so a LEFTRIGHT_MIDDLE and UPDOWN_STRAIGHT point is in the center of
  23. * the top edge
  24. * The values that follow here are the dimensions of the surface and the distance of the laser (same unit)
  25. * such that the corresponding angles for the corners can easily be computed.
  26. */
  27. // distance to the projection surface (all units are cm)
  28. #define SCREEN_DISTANCE 100
  29. #define SCREEN_WIDTH 100
  30. #define SCREEN_HEIGHT 50
  31. #define LEFTRIGHT_LEFT_ANGLE atan2(SCREEN_DISTANCE, SCREEN_WIDTH/2)
  32. #define LEFTRIGHT_RIGHT_ANGLE (-LEFTRIGHT_LEFT_ANGLE)
  33. #define UPDOWN_UP_ANGLE 0
  34. #define UPDOWN_DOWN_ANGLE atan2(SCREEN_DISTANCE, SCREEN_HEIGHT)
  35. void draw_line()
  36. {
  37. //atan2()
  38. }
  39. void app_main()
  40. {
  41. printf("Hello world!\n");
  42. /*
  43. * According to the data sheet, the SG90 9g micro servos
  44. * operate at 0.1s / 60deg
  45. * and require a pwm signal at 50Hz
  46. * with duty length of "about" 1.0 - 1.5 - 2.0 ms
  47. * for all the way left, center, all the way right
  48. */
  49. ledc_timer_config_t timer_conf;
  50. timer_conf.duty_resolution = LEDC_TIMER_15_BIT;
  51. timer_conf.freq_hz = 50;
  52. timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
  53. timer_conf.timer_num = LEDC_TIMER_0;
  54. ledc_timer_config(&timer_conf);
  55. // 2nd servo up/down
  56. // 2550 - straight
  57. // 1280 - down
  58. ledc_channel_config_t ledc_conf;
  59. ledc_conf.channel = LEDC_CHANNEL_0;
  60. ledc_conf.duty = UPDOWN_STRAIGHT; //900; //3276; //1638; // to 3276 // 0 - 2088 (actually a bit less than that)
  61. ledc_conf.gpio_num = 13;
  62. ledc_conf.intr_type = LEDC_INTR_DISABLE;
  63. ledc_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
  64. ledc_conf.timer_sel = LEDC_TIMER_0;
  65. ledc_channel_config(&ledc_conf);
  66. // first servo connected to ground
  67. // left-right
  68. // 3276 - 2000 - 900
  69. ledc_channel_config_t ledc_conf2;
  70. ledc_conf2.channel = LEDC_CHANNEL_1;
  71. ledc_conf2.duty = LEFTRIGHT_LEFT; //900; //3276; //1638; // to 3276 // 0 - 2088 (actually a bit less than that)
  72. ledc_conf2.gpio_num = 15;
  73. ledc_conf2.intr_type = LEDC_INTR_DISABLE;
  74. ledc_conf2.speed_mode = LEDC_HIGH_SPEED_MODE;
  75. ledc_conf2.timer_sel = LEDC_TIMER_0;
  76. ledc_channel_config(&ledc_conf2);
  77. /* Print chip information */
  78. esp_chip_info_t chip_info;
  79. esp_chip_info(&chip_info);
  80. printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
  81. chip_info.cores,
  82. (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
  83. (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
  84. printf("silicon revision %d, ", chip_info.revision);
  85. printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
  86. (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
  87. for (int i = 0; i < 6; i++) {
  88. printf("Restarting in %d seconds...\n", i);
  89. vTaskDelay(1000 / portTICK_PERIOD_MS);
  90. ledc_conf2.duty = LEFTRIGHT_LEFT - (i+1) * (LEFTRIGHT_LEFT - LEFTRIGHT_MIDDLE) / 6;
  91. //ledc_channel_config(&ledc_conf2);
  92. //if(ledc_conf.duty > 1000) {
  93. // ledc_conf.duty = 800;
  94. //} else {
  95. // ledc_conf.duty = 3276;
  96. //}
  97. //ledc_channel_config(&ledc_conf);
  98. }
  99. printf("Restarting now.\n");
  100. fflush(stdout);
  101. esp_restart();
  102. }