TOPに戻る

コンテックRAS/RTC Raspberry Pi ボードを使う⑤A-D変換ボードのAPIを使う

 前、アナログ入力ボードCPI-AI-1208LIのセットアップを行いました。入力端子は10本あり、シングルエンドと差動入力で、接続が異なります。

 データ入力端子(シングルエンド入力時)の配列です。

1 2 3 4 5 6 7 8 9 10
アナログGND AI7 AI6 AI5 AI4 アナログGND AI3 AI2 AI1 AI0

  実験では、入力端子AI0はアナログGNDへ、入力端子AI1は2.5Vの電圧を加えます。

 

  差動入力時のピン配置です。

1 2 3 4 5 6 7 8 9 10
アナログGND AI3- AI3+ AI2- AI2+ アナログGND AI1- AI1+ AI0- AI0+

プログラム

 samplesフォルダに入っていたsingleai.pyをもとに、aitest01.pyの名称でプログラムを作ります。

 現在、~/Downloads/caio490F/contec/caio/configディレクトリで作業をしています。

 入力モードは次のようになっています。

 シングルエンドで0~10Vを設定したいので、AiRangeは「50」です。

設定値 電圧/電流範囲 設定値 電圧/電流範囲
0 ±10V 50 0~10V
1 ±5V 51 0~5V
2 ±2.5V 52 0~4.095V
3 ±1.25V 53 0~2.5V
4 ±1V 54 0~1.25V
5 ±0.625V 55 0~1V
6 ±0.5V 56 0~0.5V
7 ±0.3125V 57 0~0.25V
8 ±0.25V 58 0~0.1V
9 ±0.125V 59 0~0.05V
10 ±0.1V 60 0~0.025V
11 ±0.05V 61 0~0.0125V
12 ±0.025V 62 0~0.01V
13 ±0.0125V 100 0~20mA
14 ±0.01V 101 4~20mA
102 ±20mA 150 1~5V

 最初AiChannel = 0で測定し、次にAiChannel = 1で測定します。

 実行前に、sudo ./contec_aio_start.shを一度動かします。同じフォルダに、caio.pyをコピーしておきます。


import ctypes
import sys
import caio

#================================================================
# Function that checks if a string can be converted to a number
#================================================================
def isnum(str, base):
    try:
        if 10 == base:
            int(str, 10)
        else:
            float(str)
    except:
        return False
    return True

# main

#----------------------------------------
# Declare variables
#----------------------------------------
err_str = ctypes.create_string_buffer(256)      # Error string
lret = ctypes.c_long()                          # Return values of functions
aio_id = ctypes.c_short()                       # ID
device_name = ctypes.create_string_buffer(50)   # Device name
AiData = ctypes.c_float()                       # Converted dat
MaxAiChannels = ctypes.c_short()                # The max number of channels

#----------------------------------------
# Initialize the device
#----------------------------------------
device_name = "AIO000"
AiRange = int(50)  # 0-10V
#----------------------------------------
# Initialization
#----------------------------------------
lret.value = caio.AioInit(device_name.encode(), ctypes.byref(aio_id))
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str)
    print(f"44-AioInit = {lret.value} : {err_str.value.decode('sjis')}")
    sys.exit()
#----------------------------------------
# Reset device
#----------------------------------------
lret.value = caio.AioResetDevice(aio_id)
#print(aio_id)
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str)
    print(f"53-AioResetDevice = {lret.value} : {err_str.value.decode('sjis')}")
    caio.AioExit(aio_id)
    sys.exit()
#----------------------------------------
# Set analog input range
#----------------------------------------
print("\ninput range +/-10V\t\t : 0\t0~10V\t\t : 50")
print("")
#----------------------------------------
# Set the input range
#----------------------------------------
lret.value = caio.AioSetAiRangeAll(aio_id, AiRange)
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str)
    print(f"AioSetAiRangeAll = {lret.value} : {err_str.value.decode('sjis')}")
    caio.AioExit(aio_id)
    sys.exit()
#----------------------------------------
# Set the analog input channel
#----------------------------------------
AiChannel = 0
#----------------------------------------
# Analog input once from the specified channel
#----------------------------------------
lret.value = caio.AioSingleAiEx(aio_id, AiChannel, ctypes.byref(AiData))
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str) 
    print(f"87-AioSingleAiEx = {lret.value} : {err_str.value.decode('sjis')}")
    caio.AioExit(aio_id)
    sys.exit()
print("Channel\t\tVoltage")
print("{0:d}\t\t{1:f}V".format(AiChannel, AiData.value))

AiChannel = 1
#----------------------------------------
# Analog input once from the specified channel
#----------------------------------------
lret.value = caio.AioSingleAiEx(aio_id, AiChannel, ctypes.byref(AiData))
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str) 
    print(f"AioSingleAiEx = {lret.value} : {err_str.value.decode('sjis')}")
    caio.AioExit(aio_id)
    sys.exit()

print("Channel\t\tVoltage")
print("{0:d}\t\t{1:f}V".format(AiChannel, AiData.value))
#----------------------------------------
# Exit the device
#----------------------------------------
lret.value = caio.AioExit(aio_id)
if lret.value != 0:
    caio.AioGetErrorString(lret, err_str)
    print(f"AioExit = {lret.value} : {err_str.value.decode('sjis')}")
    sys.exit()
sys.exit()

 sudo python3 aitest01.py

 実行結果です。チャネル0は0.000Vであってほしいのですが、少し高めの電圧になっています。

連載 コンテックRAS/RTC Raspberry Pi ボードを使う

(1) ドライバのインストール

(2) サンプル・プログラムを動かす

(3) RTCを動かす

(4) A-D変換ボード

(5) A-D変換ボードのAPIを使う