Last active
December 23, 2017 07:24
-
-
Save watakemi725/2593f66cbb345d615c351027b5bd1080 to your computer and use it in GitHub Desktop.
マイコン不要・スマホからサーボモータを簡単に動かそう【GlueMotor×Swift】 ref: https://qiita.com/watakemi725/items/dc7dc4b9eef2d839861b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GlueMotorCore.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController,GlueMotorCoreDelegate/*GlueMotorのデリゲートまわり*/ { | |
//UILable,UISliderまわりの宣言 | |
@IBOutlet var servoALabel:UILabel! | |
@IBOutlet var slideMotorA:UISlider! | |
@IBOutlet var servoBLabel:UILabel! | |
@IBOutlet var slideMotorB:UISlider! | |
//GlueMotorまわり | |
var glueMotor:GlueMotorCore! | |
var servoAPulseWidth:TimeInterval! | |
var servoBPulseWidth:TimeInterval! | |
//角度調整用値 | |
var angleAmountA = 0.0000001 | |
var angleAmountB = 0.0000001 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
glueMotor = GlueMotorCore.sharedInstance() as! GlueMotorCore | |
glueMotor.delegate = self | |
//待機状態の数値をsliderに反映させる 初期値は0.0015 | |
slideMotorA.value = Float(0.0015) | |
slideMotorB.value = Float(0.0015) | |
//モータへの周波数にも反映させる | |
servoAPulseWidth = TimeInterval(slideMotorA.value) | |
servoBPulseWidth = TimeInterval(slideMotorB.value) | |
//ラベルにも表示させる | |
servoALabel.text = "A:"+String(format:"%f", servoAPulseWidth * 1000) | |
servoBLabel.text = "B:"+String(format:"%f", servoBPulseWidth * 1000) | |
//周波数をupdate | |
self.updatePulseWidth() | |
} | |
@IBAction func slidercahngedA(_ sender : UISlider){ | |
//Sliderが変更された時に周波数も変更 | |
servoAPulseWidth = TimeInterval(sender.value) | |
self.updateLabels() | |
} | |
@IBAction func slidercahngedB(_ sender : UISlider){ | |
print(sender.value) | |
servoBPulseWidth = TimeInterval(sender.value) | |
self.updateLabels() | |
} | |
//周波数をupdate | |
func updatePulseWidth(){ | |
glueMotor.setPulseWidth(servoAPulseWidth, forServo: 1) | |
glueMotor.setPulseWidth(servoBPulseWidth, forServo: 0) | |
} | |
//labelの表示変更 | |
func updateLabels(){ | |
servoALabel.text = "A:"+String(format:"%f", servoAPulseWidth * 1000) | |
servoBLabel.text = "B:"+String(format:"%f", servoBPulseWidth * 1000) | |
} | |
//glueMotorお約束 | |
func glueMotorCoreWillStartPWMCycle(_ glueMotor:GlueMotorCore){ | |
// NOTE: this delegate method will be called in Audio Queue thread, so don't call the UI related API from here. | |
self.updatePulseWidth() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment