ツタンラーメンの忘備録

プログラミングや精神疾患、ラーメンについて書いていきます。たぶん。

フォトリフレクタ+Arduinoで可変抵抗をつないで分解能を上げる話

フォトリフレクタ(RPR220)を使ったときに、分解能を上げるための施策にこまった。
deviceplus.jp

基本的にはこのサイトを参照した。困ったのは512Ωの部分の最適な抵抗値。なるべく大きいrangeで取りたい。
で普通に可変抵抗(max10kΩ)を使ってみると、どうやら20kΩくらいがちょうど良さそうなことがわかった。
ふむふむ

マイコン・カーを製作してみよう(9) - ARM(Cortex-M3,M0)
ちなみに今(手元に在庫がなくて二種類使っているのだけど)LBR-127HLDも同時に使っている。

ちなみに最終的にどういう回路になったかと言うと
f:id:hungrykirby:20170522130958p:plain

適当に組んだので真逆の動きをする。いいんだけどね。変化が取れれば。

herokuコマンドを入力するとエラーが出る問題に対処する

heroku login

をするが、なぜか動かない

heroku-cli: Installing CLI... 18.53MB/18.53MB ! Heroku client internal error.
! Search for help at: https://help.heroku.com
! Or report a bug at: https://github.com/heroku/heroku/issues/new

Permission denied @ rb_sysopen .../heroku/cli/lib/node.exe

みたいなのが出る。
github.com

ここを見るとよくわからんが「消せ」と書いてあるので
/heroku
以下を消す

消せない

node.exeが起動中らしい。

コントロールパネルを見ると、node.jsが勝手に起動しているので止める。


デプロイできた!!!

pythonでherokuの/tmpファイルに一時ファイルを保管する

これに需要がなさそうなのですが、herokuは一時的にファイルを置けるんですよね。どう使い道があるのかわからないのですが…。

from flask import Flask, render_template, request, redirect, url_for
import numpy as np
import os
import glob
from werkzeug import secure_filename
import requests

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)

def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/')
def index():
    title = "ようこそ"
    message = "welcome"
    return render_template('index.html',
                           message=message, title=title)

@app.route('/post', methods=['GET', 'POST'])
def post():
    title = "こんにちは"
    if request.method == 'POST':
        name = request.form['name']
        return render_template('index.html',
                               name=name, title=title)
    else:
        return redirect(url_for('index'))

@app.route('/image-upload', methods=['GET', 'POST'])
def image_post():
    if request.method == 'POST':
        f = request.files['file']
        if f and allowed_file(f.filename):
            filename = secure_filename(f.filename)
            path_2_tmp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
            print(os.path.join(path_2_tmp, filename))
            if not os.path.exists(path_2_tmp):
                os.mkdir(path_2_tmp)
            f.save(os.path.join(path_2_tmp, filename))
            return filename
        return "file req error"
    return "post error"

@app.route('/fetch-image', methods=['GET', 'POST'])
def fetch_image():
    if request.method == 'GET':
        for fn in glob.glob(os.path.join(os.getcwd(),"tmp","*")):
            print(fn)
    return "fetch image"


if __name__ == '__main__':
    app.run()

どっかで見たようなコードですがだいたいどっかのパクリです。
これを適当にhtmlファイルと画像uploadボタンを作ってください。
そのあとURLに/image-uploadや/fetch-imageすれば完了です。まじで誰が使うんだこれ…。

Arduinoからシリアル通信で送られてきた値をリアルタイムにグラフでプロットしていく

Processingでやれよ!!!!

三軸方位センサの値をプロットしています。
冷静に考えて、webとしてFlask+p5.jsとかでやったほうがきれいだったんじゃないか…。

ちなみに何回かやってわかったのですが、これやらないほうがいいです。Ctrl-cでも止まらなくなったりdelayが出たり、そもそも重くなったりするので。

#app.py
import serial
import re
import arrange

def main():
    arra = arrange.Arrange()
    with serial.Serial('COM3',9600,timeout=1) as ser:
        while True:
            c = ser.readline()
            de = c.decode('utf-8')
            m = re.match("\-*[\w]+", str(de))
            if(m != None):
                arra.fetch_three_numbers(m.group())
            else:
                pass
        ser.close()


if __name__ == "__main__":
    main()
#arrage.py
import numpy as np
from scipy import signal
from pylab import *

class Arrange:
    bwn_a = False
    ax = None
    plots_numbers = []
    def __init__(self):
        plot(500, 1) #ここいらない気がする。
        plot(500, 3)
        plot(500, 5)


    def plots(self, x, y, z):
        clf()
        axis([-500, 500, -0.5, 5.5])
        subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.5)
        plot(x, 1, 'o')
        plot(y, 3, '*')
        plot(z, 5, 'd')
        pause(0.01)

    def fetch_three_numbers(self, matched_group):
        if matched_group == "a":
            self.bwn_a = True
            self.plots_numbers = []
        elif self.bwn_a:
            self.plots_numbers.append(matched_group)

        if len(self.plots_numbers) == 3:
            pl = self.plots_numbers
            self.plots(pl[0], pl[1], pl[2])
            self.bwn_a = False
            print(pl)

ちなみにArduinoのコード

#include <Wire.h>
#include "skHMC5883L.h"
#define SENSOR_ADRS     0x1E
skHMC5883L  Compass(SENSOR_ADRS);

void setup() {
  int ans;
  Serial.begin(9600);
  Wire.begin();
  ans = Compass.Begin() ;
}

void loop() {
  // put your main code here, to run repeatedly:
  int ans;
  float deg;
  ans = Compass.SingleRead(&deg,6.6);
  if(ans == 0){
    Serial.println("a");
    Serial.println(RowX);
    Serial.println(RowY);
    Serial.println(RowZ);
  }else Serial.println("NG");
  delay(1000);
}

mySQLで新たに作ったユーザでデータベースが作れなかった。

mysql -u root -p

でrootに入る。この状態で作成したユーザに権限を付与しないとエラーになる(なった)

grant all on *.* to 'user'@'localhost';

これするといい!

flask, python, html, form, inputで画像のアップロード

nekoyukimmm.hatenablog.com

これでだいたいうまくいく。

UPLOAD_FOLDER = '/path/to/the/uploads'

の部分は

UPLOAD_FOLDER = 'path/to/the/uploads'

とする(初心者なのでここでつまずいた)。

また日本語が入るファイル名はアップできない

ArduinoとpythonのSerial通信

基本形。ほんとに単純化した者。
pythonのコードを記載しておく。Arduinoは方位センサを使っている。

pip install pyserial

して

import serial
import re

def main():
    with serial.Serial('COM3',9600,timeout=1) as ser:
        while True:
            c = ser.readline()
            de = c.decode('utf-8') #バイトから文字として認識できる形に。
            m = re.match("\-*\d+", str(de))
            if(m != None):
                print(m.group())
            else:
                pass
                #print(type(m))
        ser.close()

if __name__ == "__main__":
    main()

ちなみに方位センサのコードは

#include <Wire.h>
#include "skHMC5883L.h"
#define SENSOR_ADRS     0x1E
skHMC5883L  Compass(SENSOR_ADRS);

void setup() {
  int ans;
  Serial.begin(9600);
  Wire.begin();
  ans = Compass.Begin() ;
  if (ans == 0) Serial.println("Initialization normal") ;
  else {
    Serial.print("Initialization abnormal ans=") ;
    Serial.println(ans) ;
  }
}

void loop() {
  int ans;
  float deg;
  ans = Compass.SingleRead(&deg,6.6);
  if(ans == 0){
    Serial.println(RowX);
    Serial.println(RowY);
    Serial.println(RowZ);
  }else Serial.println("NG");
  delay(1000);
}

別にサンプルコードのままだから特に何も。