|
本帖最后由 strong161 于 2017-6-6 10:19 编辑
点完灯后一直没有没更新,主要是技术不行,BLE一直整不懂,所以暂时先搞下基本车子的功能,先分享一下近期的进展吧。结构比较简单,就是个四驱小车,实际采用的是双驱,只用了一片L298N,结果,压跟带不起4个电机,……抽空再加一片L298N吧。半边跑一顿撞,红外寻迹Sensor只余下两路了,所以代码还是得改改,2路是个尴尬的问题变成妞妞车了。
因为KW41兼容arduino接口,所以就直接把所有电路都直接怼在一个arduino扩展板上了,这样的好处就是,每次不用老是一根一根对线了。在这里对KW41还是要提点建议的,KW41的板子上面设计可不可以把带PWM IO也能arduino兼容一下,这样就更加直接观了,更加方便了,因为某宝上查了一些arduino直插的电机驱动板,那样会很方便,但是与KW41是没法怼的,因为使用是arduino带PWM输出的IO,而对应的IO在KW41上面并没有PWM功能,需要软件来做,这样就比较复杂了。
另外还有一个比较尴尬的事,就是原来设计按SW3进入中断,然后使能启动主程序,结果一直进不了中断,后面检查发现,SW3的IO被占用了,也就是一个IO做了板载SW后,同时又做成了arduino接口,当然这个主要是自己没有仔细看,不过一个IO做了板载SW最好就不要再放到arduino接口上面了。
- gpio_pin_config_t moto_config = {
- kGPIO_DigitalOutput, 0,
- };
- /*moto IO init*/
- #define MOTO_INIT \
- GPIO_PinInit(MOTO_LEFT1_GPIO, MOTO_LEFT1_GPIO_PIN, &moto_config);\
- GPIO_PinInit(MOTO_LEFT2_GPIO, MOTO_LEFT2_GPIO_PIN, &moto_config);\
- GPIO_PinInit(MOTO_RIGHT1_GPIO, MOTO_RIGHT1_GPIO_PIN, &moto_config);\
- GPIO_PinInit(MOTO_RIGHT2_GPIO, MOTO_RIGHT2_GPIO_PIN, &moto_config)
- /*moto dirct*/
- #define MOTO_LEFT_RUN \
- GPIO_WritePinOutput(MOTO_LEFT1_GPIO, MOTO_LEFT1_GPIO_PIN, 1);\
- GPIO_WritePinOutput(MOTO_LEFT2_GPIO, MOTO_LEFT2_GPIO_PIN, 0)
- #define MOTO_RIGHT_BACK \
- GPIO_WritePinOutput(MOTO_LEFT1_GPIO, MOTO_LEFT1_GPIO_PIN, 0);\
- GPIO_WritePinOutput(MOTO_LEFT2_GPIO, MOTO_LEFT2_GPIO_PIN, 1)
- #define MOTO_RIGHT_RUN \
- GPIO_WritePinOutput(MOTO_RIGHT1_GPIO, MOTO_RIGHT1_GPIO_PIN, 1);\
- GPIO_WritePinOutput(MOTO_RIGHT2_GPIO, MOTO_RIGHT2_GPIO_PIN, 0)
- #define MOTO_LEFT_BACK \
- GPIO_WritePinOutput(MOTO_RIGHT1_GPIO, MOTO_RIGHT1_GPIO_PIN, 0);\
- GPIO_WritePinOutput(MOTO_RIGHT2_GPIO, MOTO_RIGHT2_GPIO_PIN, 1)
复制代码
- /*setting for four tracking Sensors */
- #define TRACK_SENSOR_GPIO GPIOB
- #define TRACK_SENSOR_PORT PORTB
- //#define TRACK_SENSOR1_GPIO_PIN 1U
- #define TRACK_SENSOR2_GPIO_PIN 1U
- #define TRACK_SENSOR3_GPIO_PIN 2U
- //#define TRACK_SENSOR4_GPIO_PIN 18U
- #define TRACK_SENSOR_IRQ PORTB_PORTC_IRQn
- #define TRACK_SENSOR_IRQ_HANDLER PORTB_PORTC_IRQHandler
复制代码
以前没有好好玩过小车,所以对L298N使用有点尴尬,仿佛是有坑的,本来是打算通过Enable输入PWM进行调速的,结果根本调不了……后来经饺神指导,说L298N的enable使能调PWM基本上要最低频,那样PWM就没有什么意思了,所以还是用直接输出到IN上面控制电机。
- void TRACK_SENSOR_IRQ_HANDLER(void) {
- /*read IRQ source and clean IRQ */
- // if (GPIO_GetPinsInterruptFlags(TRACK_SENSOR_GPIO)&(1U << TRACK_SENSOR1_GPIO_PIN)) {
- // GPIO_ClearPinsInterruptFlags( TRACK_SENSOR_GPIO,1U << TRACK_SENSOR1_GPIO_PIN);
- // AUTO_MODE = 1;
- // }
- //
- // else
- if (GPIO_GetPinsInterruptFlags(TRACK_SENSOR_GPIO)&(1U << TRACK_SENSOR2_GPIO_PIN)) {
- GPIO_ClearPinsInterruptFlags( TRACK_SENSOR_GPIO,1U << TRACK_SENSOR2_GPIO_PIN );
- AUTO_MODE = 2;
- }
- else if (GPIO_GetPinsInterruptFlags(TRACK_SENSOR_GPIO)&(1U << TRACK_SENSOR3_GPIO_PIN)) {
- GPIO_ClearPinsInterruptFlags( TRACK_SENSOR_GPIO,1U << TRACK_SENSOR3_GPIO_PIN);
- AUTO_MODE = 3;
- }
- else if(GPIO_GetPinsInterruptFlags(BOARD_SW_GPIO)&(1U<< BOARD_SW_GPIO_PIN)){
- /* Clear external interrupt flag. */
- GPIO_ClearPinsInterruptFlags(BOARD_SW_GPIO, 1U << BOARD_SW_GPIO_PIN);
- /* Change state of button. */
- SwForStart = 1;
- }
- else printf("error cannot find the IRQ");
- }
复制代码
主程序
- int main(void) {
- /* Define the init structure for the input switch pin */
- gpio_pin_config_t sw_config = {
- kGPIO_DigitalInput, 0,
- };
- /* Init input switch GPIO. */
- /*MOTO Enable TPM seting */
- /*
- * tpmInfo.prescale = kTPM_Prescale_Divide_1;
- * tpmInfo.useGlobalTimeBase = false;
- * tpmInfo.enableDoze = false;
- * tpmInfo.enableDebugMode = false;
- * tpmInfo.enableReloadOnTrigger = false;
- * tpmInfo.enableStopOnOverflow = false;
- * tpmInfo.enableStartOnTrigger = false;
- * tpmInfo.enablePauseOnTrigger = false;
- * tpmInfo.triggerSelect = kTPM_Trigger_Select_0;
- * tpmInfo.triggerSource = kTPM_TriggerSource_External;
- */
- tpm_config_t tpmInfo;
- tpm_chnl_pwm_signal_param_t tpmParam[2];
- /* Configure tpm params with frequency 24kHZ */
- tpmParam[0].chnlNumber = (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL;
- tpmParam[0].level = kTPM_LowTrue;
- tpmParam[0].dutyCyclePercent = updatedDutycycleLeft;
- tpmParam[1].chnlNumber = (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL;
- tpmParam[1].level = kTPM_LowTrue;
- tpmParam[1].dutyCyclePercent = updatedDutycycleRight;
- /*config for Sensor IO*/
- gpio_pin_config_t Track_Sensor_config = {
- kGPIO_DigitalInput, 0,
- };
- /* Init board hardware. */
- BOARD_InitBootPins();
- BOARD_InitBootClocks();
- /* Init FSL debug console. */
- BOARD_InitDebugConsole();
- /*Init the Moto IO*/
- MOTO_INIT;
- /*for Start SW */
- PORT_SetPinInterruptConfig(BOARD_SW_PORT, BOARD_SW_GPIO_PIN, kPORT_InterruptFallingEdge);
- /*Init the Tracking Sensor IO */
- /*
- * Sensor1 out of line and in line all in interrupt,
- * then need to check the site of the pin,if in line Turn to high speed ,else in half speed
- * Sensor2,3 only the Sensor in rising Well be interrupt
- * */
- //PORT_SetPinInterruptConfig(TRACK_SENSOR_PORT, TRACK_SENSOR1_GPIO_PIN, kPORT_InterruptEitherEdge);
- PORT_SetPinInterruptConfig(TRACK_SENSOR_PORT, TRACK_SENSOR2_GPIO_PIN, kPORT_InterruptRisingEdge);
- PORT_SetPinInterruptConfig(TRACK_SENSOR_PORT, TRACK_SENSOR3_GPIO_PIN, kPORT_InterruptRisingEdge);
- /*Enable of IRQ of the Sensor and Init all Sensor pin*/
- EnableIRQ(TRACK_SENSOR_IRQ);
- // GPIO_PinInit(TRACK_SENSOR_GPIO, TRACK_SENSOR1_GPIO_PIN, &Track_Sensor_config);
- GPIO_PinInit(TRACK_SENSOR_GPIO, TRACK_SENSOR2_GPIO_PIN, &Track_Sensor_config);
- GPIO_PinInit(TRACK_SENSOR_GPIO, TRACK_SENSOR3_GPIO_PIN, &Track_Sensor_config);
- /*for Start SW*/
- GPIO_PinInit(BOARD_SW_GPIO, BOARD_SW_GPIO_PIN, &sw_config);
- /* Select the clock source for the TPM counter as MCGPLLCLK */
- CLOCK_SetTpmClock(1U);
- TPM_GetDefaultConfig(&tpmInfo);
- /* Initialize TPM module */
- TPM_Init(MOTO_TPM_BASEADDR, &tpmInfo);
- TPM_SetupPwm(MOTO_TPM_BASEADDR, tpmParam, 2U, kTPM_EdgeAlignedPwm, 20000U, TPM_SOURCE_CLOCK);
- // car_move_control_t car_mover_config;
- // car_mover_config.CarLeftMotoDirection = 1;
- // car_mover_config.CarRightMotoDirection = 1;
- // car_mover_config.CarLeftMotoEnable = 50;
- // car_mover_config.CarRightMotoEnable = 20;
- /*wait for pass Sw4 for start */
- while( 0==SwForStart ){
- printf("pass the key for start \n");
- delay();
- delay();
- delay();
- }
- printf("init over \n");
- /*moto direct */
- MOTO_LEFT_RUN;
- MOTO_RIGHT_RUN;
- /*Start PWM*/
- TPM_StartTimer(MOTO_TPM_BASEADDR, kTPM_SystemClock);
- /* Enter an infinite loop for Run Auto */
- /*just for test pwm*/
- // for(;;){
- //
- // TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- // (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL,
- // kTPM_EdgeAlignedPwm,
- // 50);
- //
- // TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- // (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL,
- // kTPM_EdgeAlignedPwm,
- // 80);
- //
- // }
- for(;;){
- if(0 == AUTO_MODE){
- CarRun(0);
- }else if (1 == AUTO_MODE){
- CarRun(0);
- }else if (2 == AUTO_MODE){
- CarTurnLeft(1);
- }else if (3 == AUTO_MODE){
- CarTurnRight(1);
- }
- }
- return 0 ;
- }
复制代码
自动运动与远程控制的函数
- void CarTurnRight(char turnRightSpeed){
- uint8_t turnSpeed[2];
- if(1 == turnRightSpeed) {
- turnSpeed[0] = 80;
- turnSpeed[1] = 99;
- }else if (2 == turnRightSpeed) {
- turnSpeed[0] = 90;
- turnSpeed[1] = 99;
- }else {
- turnSpeed[0] = 95;
- turnSpeed[1] = 99;
- }
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- turnSpeed[0]);
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- turnSpeed[1]);
- }
- /**car TURN LEFT
- * turnLeftSpeed 1 high turn left
- * turnLeftSpeed 2 50% turn left
- * turnLefttSpeed 3 25% turn left
- * */
- void CarTurnLeft(char turnLeftSpeed){
- uint8_t turnSpeed[2];
- if (1 == turnLeftSpeed) {
- turnSpeed[0] = 80;
- turnSpeed[1] = 99;
- }else if (2 == turnLeftSpeed) {
- turnSpeed[0] = 90;
- turnSpeed[1] = 99;
- }else {
- turnSpeed[0] = 95;
- turnSpeed[1] = 99;
- }
- MOTO_LEFT_RUN;
- MOTO_RIGHT_RUN;
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- turnSpeed[1]);
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- turnSpeed[0]);
- }
- /**
- * Run mode
- * speed
- * depend on the
- * speed = 1
- * 1 = hight speed
- * 0 = Low speed
- * */
- void CarRun(bool Speed){
- uint8_t RunSpeed;
- if(1 == Speed){
- RunSpeed = 95;
- }else{
- RunSpeed = 99;
- }
- MOTO_LEFT_RUN;
- MOTO_RIGHT_RUN;
- /*Run with the speed */
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- RunSpeed);
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- RunSpeed);
- }
- typedef struct _car_move_control
- {
- bool CarLeftMotoDirection; /* left moto direction */
- bool CarRightMotoDirection; /* Output direction */
- char CarLeftMotoEnable; /* present of Left moto PWM */
- char CarRightMotoEnable; /* present of Right moto PWM */
- } car_move_control_t;
- void CarMoveControl(car_move_control_t *Car_Move_control){
- assert(Car_Move_control);
- //uint16_t _temp = car_move_control_t *Car_Move_control
- if(Car_Move_control->CarLeftMotoDirection) {
- MOTO_LEFT_RUN;
- }else {
- MOTO_LEFT_BACK;
- }
- if(Car_Move_control->CarRightMotoDirection){
- MOTO_RIGHT_RUN;
- }else {
- MOTO_RIGHT_BACK;
- }
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_LEFT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- Car_Move_control->CarLeftMotoEnable);
- TPM_UpdatePwmDutycycle( MOTO_TPM_BASEADDR,
- (tpm_chnl_t)MOTO_RIGHT_TPM_CHANNEL,
- kTPM_EdgeAlignedPwm,
- Car_Move_control->CarRightMotoEnable);
- }
复制代码
整机状态如下图。
周未由于撞坏了2个Sensor所以也改成2Sensor测试了,通过Sensor进中断,后分别调整两侧电机PWM,测试结果还是良好的。
来个视频吧。EE的视频咋加的,还不会,过一会儿再来补
此内容由EEWORLD论坛网友strong161原创,如需转载或用于商业用途需征得作者同意并注明出处
|
|