rpcam

iPhoneの実行画面

iPhone側のプログラムです。やっていることは、
・web viewにRaspberry PiのIPアドレスを設定してカメラのストリーミング画像を表示
・CMMotionManagerでiPhoneの姿勢(Pitch/Yaw)を取得してカメラ(サーボ)角度を計算しRasPiにサーボの制御を依頼
・UIButtonのタッチを検知してRasPiにGPIOの制御を依頼
となります。

RasPiへの制御依頼には、stringByEvaluatingJavaScriptFromStringを使います。これでRasPi側のJavascript内の関数が実行されます。

//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
- (IBAction)showWebSite:(UITextField *)sender;

- (IBAction)fwdStart:(id)sender;
- (IBAction)fwdEnd:(id)sender;
- (IBAction)bwdStart:(id)sender;
- (IBAction)bwdEnd:(id)sender;
- (IBAction)rightStart:(id)sender;
- (IBAction)rightEnd:(id)sender;
- (IBAction)leftStart:(id)sender;
- (IBAction)leftEnd:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSURL *URL = [NSURL URLWithString:@"http://192.168.13.122:8000/bb/08/"];
    NSURLRequest *URLreq = [NSURLRequest requestWithURL:URL];
    self.webView.scalesPageToFit = YES;
    [self.webView loadRequest: URLreq];
    
    _motionManager = [[CMMotionManager alloc] init];
    
    if (_motionManager.deviceMotionAvailable) {
        _motionManager.deviceMotionUpdateInterval = 1.0f / 4.0f;
        
        CMDeviceMotionHandler   deviceMotionHandler;
        deviceMotionHandler = ^ (CMDeviceMotion* motion, NSError* error) {
            int arg1 = (motion.attitude.roll + 1.57) * 10 + 5;
            int arg2 = (motion.attitude.yaw) * 10 + 5;
            [_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"contServo(13,%d)",arg2]];
            [_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"contServo(18,%d)",arg1]];
        };
        
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler:deviceMotionHandler];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showWebSite:(UITextField *)sender {
    
    NSURL *url = [NSURL URLWithString:sender.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (IBAction)fwdStart:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(27,1)"];
}

- (IBAction)fwdEnd:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(27,0)"];
}

- (IBAction)bwdStart:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(22,1)"];
}

- (IBAction)bwdEnd:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(22,0)"];
}

- (IBAction)rightStart:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(23,1)"];
}

- (IBAction)rightEnd:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(23,0)"];
}

- (IBAction)leftStart:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(24,1)"];
}

- (IBAction)leftEnd:(id)sender {
    [_webView stringByEvaluatingJavaScriptFromString:@"contDrive(24,0)"];
}

@end