[Kinetis] 【FRDM_KL02Z】开发笔记+基于mbed开发环境+驱动舵机

[复制链接]
1686|7
 楼主| ccw1986 发表于 2015-12-19 20:21 | 显示全部楼层 |阅读模式
1、开发环境
开发环境使用的是mbed离线开发包,Smeshstudio。这个是一个基于java开发环境eclipse的编译器也就是Mbed的离线版,同时SMeshStudio还支持arduino,contiki的开发,是一个多平台的编译器smeshlink官网地址http://mbed.smeshlink.com/。
2、支持的开发板
Mbed官网给出的支持的开发板并没有KL02z,但支持FRDM-KL05z,我对比了两块芯片,不同之处在于KL02z比KL05z少了DAC接口和Touch System Interface,其他引脚定义基本一样,因此本次就基于FRDM—kl05z建立了工程。
 楼主| ccw1986 发表于 2015-12-19 20:22 | 显示全部楼层




3、PWM输出控制伺服舵机
今天用到的是Kl02Z,有四个PWM输出的通道,分别是TPM0_CH0、TPM0_CH1、TPM1_CH0、TPM1_CH1,对应的引脚分别是PTA6、PTA5、PTA0、PTB13。例程给出的是使用TPM0_CH0输出PWM驱动舵机在全角度扫描转动。
 楼主| ccw1986 发表于 2015-12-19 20:23 | 显示全部楼层



                                         

4、用到的舵机驱动专用库函数SERVO.h,这是smeshlink自带的,直接添加就好了。代码贴出来:
 楼主| ccw1986 发表于 2015-12-19 20:24 | 显示全部楼层
  1. /* mbed R/C Servo Library
  2. *  
  3. * Copyright (c) 2007-2010 sford, cstyles
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */

  23. #include "Servo.h"
  24. #include "mbed.h"

  25. static float clamp(float value, float min, float max) {
  26.     if(value < min) {
  27.         return min;
  28.     } else if(value > max) {
  29.         return max;
  30.     } else {
  31.         return value;
  32.     }
  33. }

 楼主| ccw1986 发表于 2015-12-19 20:27 | 显示全部楼层
  1. Servo::Servo(PinName pin) : _pwm(pin) {
  2.     calibrate();
  3.     write(0.5);
  4. }

  5. void Servo::write(float percent) {
  6.     float offset = _range * 2.0 * (percent - 0.5);
  7.     _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
  8.     _p = clamp(percent, 0.0, 1.0);
  9. }

  10. void Servo::position(float degrees) {
  11.     float offset = _range * (degrees / _degrees);
  12.     _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
  13. }
 楼主| ccw1986 发表于 2015-12-19 20:28 | 显示全部楼层
  1. void Servo::calibrate(float range, float degrees) {
  2.     _range = range;
  3.     _degrees = degrees;
  4. }

  5. float Servo::read() {
  6.     return _p;
  7. }

  8. Servo& Servo::operator= (float percent) {
  9.     write(percent);
  10.     return *this;
  11. }

  12. Servo& Servo::operator= (Servo& rhs) {
  13.     write(rhs.read());
  14.     return *this;
  15. }
 楼主| ccw1986 发表于 2015-12-19 20:31 | 显示全部楼层
  1. Servo::operator float() {
  2.     return read();
  3. }

6、最终代码如下:
  1. // Do not remove the include below
  2. #include "kl02z_servo.h"

  3. // Continuously sweep the servo through it's full range
  4. #include "mbed.h"
  5. #include "Servo.h"

  6. Servo myservo(PTA6);
 楼主| ccw1986 发表于 2015-12-19 20:32 | 显示全部楼层
  1. int main()
  2.   {
  3.         myservo.calibrate(0.002,180);
  4.       while(1)
  5.       {
  6.           for(int i=-90; i<90; i++)
  7.           {
  8.               myservo.position(i);
  9.               wait(0.01);
  10.           }
  11.           wait(2);

  12.           for(int i=-90; i>0; i--)
  13.           {
  14.               myservo.position(i);
  15.               wait(0.01);
  16.           }
  17.           wait(2);
  18.       }
  19.   }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

925

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部