view main/robots/odr-motion/motorctl.cpp @ 247:caf5ac88d09c main

bus speed is now 500k rather than 250k new motor wiring reversed the previously-reversed rear motor connections
author Bob Cook <bob@bobcookdev.com>
date Sat, 22 Aug 2015 13:48:35 -0700
parents 240d07f41efe
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  Copyright (C) 2012-2013 Bob Cook
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Pololu Qik2s12v10 functions for the Outdoor Robot motion control node.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#include <ctype.h>
#include <string.h>

#include "project_defs.h"
#include "func.h"

#include "packages/avr/device/spinwait.h"
#include "packages/avr/device/uart.h"

#include "packages/common/pololu/qik2s12v10.h"

// ----------------------------------------------------------------------------------------

static volatile bool   s_motor_error;
static volatile bool   s_motor_suspended;

static volatile int8_t s_motor_speed_front;
static volatile int8_t s_motor_speed_rear;

// ----------------------------------------------------------------------------------------

inline void motorctl_qik_reset_high()
{
    PORTD |= ( 1 << PD6 );
}

inline void motorctl_qik_reset_low()
{
    PORTD &= ~( 1 << PD6 );
}

// ----------------------------------------------------------------------------------------

bool motorctl_init()
{
    //--    Qik motor controller reset on PORTD.6.
    
    DDRD |= ( 1 << PD6 );

    //--    Qik motor controller error on PORTD.5. Turn off the pull-up.

    DDRD &= ~( 1 << PD5 );
    PORTD &= ~( 1 << PD5 );

    //--    Initialize the Pololu Qik 2s12v10 motor controller.

    motorctl_qik_reset_low();
    spinwait_delay_ms( 100 );

    motorctl_qik_reset_high();
    spinwait_delay_ms( 100 );

    if ( pololu_qik2s12v10_init() && ! motorctl_is_in_error() )
    {
        pololu_qik2s12v10_m0_forward( 0 );
        pololu_qik2s12v10_m1_forward( 0 );
        s_motor_error = false;
        return true;
    }

    s_motor_error = true;
    return false;
}

// ----------------------------------------------------------------------------------------

void motorctl_shutdown()
{
    motorctl_qik_reset_low();
}

// ----------------------------------------------------------------------------------------

bool motorctl_check_status()
{
    uint8_t qik_firmware_version;
    if ( ! pololu_qik2s12v10_firmware_version( &qik_firmware_version ) ) 
    {
        s_motor_error = true;
        return false;
    }

    s_motor_error = false;
    return true;
}

// ----------------------------------------------------------------------------------------

bool motorctl_is_in_error()
{
    if ( ( PIND & ( 1 << PD5 ) ) > 0 )
    {
        return true;
    }

    if ( s_motor_error ) return true;

    return false;
}

// ----------------------------------------------------------------------------------------

bool motorctl_is_suspended()
{
    return s_motor_suspended;
}

// ----------------------------------------------------------------------------------------

void motorctl_suspend()
{
    if ( ! s_motor_suspended )
    {
        s_motor_suspended = true;
        pololu_qik2s12v10_m0_forward( 0 );
        pololu_qik2s12v10_m1_forward( 0 );
    }
}

// ----------------------------------------------------------------------------------------

void motorctl_unsuspend()
{
    if ( s_motor_suspended )
    {
        s_motor_suspended = false;

        // update the front motor speed; yes forward is really reverse, see below
        
        if ( s_motor_speed_front < 0 )
        {
            pololu_qik2s12v10_m1_forward( -s_motor_speed_front );
        }
        else
        {
            pololu_qik2s12v10_m1_reverse( s_motor_speed_front );
        }

        // update the rear motor speed; ditto for reverse here too
        
        if ( s_motor_speed_rear < 0 )
        {
            pololu_qik2s12v10_m0_forward( -s_motor_speed_rear );
        }
        else
        {
            pololu_qik2s12v10_m0_reverse( s_motor_speed_rear );
        }
    }
}

// ----------------------------------------------------------------------------------------

static void set_speed_front( int8_t speed )
{
    if ( speed == s_motor_speed_front )
    {
        return;
    }

    s_motor_speed_front = speed;

    if ( s_motor_suspended )
    {
        return;
    }

    // yes, the motor leads are backwards thus forward == reverse
    
    if ( speed < 0 )
    {
        pololu_qik2s12v10_m1_forward( -speed );
    }
    else
    {
        pololu_qik2s12v10_m1_reverse( speed );
    }
}

// ----------------------------------------------------------------------------------------

static void set_speed_rear( int8_t speed )
{
    if ( speed == s_motor_speed_rear )
    {
        return;
    }

    s_motor_speed_rear = speed;

    if ( s_motor_suspended )
    {
        return;
    }

    if ( speed < 0 )
    {
        pololu_qik2s12v10_m0_reverse( -speed );
    }
    else
    {
        pololu_qik2s12v10_m0_forward( speed );
    }
}

// ----------------------------------------------------------------------------------------

void motorctl_set_speed( int8_t front, int8_t rear )
{
    set_speed_front( front );
    set_speed_rear( rear );
}

// ----------------------------------------------------------------------------------------

int8_t motorctl_speed_front()
{
    return s_motor_speed_front;
}

int8_t motorctl_speed_rear()
{
    return s_motor_speed_rear;
}

// ----------------------------------------------------------------------------------------