Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame^] | 1 | #!/bin/bash -e |
| 2 | # |
| 3 | # Control step motor rotate of sled |
| 4 | |
| 5 | function set_gpio() |
| 6 | { |
| 7 | NET_NAME=$1 |
| 8 | OUT_VAL=$2 |
| 9 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 10 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 11 | echo "set_gpio: can not find gpio, $NET_NAME" |
| 12 | return 1 |
| 13 | fi |
| 14 | echo -n "set_gpio: set $NET_NAME = $OUT_VAL" |
| 15 | if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then |
| 16 | echo " failed" |
| 17 | return 1 |
| 18 | fi |
| 19 | echo " success" |
| 20 | return 0 |
| 21 | } |
| 22 | |
| 23 | #Get i2c bus number for sledN |
| 24 | function get_bus_num() { |
| 25 | SLED_NUM=$1 |
| 26 | local bus=0 |
| 27 | |
| 28 | if [[ "$SLED_NUM" = [0-5] ]]; then |
| 29 | bus="$SLED_NUM" |
| 30 | fi |
| 31 | echo "$bus" |
| 32 | } |
| 33 | |
| 34 | #Enable sledN Motor VRef |
| 35 | function open_vref() { |
| 36 | i2cset -f -y "${1}" 0x67 0x06 0x95 |
| 37 | } |
| 38 | |
| 39 | #Disable sledN Motor VRef |
| 40 | function close_vref() { |
| 41 | i2cset -f -y "${1}" 0x67 0x06 0x55 |
| 42 | } |
| 43 | |
| 44 | ####################################### |
| 45 | # Setting step motor control pins to start/stop motor |
| 46 | # Arguments: |
| 47 | # 1. SLED NUMBER |
| 48 | # 2. Value of STBY RESET PIN |
| 49 | # 3. Value of ENABLE PIN |
| 50 | # 4. Value of DIRECTION PIN |
| 51 | ####################################### |
| 52 | function set_motor() { |
| 53 | STBY_PIN="SLED${1}_MD_STBY_RESET" |
| 54 | EN_PIN="SLED${1}_MD_IOEXP_EN_FAULT" |
| 55 | DIR_PIN="SLED${1}_MD_DIR" |
| 56 | set_gpio "$STBY_PIN" "$2" |
| 57 | set_gpio "$EN_PIN" "$3" |
| 58 | set_gpio "$DIR_PIN" "$4" |
| 59 | } |
| 60 | |
| 61 | function show_usage(){ |
| 62 | echo "Usage: motor-ctrl [sled0 | sled1 | sled2 | sled3 | sled4 | sled5] [f r s]" |
| 63 | echo " f : Step Motor go forward" |
| 64 | echo " r : Step Motor go reverse" |
| 65 | echo " s : Step Motor stop " |
| 66 | } |
| 67 | |
| 68 | if [ $# -ne 2 ]; then |
| 69 | show_usage |
| 70 | exit 1; |
| 71 | fi |
| 72 | |
| 73 | if [[ "$1" =~ ^(slot[0-5]{1})$ ]] || [[ "$1" =~ ^(sled[0-5]{1})$ ]]; then |
| 74 | SLED=$1 |
| 75 | SLED_NUM=${SLED:4} |
| 76 | I2C_NUM=$(get_bus_num "$SLED_NUM") |
| 77 | ACTION=$2 |
| 78 | else |
| 79 | echo "invalid sled name: $1" |
| 80 | exit 1; |
| 81 | fi |
| 82 | |
| 83 | if [[ "$ACTION" == "s" ]]; then |
| 84 | echo "stop motor" |
| 85 | set_motor "$SLED_NUM" 1 0 0 |
| 86 | close_vref "$I2C_NUM" |
| 87 | elif [[ "$ACTION" == "f" ]];then |
| 88 | echo "start motor, direction:forward" |
| 89 | set_motor "$SLED_NUM" 1 1 0 |
| 90 | open_vref "$I2C_NUM" |
| 91 | elif [[ "$ACTION" == "r" ]];then |
| 92 | echo "start motor, direction:reverse" |
| 93 | set_motor "$SLED_NUM" 1 1 1 |
| 94 | open_vref "$I2C_NUM" |
| 95 | else |
| 96 | echo "Error: Unknown action!" |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | exit 0 |
| 101 | |