fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define UINT16_MAX (65535)
  5. #define VREF (3.3)
  6. /* Current gain of the AD8210 plus effect of resistor divider */
  7. #define GALVO_CUR_GAIN (20.0)
  8. #define GALVO_R_SENSE (0.008)
  9.  
  10. #ifndef M_PI
  11. // Stolen from /usr/include/math.h
  12. #define M_PI 3.14159265358979323846f
  13. #endif
  14. #define POSITION_SCALE_FACTOR (1)
  15. #define DEGREES_TO_RADIANS(d) ((d) * (M_PI / 180.0f))
  16. #define RADIANS_TO_DEGREES(r) ((r) * (180.0f / M_PI))
  17.  
  18. /* VREF RANGE === V_AT_8210 === GAIN SENSE_R */
  19. #define ADC2AMP_FACTOR (VREF/(UINT16_MAX + 1) / GALVO_CUR_GAIN / GALVO_R_SENSE)
  20.  
  21. enum stop_type
  22. {
  23. STOP_TYPE_BOTTOM,
  24. STOP_TYPE_TOP,
  25. STOP_TYPE_NUM_STOPS,
  26. };
  27. struct stop_data
  28. {
  29. int32_t stop_position;
  30. float stop_det_cur;
  31. int32_t current_sum;
  32. int32_t voltage_sum;
  33. uint32_t sum_count;
  34. };
  35. struct stop_data stops[STOP_TYPE_NUM_STOPS];
  36. int main(void) {
  37. // your code goes here
  38. stops[STOP_TYPE_BOTTOM].stop_position = -93174;
  39. stops[STOP_TYPE_TOP].stop_position = 93511;
  40. stops[STOP_TYPE_BOTTOM].stop_det_cur = 0.000276;
  41. stops[STOP_TYPE_TOP].stop_det_cur = 0.000279;
  42. stops[STOP_TYPE_BOTTOM].current_sum = -258999;
  43. stops[STOP_TYPE_TOP].current_sum = 22135;
  44. stops[STOP_TYPE_BOTTOM].voltage_sum = -589489;
  45. stops[STOP_TYPE_TOP].voltage_sum = 55361;
  46. stops[STOP_TYPE_BOTTOM].sum_count = 1063;
  47. stops[STOP_TYPE_TOP].sum_count = 100;
  48. float const voltage_top = stops[STOP_TYPE_TOP].voltage_sum * 5.04f / 5000 / stops[STOP_TYPE_TOP].sum_count;
  49. float const voltage_bottom = stops[STOP_TYPE_BOTTOM].voltage_sum * 5.04f / 5000 / stops[STOP_TYPE_BOTTOM].sum_count;
  50. float const current_top = stops[STOP_TYPE_TOP].current_sum * ADC2AMP_FACTOR / stops[STOP_TYPE_TOP].sum_count;
  51. float const current_bottom = stops[STOP_TYPE_BOTTOM].current_sum * ADC2AMP_FACTOR / stops[STOP_TYPE_BOTTOM].sum_count;
  52. float const resistance = (voltage_top - voltage_bottom) / (current_top - current_bottom);
  53. printf("Resistance = %.3f\n", resistance);
  54. float const pwm_offset = voltage_top - (resistance * current_top);
  55. printf("PWM Offset = %.3f\n", pwm_offset);
  56. float const pwm_offset2 = voltage_bottom - (resistance * current_bottom);
  57. printf("PWM Offset 2 = %.3f\n", pwm_offset2);
  58. int32_t const adjusted_bottom_pos = (stops[STOP_TYPE_BOTTOM].stop_det_cur / 0.000250) * stops[STOP_TYPE_BOTTOM].stop_position;
  59. int32_t const adjusted_top_pos = (stops[STOP_TYPE_TOP].stop_det_cur / 0.000250) * stops[STOP_TYPE_TOP].stop_position;
  60. int32_t const pos_delta = adjusted_top_pos - adjusted_bottom_pos;
  61. int32_t const expected_delta = (100000 / 30.f) * 60.0f;
  62. float const scale = (float)pos_delta / (float)expected_delta;
  63. printf("Adjusted top pos = %d\n", adjusted_top_pos);
  64. printf("Adjusted bottom pos = %d\n", adjusted_bottom_pos);
  65. printf("Pos Delta = %d\n", pos_delta);
  66. printf("Expected Delta = %d\n", expected_delta);
  67. printf("Scale = %.3f\n", scale);
  68. float const scale_adjusted = scale * 1.5f;
  69. printf("Scale Adjusted = %.3f\n", scale_adjusted);
  70. float const top_deg = adjusted_top_pos /** (30.f / 100000)*/ * scale_adjusted;
  71. float const deg_offset = (30 * scale_adjusted) - top_deg;
  72. float const bottom_deg = adjusted_bottom_pos /** * (30.f / 100000) */ * scale_adjusted;
  73. float const deg_offset2 = (-30 * scale_adjusted) - bottom_deg;
  74. printf("Top Deg = %.3f\n", top_deg);
  75. printf("Bottom Deg = %.3f\n", bottom_deg);
  76. printf("Deg Offset = %.3f\n", deg_offset);
  77. printf("Deg Offset 2 = %.3f\n", deg_offset2);
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Resistance = 7.633
PWM Offset = 0.026
PWM Offset 2 = 0.026
Adjusted top pos = 104358
Adjusted bottom pos = -102864
Pos Delta = 207222
Expected Delta = 200000
Scale = 1.036
Scale Adjusted = 1.554
Top Deg = 48.657
Bottom Deg = -47.960
Deg Offset = -2.032
Deg Offset 2 = 1.335