• PWM配置智能小车电机-让小车动起来



    一、硬件电路

    PC6为左前电机的PWM输入,为电机提供动力。
    PG7、PG8控制左前电机的方向。
    在这里插入图片描述
    在这里插入图片描述

    二、MOTOR初始化

    给电机提供周期为1000000ns的PWM波形

    static int MotorInit(struct Motor *ptdev){
    	if(NULL == ptdev){
    		LogDebug("MotorStart EINVAL\r\n");
    		return -EINVAL;
    	}
    
        Pwm_s MotorPwm;
    	switch(ptdev->channel){
            case 1:
                MotorPwm = LFMotorPwm;
                break;  
            case 2:
                MotorPwm = RFMotorPwm;
                break;
            case 3:
                MotorPwm = LBMotorPwm;
                break;
            case 4:
                MotorPwm = RBMotorPwm;
                break; 
            default:
                return -EINVAL;
        }
        
    	int ret = MotorPwm.Config(&MotorPwm,SET_PERIOD_VALUE,1000000);
    	if(ret != ESUCCESS){
    		LogDebug("MotorPwm.Config failed\r\n");
    		return -EIO;
    	}
    
    	
    	ret = MotorPwm.Start(&MotorPwm);
    
    	if(ret != ESUCCESS){
    		LogDebug("MotorPwm.Start failed\r\n");
    		return -EIO;
    	}
    	
    	ret = MotorStop(ptdev);
    	if(ret != ESUCCESS){
    		LogDebug("MotorStop failed\r\n");
    		return -EIO;
    	}
    	
    	ret = MotorSetDir(ptdev,Motor_CR);
    	if(ret != ESUCCESS){
    		LogDebug("MotorSetDir failed\r\n");
    		return -EIO;
    	}
    	
    	return ESUCCESS;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    三、MOTOR的开始与停止

    通过方向控制来开始和停止电机

    static int MotorStart(struct Motor *ptdev){
    	if(NULL == ptdev){
    		LogDebug("MotorStart EINVAL\r\n");
    		return -EINVAL;
    	}
        
    	Gpio_s MotorCR,MotorCCR;
        switch(ptdev->channel){
            case 1:
                MotorCR = LFMotorCR;
                MotorCCR = LFMotorCCR;
                break;  
            case 2:
                MotorCR = RFMotorCR;
                MotorCCR = RFMotorCCR;
                break;
            case 3:
    			MotorCR = LBMotorCR;
                MotorCCR = LBMotorCCR;
                break;
            case 4:
                MotorCR = RBMotorCR;
                MotorCCR = RBMotorCCR;
                break; 
        }
    
    	MotorCR.Write(&MotorCR,ptdev->dir);
    	MotorCCR.Write(&MotorCCR,!ptdev->dir);
    	ptdev->status = Motor_Start;
    	return ESUCCESS;
    }
    
    static int MotorStop(struct Motor *ptdev){
    	if(NULL == ptdev){
    		LogDebug("MotorStart EINVAL\r\n");
    		return -EINVAL;
    	}
    	
    	Gpio_s MotorCR,MotorCCR;
        switch(ptdev->channel){
            case 1:
                MotorCR = LFMotorCR;
                MotorCCR = LFMotorCCR;
                break;  
            case 2:
                MotorCR = RFMotorCR;
                MotorCCR = RFMotorCCR;
                break;
            case 3:
    			MotorCR = LBMotorCR;
                MotorCCR = LBMotorCCR;
                break;
            case 4:
                MotorCR = RBMotorCR;
                MotorCCR = RBMotorCCR;
                break; 
        }
    
    	MotorCR.Write(&MotorCR,ptdev->dir);
    	MotorCCR.Write(&MotorCCR,ptdev->dir);
    	
    	ptdev->status = Motor_Stop;
    	return ESUCCESS;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    四、MOTOR设置速度

    static int MotorSetSpeed(struct Motor *ptdev,uint8_t speed){
    	if(NULL == ptdev){
    		LogDebug("MotorStart EINVAL\r\n");
    		return -EINVAL;
    	}
    	if(0 == speed){
    		return MotorStop(ptdev);
    		}
    
    	int pulse = speed * 1000000 / 100;
    	
    	Pwm_s MotorPwm;
        switch(ptdev->channel){
            case 1:
                MotorPwm = LFMotorPwm;
                break;  
            case 2:
                MotorPwm = RFMotorPwm;
                break;
            case 3:
                MotorPwm = LBMotorPwm;
                break;
            case 4:
                MotorPwm = RBMotorPwm;
                break; 
            default:
                return -EINVAL;
        }
    	
    	int ret = MotorPwm.Config(&MotorPwm,SET_COMPARE_VALUE,pulse);
    	if(ret != ESUCCESS){
    		LogDebug("MotorPwm.Config failed\r\n");
    		return -EIO;
    	}
    
    	if(ptdev->status != Motor_Start){
    		return MotorStart(ptdev);
    		}
    	return ESUCCESS;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    每日一练 | 华为认证真题练习Day118
    hive 中少量数据验证函数的方法-stack
    囚徒逃生问题优化策略
    宇宙最強的IDE - Visual Studio 25岁生日快乐
    基于Python的“书怡”在线书店系统的设计与实现毕业设计源码082332
    单例模式之懒汉模式和饿汉模式
    【LeetCode-简单题】501. 二叉搜索树中的众数
    让您了解GPS北斗卫星时间同步系统(NTP时钟同步)重要性
    计算机毕业设计SSM电影院购票系统【附源码数据库】
    【元宇宙欧米说】对话NFT平台丨Maze NFT 协议
  • 原文地址:https://blog.csdn.net/qq_45456950/article/details/136636643