|
| 1 | +#include <Wire.h> |
| 2 | +#include <Adafruit_PWMServoDriver.h> |
| 3 | + |
| 4 | +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); |
| 5 | +const uint8_t SERVO_PWM_FREQ {50}; // 50 Hz |
| 6 | + |
| 7 | +void setup() { |
| 8 | + Serial.begin(9600); |
| 9 | + Serial.println("Robot Start!"); |
| 10 | + Serial.println("Please select from the menu: "); |
| 11 | + Serial.println("1. Robot stand"); |
| 12 | + Serial.println("#############################"); |
| 13 | + |
| 14 | + pwm.begin(); |
| 15 | + delay(50); |
| 16 | + pwm.setPWMFreq(SERVO_PWM_FREQ); |
| 17 | + reset_robot(); |
| 18 | +} |
| 19 | + |
| 20 | +void set_servo_pulse(uint8_t n, double pulse) { |
| 21 | + double pulselength; |
| 22 | + |
| 23 | + pulselength = 1000000; // 1,000,000 us per second |
| 24 | + pulselength /= SERVO_PWM_FREQ; |
| 25 | + // Serial.print(pulselength); Serial.println(" us per period"); |
| 26 | + pulselength /= 4096; // 12 bits of resolution |
| 27 | + // Serial.print(pulselength); Serial.println(" us per bit"); |
| 28 | + pulse *= 1000; |
| 29 | + pulse /= pulselength; |
| 30 | + // Serial.println(pulse); |
| 31 | + pwm.setPWM(n, 0, pulse); |
| 32 | +} |
| 33 | + |
| 34 | +void write_servo_ang(uint8_t n,uint8_t angle){ |
| 35 | + double pulse; |
| 36 | + pulse=0.5+angle/90.0; |
| 37 | + set_servo_pulse(n,pulse); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +/*** |
| 42 | + * Params: |
| 43 | + * alpha: Rate of convergence |
| 44 | + * mu: control the amplitude of the output signals, Ah = sqrt(mu) |
| 45 | + * Ah: Amplitude of the hip joint control signal |
| 46 | + * Ak: Amplitude of the knee joint control signal |
| 47 | + * beta: Duty cycle of the support phase (Load Factor) |
| 48 | + * omega_st / omega_sw = (1 - beta) / beta |
| 49 | + * omega_sw: Frequency of the swing phase |
| 50 | + * omega_st: Frequency of the support phase |
| 51 | + * a: rate of the change between omega_sw and omega_st |
| 52 | + * u: (optional, default 0), feedback, EX: u1 <=> x1, u2<=> y2... |
| 53 | + * |
| 54 | + * Outputs: |
| 55 | + * x1 => LF |
| 56 | + * x2 => RF |
| 57 | + * x3 => LH |
| 58 | + * x4 => RH |
| 59 | + * |
| 60 | + * x, the output of the oscillator, is control signal for hip joint |
| 61 | + * the Ascending part is in the swing phase |
| 62 | + * the Descending part is in support phase (when the knee joint is frozen) |
| 63 | +*/ |
| 64 | + |
| 65 | +/* |
| 66 | +const uint16_t SERVOMIN {102}; |
| 67 | +const uint16_t SERVOMAX {512}; |
| 68 | +uint8_t alpha {100}; |
| 69 | +double Ah {0.2}; |
| 70 | +double Ak {0.1}; |
| 71 | +uint8_t a {10}; |
| 72 | +double omega_sw {3 * M_PI}; |
| 73 | +
|
| 74 | +// servo init angle with model coordinate |
| 75 | +double servo_int_ang[12] { |
| 76 | + 45, 100, 180, 135, 90, 0, 135, 90, 0, 45, 90, 180 |
| 77 | +}; |
| 78 | +int8_t servo_dir[12] { |
| 79 | + 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1 |
| 80 | +}; |
| 81 | +
|
| 82 | +// hip0, hip1, knee |
| 83 | +double servo_num[12] { |
| 84 | + 0, 1, 2, |
| 85 | + 3, 4, 5, |
| 86 | + 6, 7, 8, |
| 87 | + 9, 10, 11 |
| 88 | +}; |
| 89 | +// LF, RF, RH, LF |
| 90 | +// walk 0, 0.5, 0.25, 0.75 |
| 91 | +// trot 0, 0.5, 0, 0.5 |
| 92 | +double phi[4] {0, 0.5, 0.25, 0.75}; |
| 93 | +double beta {0.5}; |
| 94 | +
|
| 95 | +// current angle rad buffer |
| 96 | +double current_rad[12] {}; |
| 97 | +*/ |
| 98 | +const uint16_t SERVOMIN {102}; |
| 99 | +const uint16_t SERVOMAX {512}; |
| 100 | + |
| 101 | +// servo init angle with model coordinate |
| 102 | +double servo_int_ang[12] { |
| 103 | + 45, 100, 180, 135, 90, 0, 135, 90, 0, 45, 90, 180 |
| 104 | +}; |
| 105 | + |
| 106 | +int8_t servo_dir[12] { |
| 107 | + 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1 |
| 108 | +}; |
| 109 | + |
| 110 | +// hip0, hip1, knee |
| 111 | +double servo_num[12] { |
| 112 | + 0, 1, 2, |
| 113 | + 3, 4, 5, |
| 114 | + 6, 7, 8, |
| 115 | + 9, 10, 11 |
| 116 | +}; |
| 117 | + |
| 118 | +void reset_robot(){ |
| 119 | + pwm.setPWM(0, 0, 0); |
| 120 | + pwm.setPWM(1, 0, 0); |
| 121 | + pwm.setPWM(2, 0, 0); |
| 122 | + pwm.setPWM(3, 0, 0); |
| 123 | + pwm.setPWM(4, 0, 0); |
| 124 | + pwm.setPWM(5, 0, 0); |
| 125 | + pwm.setPWM(6, 0, 0); |
| 126 | +} |
| 127 | + |
| 128 | +// init STAND state |
| 129 | +void init_robot() { |
| 130 | + Serial.println("init robot"); |
| 131 | + |
| 132 | + write_servo_ang(0, 45); |
| 133 | + write_servo_ang(1, 100); |
| 134 | + write_servo_ang(2, 180); |
| 135 | + |
| 136 | + write_servo_ang(4, 135); |
| 137 | + write_servo_ang(5, 90); |
| 138 | + write_servo_ang(6, 0); |
| 139 | + |
| 140 | +// write_servo_ang(8, 135); |
| 141 | +// write_servo_ang(9, 90); |
| 142 | +// write_servo_ang(10, 0); |
| 143 | +} |
| 144 | + |
| 145 | +String input_str = ""; |
| 146 | +void loop() { |
| 147 | + if (Serial.available() > 0) { |
| 148 | + if (Serial.peek() != '\n') { |
| 149 | + input_str += (char)Serial.read(); |
| 150 | + } else { |
| 151 | + Serial.read(); |
| 152 | + Serial.print("Instruction received: "); |
| 153 | + Serial.println(input_str); |
| 154 | + switch (input_str.toInt()) { |
| 155 | + case 1: |
| 156 | + init_robot(); |
| 157 | + break; |
| 158 | + case 2: |
| 159 | + reset_robot(); |
| 160 | + break; |
| 161 | + } |
| 162 | + input_str = ""; |
| 163 | + } |
| 164 | + delay(100); |
| 165 | + } |
| 166 | +} |
0 commit comments