| 1 | /****************************************************************************** |
|---|
| 2 | * The MIT License |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2010, LeafLabs, LLC. |
|---|
| 5 | * |
|---|
| 6 | * Permission is hereby granted, free of charge, to any person |
|---|
| 7 | * obtaining a copy of this software and associated documentation |
|---|
| 8 | * files (the "Software"), to deal in the Software without |
|---|
| 9 | * restriction, including without limitation the rights to use, copy, |
|---|
| 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
|---|
| 11 | * of the Software, and to permit persons to whom the Software is |
|---|
| 12 | * furnished to do so, subject to the following conditions: |
|---|
| 13 | * |
|---|
| 14 | * The above copyright notice and this permission notice shall be |
|---|
| 15 | * included in all copies or substantial portions of the Software. |
|---|
| 16 | * |
|---|
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
|---|
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
|---|
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|---|
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 24 | * SOFTWARE. |
|---|
| 25 | *****************************************************************************/ |
|---|
| 26 | |
|---|
| 27 | #if defined(ARDUINO_ARCH_STM32F4) |
|---|
| 28 | |
|---|
| 29 | #include "ServoTimers.h" |
|---|
| 30 | |
|---|
| 31 | #include "boards.h" |
|---|
| 32 | #include "io.h" |
|---|
| 33 | #include "pwm.h" |
|---|
| 34 | #include "math.h" |
|---|
| 35 | |
|---|
| 36 | // 20 millisecond period config. For a 1-based prescaler, |
|---|
| 37 | // |
|---|
| 38 | // (prescaler * overflow / CYC_MSEC) msec = 1 timer cycle = 20 msec |
|---|
| 39 | // => prescaler * overflow = 20 * CYC_MSEC |
|---|
| 40 | // |
|---|
| 41 | // This picks the smallest prescaler that allows an overflow < 2^16. |
|---|
| 42 | #define MAX_OVERFLOW ((1 << 16) - 1) |
|---|
| 43 | #define CYC_MSEC (1000 * CYCLES_PER_MICROSECOND) |
|---|
| 44 | #define TAU_MSEC 20 |
|---|
| 45 | #define TAU_USEC (TAU_MSEC * 1000) |
|---|
| 46 | #define TAU_CYC (TAU_MSEC * CYC_MSEC) |
|---|
| 47 | #define SERVO_PRESCALER (TAU_CYC / MAX_OVERFLOW + 1) |
|---|
| 48 | #define SERVO_OVERFLOW ((uint16)round((double)TAU_CYC / SERVO_PRESCALER)) |
|---|
| 49 | |
|---|
| 50 | // Unit conversions |
|---|
| 51 | #define US_TO_COMPARE(us) ((uint16)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW)) |
|---|
| 52 | #define COMPARE_TO_US(c) ((uint32)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC)) |
|---|
| 53 | #define ANGLE_TO_US(a) ((uint16)(map((a), this->minAngle, this->maxAngle, \ |
|---|
| 54 | this->minPW, this->maxPW))) |
|---|
| 55 | #define US_TO_ANGLE(us) ((int16)(map((us), this->minPW, this->maxPW, \ |
|---|
| 56 | this->minAngle, this->maxAngle))) |
|---|
| 57 | |
|---|
| 58 | Servo::Servo() { |
|---|
| 59 | this->resetFields(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | bool Servo::attach(uint8 pin, uint16 minPW, uint16 maxPW, int16 minAngle, int16 maxAngle) |
|---|
| 63 | { |
|---|
| 64 | // SerialUSB.begin(115200); |
|---|
| 65 | // SerialUSB.println(MAX_OVERFLOW); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | timer_dev *tdev = PIN_MAP[pin].timer_device; |
|---|
| 69 | |
|---|
| 70 | analogWriteResolution(16); |
|---|
| 71 | |
|---|
| 72 | int prescaler = 6; |
|---|
| 73 | int overflow = 65400; |
|---|
| 74 | int minPW_correction = 300; |
|---|
| 75 | int maxPW_correction = 300; |
|---|
| 76 | |
|---|
| 77 | pinMode(pin, OUTPUT); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | if (tdev == NULL) { |
|---|
| 81 | // don't reset any fields or ASSERT(0), to keep driving any |
|---|
| 82 | // previously attach()ed servo. |
|---|
| 83 | return false; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if ( (tdev == TIMER1) || (tdev == TIMER8) || (tdev == TIMER10) || (tdev == TIMER11)) |
|---|
| 87 | { |
|---|
| 88 | prescaler = 54; |
|---|
| 89 | overflow = 65400; |
|---|
| 90 | minPW_correction = 40; |
|---|
| 91 | maxPW_correction = 50; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if ( (tdev == TIMER2) || (tdev == TIMER3) || (tdev == TIMER4) || (tdev == TIMER5) ) |
|---|
| 95 | { |
|---|
| 96 | prescaler = 6; |
|---|
| 97 | overflow = 64285; |
|---|
| 98 | minPW_correction = 370; |
|---|
| 99 | maxPW_correction = 350; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if ( (tdev == TIMER6) || (tdev == TIMER7) ) |
|---|
| 103 | { |
|---|
| 104 | prescaler = 6; |
|---|
| 105 | overflow = 65400; |
|---|
| 106 | minPW_correction = 0; |
|---|
| 107 | maxPW_correction = 0; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | if ( (tdev == TIMER9) || (tdev == TIMER12) || (tdev == TIMER13) || (tdev == TIMER14) ) |
|---|
| 111 | { |
|---|
| 112 | prescaler = 6; |
|---|
| 113 | overflow = 65400; |
|---|
| 114 | minPW_correction = 30; |
|---|
| 115 | maxPW_correction = 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (this->attached()) { |
|---|
| 119 | this->detach(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | this->pin = pin; |
|---|
| 123 | this->minPW = (minPW + minPW_correction); |
|---|
| 124 | this->maxPW = (maxPW + maxPW_correction); |
|---|
| 125 | this->minAngle = minAngle; |
|---|
| 126 | this->maxAngle = maxAngle; |
|---|
| 127 | |
|---|
| 128 | timer_pause(tdev); |
|---|
| 129 | timer_set_prescaler(tdev, prescaler); // prescaler is 1-based |
|---|
| 130 | timer_set_reload(tdev, overflow); |
|---|
| 131 | timer_generate_update(tdev); |
|---|
| 132 | timer_resume(tdev); |
|---|
| 133 | |
|---|
| 134 | return true; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | bool Servo::detach() { |
|---|
| 138 | if (!this->attached()) { |
|---|
| 139 | return false; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | timer_dev *tdev = PIN_MAP[this->pin].timer_device; |
|---|
| 143 | uint8 tchan = PIN_MAP[this->pin].timer_channel; |
|---|
| 144 | timer_set_mode(tdev, tchan, TIMER_DISABLED); |
|---|
| 145 | |
|---|
| 146 | this->resetFields(); |
|---|
| 147 | |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void Servo::write(int degrees) { |
|---|
| 152 | degrees = constrain(degrees, this->minAngle, this->maxAngle); |
|---|
| 153 | this->writeMicroseconds(ANGLE_TO_US(degrees)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | int Servo::read() const { |
|---|
| 157 | int a = US_TO_ANGLE(this->readMicroseconds()); |
|---|
| 158 | // map() round-trips in a weird way we mostly correct for here; |
|---|
| 159 | // the round-trip is still sometimes off-by-one for write(1) and |
|---|
| 160 | // write(179). |
|---|
| 161 | return a == this->minAngle || a == this->maxAngle ? a : a + 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void Servo::writeMicroseconds(uint16 pulseWidth) { |
|---|
| 165 | if (!this->attached()) { |
|---|
| 166 | ASSERT(0); |
|---|
| 167 | return; |
|---|
| 168 | } |
|---|
| 169 | pulseWidth = constrain(pulseWidth, this->minPW, this->maxPW); |
|---|
| 170 | analogWrite(this->pin, US_TO_COMPARE(pulseWidth)); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | uint16 Servo::readMicroseconds() const { |
|---|
| 174 | if (!this->attached()) { |
|---|
| 175 | ASSERT(0); |
|---|
| 176 | return 0; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | stm32_pin_info pin_info = PIN_MAP[this->pin]; |
|---|
| 180 | uint16 compare = timer_get_compare(pin_info.timer_device, |
|---|
| 181 | pin_info.timer_channel); |
|---|
| 182 | |
|---|
| 183 | return COMPARE_TO_US(compare); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | void Servo::resetFields(void) { |
|---|
| 187 | this->pin = NOT_ATTACHED; |
|---|
| 188 | this->minAngle = MIN_ANGLE; |
|---|
| 189 | this->maxAngle = MAX_ANGLE; |
|---|
| 190 | this->minPW = MIN_PULSE_WIDTH; |
|---|
| 191 | this->maxPW = MAX_PULSE_WIDTH; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | #endif |
|---|