kopia lustrzana https://github.com/pimoroni/pimoroni-pico
				
				
				
			Made calibration C++ better match MP
							rodzic
							
								
									7f83c98968
								
							
						
					
					
						commit
						c5be5be759
					
				| 
						 | 
				
			
			@ -123,31 +123,79 @@ namespace servo {
 | 
			
		|||
    return calibration[index];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Calibration::Pair &Calibration::first() {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[0];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Calibration::Pair &Calibration::last() {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[calibration_size - 1];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const Calibration::Pair &Calibration::pair(uint8_t index) const {
 | 
			
		||||
    assert(index < calibration_size);
 | 
			
		||||
    return calibration[index];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::pulse(uint8_t index) const {
 | 
			
		||||
    return pair(index).pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::pulse(uint8_t index, float pulse) {
 | 
			
		||||
    pair(index).pulse = pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::value(uint8_t index) const {
 | 
			
		||||
    return pair(index).value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::value(uint8_t index, float value) {
 | 
			
		||||
    pair(index).value = value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Calibration::Pair &Calibration::first() {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[0];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const Calibration::Pair &Calibration::first() const {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[0];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::first_pulse() const {
 | 
			
		||||
    return first().pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::first_pulse(float pulse) {
 | 
			
		||||
    first().pulse = pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::first_value() const {
 | 
			
		||||
    return first().value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::first_value(float value) {
 | 
			
		||||
    first().value = value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Calibration::Pair &Calibration::last() {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[calibration_size - 1];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const Calibration::Pair &Calibration::last() const {
 | 
			
		||||
    assert(calibration_size > 0);
 | 
			
		||||
    return calibration[calibration_size - 1];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::last_pulse() const {
 | 
			
		||||
    return last().pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::last_pulse(float pulse) {
 | 
			
		||||
    last().pulse = pulse;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float Calibration::last_value() const {
 | 
			
		||||
    return last().value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void Calibration::last_value(float value) {
 | 
			
		||||
    last().value = value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool Calibration::has_lower_limit() const {
 | 
			
		||||
    return limit_lower;
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -74,13 +74,27 @@ namespace servo {
 | 
			
		|||
    void apply_default_pairs(CalibrationType default_type);
 | 
			
		||||
 | 
			
		||||
    uint size() const;
 | 
			
		||||
    Pair &pair(uint8_t index); // Ensure the pairs are assigned in ascending value order
 | 
			
		||||
    Pair &first();
 | 
			
		||||
    Pair &last();
 | 
			
		||||
 | 
			
		||||
    Pair &pair(uint8_t index); // Ensure the pairs are assigned in ascending value order
 | 
			
		||||
    const Pair &pair(uint8_t index) const; // Ensure the pairs are assigned in ascending value order
 | 
			
		||||
    float pulse(uint8_t index) const;
 | 
			
		||||
    void pulse(uint8_t index, float pulse);
 | 
			
		||||
    float value(uint8_t index) const;
 | 
			
		||||
    void value(uint8_t index, float value);
 | 
			
		||||
 | 
			
		||||
    Pair &first();
 | 
			
		||||
    const Pair &first() const;
 | 
			
		||||
    float first_pulse() const;
 | 
			
		||||
    void first_pulse(float pulse);
 | 
			
		||||
    float first_value() const;
 | 
			
		||||
    void first_value(float value);
 | 
			
		||||
 | 
			
		||||
    Pair &last();
 | 
			
		||||
    const Pair &last() const;
 | 
			
		||||
    float last_pulse() const;
 | 
			
		||||
    void last_pulse(float pulse);
 | 
			
		||||
    float last_value() const;
 | 
			
		||||
    void last_value(float value);
 | 
			
		||||
 | 
			
		||||
    bool has_lower_limit() const;
 | 
			
		||||
    bool has_upper_limit() const;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -52,8 +52,8 @@ int main() {
 | 
			
		|||
  constexpr float WIDE_ANGLE_RANGE = 270.0f;
 | 
			
		||||
 | 
			
		||||
  // Lets modify the calibration to increase its range
 | 
			
		||||
  acal.first().value = -WIDE_ANGLE_RANGE / 2;
 | 
			
		||||
  acal.last().value = WIDE_ANGLE_RANGE / 2;
 | 
			
		||||
  acal.first_value(-WIDE_ANGLE_RANGE / 2);
 | 
			
		||||
  acal.last_value(WIDE_ANGLE_RANGE / 2);
 | 
			
		||||
 | 
			
		||||
  // As the calibration was a reference, the servo has already
 | 
			
		||||
  // been updated, but lets access it again to confirm it worked
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +73,7 @@ int main() {
 | 
			
		|||
 | 
			
		||||
  // Update the linear servo so its max value matches the real distance travelled
 | 
			
		||||
  Calibration &lcal = linear_servo.calibration();
 | 
			
		||||
  lcal.last().value = LINEAR_RANGE;
 | 
			
		||||
  lcal.last_value(LINEAR_RANGE);
 | 
			
		||||
 | 
			
		||||
  // As the calibration was a reference, the servo has already
 | 
			
		||||
  // been updated, but lets access it again to confirm it worked
 | 
			
		||||
| 
						 | 
				
			
			@ -93,8 +93,8 @@ int main() {
 | 
			
		|||
 | 
			
		||||
  // Update the continuous rotation servo so its value matches its real speed
 | 
			
		||||
  Calibration &ccal = continuous_servo.calibration();
 | 
			
		||||
  ccal.first().value = -CONTINUOUS_SPEED;
 | 
			
		||||
  ccal.last().value = CONTINUOUS_SPEED;
 | 
			
		||||
  ccal.first_value(-CONTINUOUS_SPEED);
 | 
			
		||||
  ccal.last_value(CONTINUOUS_SPEED);
 | 
			
		||||
 | 
			
		||||
  // As the calibration was a reference, the servo has already
 | 
			
		||||
  // been updated, but lets access it again to confirm it worked
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -338,8 +338,7 @@ mp_obj_t Calibration_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
 | 
			
		|||
        if(index < 0 || index >= calibration_size)
 | 
			
		||||
            mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("index out of range. Expected 0 to %d"), calibration_size - 1);
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->pair((uint)index);
 | 
			
		||||
            return mp_obj_new_float(pair.pulse);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->pulse((uint)index));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -363,8 +362,7 @@ mp_obj_t Calibration_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
 | 
			
		|||
        if(index < 0 || index >= calibration_size)
 | 
			
		||||
            mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("index out of range. Expected 0 to %d"), calibration_size - 1);
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->pair((uint)index);
 | 
			
		||||
            pair.pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
 | 
			
		||||
            self->calibration->pulse((uint)index, mp_obj_get_float(args[ARG_pulse].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -392,8 +390,7 @@ mp_obj_t Calibration_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
 | 
			
		|||
        if(index < 0 || index >= calibration_size)
 | 
			
		||||
            mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("index out of range. Expected 0 to %d"), calibration_size - 1);
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->pair((uint)index);
 | 
			
		||||
            return mp_obj_new_float(pair.value);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->value((uint)index));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -417,8 +414,7 @@ mp_obj_t Calibration_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
 | 
			
		|||
        if(index < 0 || index >= calibration_size)
 | 
			
		||||
            mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("index out of range. Expected 0 to %d"), calibration_size - 1);
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->pair((uint)index);
 | 
			
		||||
            pair.value = mp_obj_get_float(args[ARG_value].u_obj);
 | 
			
		||||
            self->calibration->value((uint)index, mp_obj_get_float(args[ARG_value].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -513,8 +509,7 @@ mp_obj_t Calibration_first_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->first();
 | 
			
		||||
            return mp_obj_new_float(pair.pulse);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->first_pulse());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -533,8 +528,7 @@ mp_obj_t Calibration_first_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->first();
 | 
			
		||||
            pair.pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
 | 
			
		||||
            self->calibration->first_pulse(mp_obj_get_float(args[ARG_pulse].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -557,8 +551,7 @@ mp_obj_t Calibration_first_value(size_t n_args, const mp_obj_t *pos_args, mp_map
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->first();
 | 
			
		||||
            return mp_obj_new_float(pair.value);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->first_value());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -577,8 +570,7 @@ mp_obj_t Calibration_first_value(size_t n_args, const mp_obj_t *pos_args, mp_map
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->first();
 | 
			
		||||
            pair.value = mp_obj_get_float(args[ARG_value].u_obj);
 | 
			
		||||
            self->calibration->first_value(mp_obj_get_float(args[ARG_value].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -673,8 +665,7 @@ mp_obj_t Calibration_last_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->last();
 | 
			
		||||
            return mp_obj_new_float(pair.pulse);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->last_pulse());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -693,8 +684,7 @@ mp_obj_t Calibration_last_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->last();
 | 
			
		||||
            pair.pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
 | 
			
		||||
            self->calibration->last_pulse(mp_obj_get_float(args[ARG_pulse].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -717,8 +707,7 @@ mp_obj_t Calibration_last_value(size_t n_args, const mp_obj_t *pos_args, mp_map_
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            const Calibration::Pair &pair = self->calibration->last();
 | 
			
		||||
            return mp_obj_new_float(pair.value);
 | 
			
		||||
            return mp_obj_new_float(self->calibration->last_value());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -737,8 +726,7 @@ mp_obj_t Calibration_last_value(size_t n_args, const mp_obj_t *pos_args, mp_map_
 | 
			
		|||
        if(self->calibration->size() == 0)
 | 
			
		||||
            mp_raise_ValueError("this calibration does not have any pairs");
 | 
			
		||||
        else {
 | 
			
		||||
            Calibration::Pair &pair = self->calibration->last();
 | 
			
		||||
            pair.value = mp_obj_get_float(args[ARG_value].u_obj);
 | 
			
		||||
            self->calibration->last_value(mp_obj_get_float(args[ARG_value].u_obj));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue