Example #1
0
        /// <summary>
        /// Calculates end point PWM duration for the specified servo orientation.
        /// </summary>
        /// <returns>
        /// The end point PWM duration.
        /// </returns>
        /// <param name="orientation">
        /// The servo orientation.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// An unsupported orientation was specified.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The end point property value is not between
        /// <see cref="CyrusBuilt.MonoPi.Components.Servos.Servo.END_POINT_MIN"/>
        /// and <see cref="CyrusBuilt.MonoPi.Components.Servos.Servo.END_POINT_MAX"/>.
        /// </exception>
        public float CalcEndPointPwmDuration(ServoOrientation orientation)
        {
            float  result   = 0f;
            String propName = String.Empty;

            switch (orientation)
            {
            case ServoOrientation.Left:
                propName = PROP_END_POINT_LEFT;
                break;

            case ServoOrientation.Right:
                propName = PROP_END_POINT_RIGHT;
                break;

            default:
                throw new InvalidOperationException("Unsupported orientation: " + Enum.GetName(typeof(ServoOrientation), orientation));
            }

            float val     = 0f;
            float epValue = PROP_END_POINT_DEFAULT;

            if (float.TryParse(base.PropertyCollection[propName], out val))
            {
                epValue = val;
                this.ValidateEndPoint(epValue, propName);
            }

            float computedPwmDuration = 0f;
            float p       = ((PWM_MAX - PWM_NEUTRAL) / 150 * epValue);
            float neutral = this.CalcNeutralPwmDuration();

            if (orientation == ServoOrientation.Left)
            {
                computedPwmDuration = (neutral - p);
            }
            else
            {
                computedPwmDuration = (neutral + p);
            }

            if (computedPwmDuration < PWM_MIN)
            {
                result = PWM_MIN;
            }
            else if (computedPwmDuration > PWM_MAX)
            {
                result = PWM_MAX;
            }
            else
            {
                result = computedPwmDuration;
            }
            return(result);
        }
Example #2
0
		/// <summary>
		/// Calculates end point PWM duration for the specified servo orientation.
		/// </summary>
		/// <returns>
		/// The end point PWM duration.
		/// </returns>
		/// <param name="orientation">
		/// The servo orientation.
		/// </param>
		/// <exception cref="InvalidOperationException">
		/// An unsupported orientation was specified.
		/// </exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// The end point property value is not between
		/// <see cref="CyrusBuilt.MonoPi.Components.Servos.Servo.END_POINT_MIN"/>
		/// and <see cref="CyrusBuilt.MonoPi.Components.Servos.Servo.END_POINT_MAX"/>.
		/// </exception>
		public float CalcEndPointPwmDuration(ServoOrientation orientation) {
			float result = 0f;
			String propName = String.Empty;
			switch (orientation) {
				case ServoOrientation.Left:
					propName = PROP_END_POINT_LEFT;
					break;
				case ServoOrientation.Right:
					propName = PROP_END_POINT_RIGHT;
					break;
				default:
					throw new InvalidOperationException("Unsupported orientation: " + Enum.GetName(typeof(ServoOrientation), orientation));
			}

			float val = 0f;
			float epValue = PROP_END_POINT_DEFAULT;
			if (float.TryParse(base.PropertyCollection[propName], out val)) {
				epValue = val;
				this.ValidateEndPoint(epValue, propName);
			}

			float computedPwmDuration = 0f;
			float p = ((PWM_MAX - PWM_NEUTRAL) / 150 * epValue);
			float neutral = this.CalcNeutralPwmDuration();
			if (orientation == ServoOrientation.Left) {
				computedPwmDuration = (neutral - p);
			}
			else {
				computedPwmDuration = (neutral + p);
			}

			if (computedPwmDuration < PWM_MIN) {
				result = PWM_MIN;
			}
			else if (computedPwmDuration > PWM_MAX) {
				result = PWM_MAX;
			}
			else {
				result = computedPwmDuration;
			}
			return result;
		}