ハロー ブログ

日々のつぶやき @c2c2c2c221

ロビ日記 ロビの音声リモコン

ロビの腰のサーボが故障しました。

 

f:id:c2c2c2c2:20200811233007j:image
f:id:c2c2c2c2:20200811233010j:image

ロビ動作テストをしたところ、腰のサーボが動かないことがわかりました。
臭くも無いので、先日臭かった時に腰のサーボが壊れて...サーボはロビ2の物しか見つからず、2つ手配しました。腰から下の分離ができて、その後、腰のサーボを取り出すことができました。
Servo Testerでテストしました。ID=12はLEDで表示されましたが、ピクリともせず。
腕のサーボと交換してテストするか...発注したサーボ待ちようです。

そこで、ロビの簡易音声リモコンのアプリを作りはじめました。

こっちにおいで、右、左、後ろ、座って、など基本的な認識語だけをボタンで呼び出すだけのアプリです。テストで「すご〜い」ができたので、これから展開とボタンの配置等を考えます。

 

iOSは、インストールするのに、お金がかかるので、Xcodeからインストールしてテストしました。

f:id:c2c2c2c2:20200812200529j:plain

 

i-app-tec.com

こんな感じです。

youtu.be

 

ソースコードは、こんな感じです。

--

ViewController.swift:

 

import UIKit

import AVFoundation

 

class ViewController: UIViewController, AVSpeechSynthesizerDelegate {

    

    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var tapPlayButton:UIButton!

    @IBOutlet weak var tapすごいButton:UIButton!

    

    let spinner = UIActivityIndicatorView(style: .whiteLarge)

    let synthesizer = AVSpeechSynthesizer()

    var audioPlayer: AVAudioPlayer!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //https://www.color-sample.com/colors/637/ RGB value sample

        let 水色1 = CGColor(srgbRed: 157/255, green: 204/255, blue: 224/255, alpha: 1.0)

        let 水色2 = UIColor(displayP3Red: 240/255, green: 255/255, blue: 255/255, alpha: 1.0)

        tapすごいButton.backgroundColor = 水色2

        tapすごいButton.layer.borderWidth = 2.0

        tapすごいButton.layer.borderColor = 水色1

        tapすごいButton.layer.cornerRadius = 10.0 //丸みを数値で変更できます

 

        synthesizer.delegate = self

            try? AVAudioSession.sharedInstance().setCategory(.playback)

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        view.endEditing(true)

    }

    

    @IBAction func tapPlayButton(_ sender: UIButton) {

        guard let text = textField.text else { return }

        synthesizer.stopSpeaking(at: AVSpeechBoundary.immediate)

        let utterance = AVSpeechUtterance(string: text)

        utterance.voice = AVSpeechSynthesisVoice(language: "ja-JP")

        utterance.rate = 0.5

        utterance.pitchMultiplier = 1.5

        utterance.volume = 1.0

        synthesizer.speak(utterance)

    }

    @IBAction func tap歌うたってButton(_ sender: UIButton) {

        playSound(name: "NF177")

    }

    @IBAction func tapこっちへおいでButton(_ sender: UIButton) {

        playSound(name: "NF59")

    }

    @IBAction func tapこっち向いてButton(_ sender: UIButton) {

        playSound(name: "NF64")

    }

    @IBAction func tapスイッチオフButton(_ sender: UIButton) {

        playSound(name: "NF276")

    }

    @IBAction func tapスイッチオンButton(_ sender: UIButton) {

        playSound(name: "NF275")

    }

    @IBAction func tap立ち上がってButton(_ sender: UIButton) {

        playSound(name: "NF73")

    }

    @IBAction func tapはじめましてButton(_ sender: UIButton) {

        playSound(name: "NF24")

    }

    @IBAction func tap左向いてButton(_ sender: UIButton) {

        playSound(name: "NF67")

    }

    @IBAction func tap右向いてButton(_ sender: UIButton) {

        playSound(name: "NF70")

    }

    @IBAction func tapミュージックスタートButton(_ sender: UIButton) {

        playSound(name: "NF86")

    }

    @IBAction func tapすごいButton(_ sender: UIButton) {

        let b = self.tapすごいButton.bounds

        UIView.animate(withDuration: 1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 20,options: , animations: {

                // ボタンサイズの変更CGRect(x: 0, y: 0, width: 100, height: 100))

            self.tapすごいButton.bounds = CGRect(x:b.origin.x - 10,y:b.origin.y,width:b.size.width + 10,height:  b.size.height)

                // ボタンカラーの変更

                self.tapすごいButton.backgroundColor = UIColor(red: 0.85, green: 0.83, blue: 0.45, alpha: 1.0)

                // spinneralpha値を変更して表示

                self.spinner.alpha = 1.0

                // spinnerの位置を設定

            self.spinner.center = CGPoint(x: 40, y: self.tapすごいButton.frame.size.height / 2)

            }, completion: nil)

        

        playSound(name: "G20")

        

        UIView.animate(withDuration: 1.0, delay: 1.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 20,options: , animations: {

                // ボタンサイズを元に戻す

            self.tapすごいButton.bounds = CGRect(x:b.origin.x,y:b.origin.y,width:b.size.width,height:b.size.height)

                // ボタンカラーを元に戻す

            let 水色2 = UIColor(displayP3Red: 240/255, green: 255/255, blue: 255/255, alpha: 1.0)

            self.tapすごいButton.backgroundColor = 水色2

                // spinnerを非表示に

                self.spinner.alpha = 0.0

            }, completion: nil)

 

    }

        

    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {

        print("Speech Start")

    }

    

    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {

        print("Speech End")

    }

 

}

 

extension ViewController: AVAudioPlayerDelegate {

    func playSound(name: String) {

        guard let path = Bundle.main.path(forResource: name, ofType: "wav") else {

            print("音源ファイルが見つかりません")

            return

        }

 

        do {

            // AVAudioPlayerインスタンス

            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))

            // AVAudioPlayerのデリゲートをセット

            audioPlayer.delegate = self

            // 音声の再生

            audioPlayer.play()

        } catch {

        }

    }

}

--

2020.08.21 追記:

 サーボ入荷して、無事に直りました。良かった。☺️