Old working code

Dependencies:   mbed QEI ros_lib_melodic

Committer:
florine_van
Date:
Fri Nov 15 13:23:56 2019 +0000
Revision:
12:0fa4c5a86c75
Parent:
11:35809512ec11
Child:
13:0be39d0abac7
Old working code for robot motor and sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
florine_van 0:57855aafa907 1 #include "mbed.h"
florine_van 7:2cf57f28255d 2 #include <ros.h>
florine_van 7:2cf57f28255d 3 #include <std_msgs/Empty.h>
florine_van 7:2cf57f28255d 4 #include <std_msgs/Int32.h>
florine_van 10:be9de79cf6b0 5 #include <std_msgs/String.h>
florine_van 8:57aa8a35d983 6 #include <mbed_custom_msgs/lidar_msg.h>
florine_van 5:8ef79eebbc97 7
florine_van 0:57855aafa907 8 #include "Sensor.h"
florine_van 5:8ef79eebbc97 9 #include "Motor.h"
florine_van 0:57855aafa907 10
florine_van 0:57855aafa907 11 // Set up serial to pc
florine_van 7:2cf57f28255d 12 //Serial pc(SERIAL_TX, SERIAL_RX);
florine_van 7:2cf57f28255d 13
florine_van 0:57855aafa907 14 // Set up I²C on the STM32 NUCLEO-401RE
florine_van 0:57855aafa907 15 #define addr1 (0x29)
florine_van 0:57855aafa907 16 #define addr2 (0x2A)
florine_van 0:57855aafa907 17 #define addr3 (0x2B)
florine_van 0:57855aafa907 18 #define addr4 (0x2C)
florine_van 0:57855aafa907 19
florine_van 6:858a5116688e 20 #define S1 PC_8
florine_van 6:858a5116688e 21 #define S2 PC_9
florine_van 6:858a5116688e 22 #define S3 PC_10
florine_van 6:858a5116688e 23 #define S4 PC_11
florine_van 6:858a5116688e 24 #define S5 PC_12
florine_van 6:858a5116688e 25 #define S6 PD_2
florine_van 6:858a5116688e 26 #define S7 PG_2
florine_van 6:858a5116688e 27 #define S8 PG_3
florine_van 6:858a5116688e 28
florine_van 5:8ef79eebbc97 29 // VL6180x sensors
florine_van 7:2cf57f28255d 30 Sensor sensor_back(I2C_SDA, I2C_SCL, S1);
florine_van 7:2cf57f28255d 31 Sensor sensor_left(I2C_SDA, I2C_SCL, S3);
florine_van 7:2cf57f28255d 32 Sensor sensor_forward(I2C_SDA, I2C_SCL, S5);
florine_van 7:2cf57f28255d 33 Sensor sensor_right(I2C_SDA, I2C_SCL, S7);
florine_van 5:8ef79eebbc97 34
florine_van 5:8ef79eebbc97 35 // Motors
florine_van 12:0fa4c5a86c75 36 Motor motor_left(PC_6, PB_15, PB_13);
florine_van 12:0fa4c5a86c75 37 Motor motor_right(PA_15, PC_7, PB_4);
florine_van 5:8ef79eebbc97 38
florine_van 10:be9de79cf6b0 39 void controlMotor(const std_msgs::Int32& motor_dir)
florine_van 10:be9de79cf6b0 40 {
florine_van 11:35809512ec11 41 switch(motor_dir.data) {
florine_van 11:35809512ec11 42 case 0:
florine_van 12:0fa4c5a86c75 43 motor_left.stop();
florine_van 12:0fa4c5a86c75 44 motor_right.stop();
florine_van 11:35809512ec11 45 break;
florine_van 11:35809512ec11 46
florine_van 10:be9de79cf6b0 47 // Move forward
florine_van 10:be9de79cf6b0 48 case 1:
florine_van 12:0fa4c5a86c75 49 motor_left.moveForward();
florine_van 12:0fa4c5a86c75 50 motor_right.moveForward();
florine_van 10:be9de79cf6b0 51 break;
florine_van 10:be9de79cf6b0 52
florine_van 10:be9de79cf6b0 53 // Move left
florine_van 10:be9de79cf6b0 54 case 2:
florine_van 12:0fa4c5a86c75 55 motor_left.moveBackward();
florine_van 12:0fa4c5a86c75 56 motor_right.moveForward();
florine_van 10:be9de79cf6b0 57 break;
florine_van 10:be9de79cf6b0 58
florine_van 10:be9de79cf6b0 59 // Move right
florine_van 10:be9de79cf6b0 60 case 3:
florine_van 12:0fa4c5a86c75 61 motor_left.moveForward();
florine_van 12:0fa4c5a86c75 62 motor_right.moveBackward();
florine_van 10:be9de79cf6b0 63 break;
florine_van 10:be9de79cf6b0 64 }
florine_van 10:be9de79cf6b0 65 }
florine_van 10:be9de79cf6b0 66
florine_van 5:8ef79eebbc97 67 int main()
florine_van 5:8ef79eebbc97 68 {
florine_van 7:2cf57f28255d 69 ros::NodeHandle nh;
florine_van 7:2cf57f28255d 70 nh.initNode();
florine_van 8:57aa8a35d983 71
florine_van 10:be9de79cf6b0 72 // ROS publisher for sensor readings
florine_van 12:0fa4c5a86c75 73 mbed_custom_msgs::lidar_msg lidar_msg;
florine_van 12:0fa4c5a86c75 74 ros::Publisher lidar_pub("lidar_reading", &lidar_msg);
florine_van 12:0fa4c5a86c75 75
florine_van 12:0fa4c5a86c75 76 // ROS subscriber for motors control
florine_van 12:0fa4c5a86c75 77 ros::Subscriber<std_msgs::Int32> motor_sub("motor_control", &controlMotor);
florine_van 11:35809512ec11 78
florine_van 8:57aa8a35d983 79 nh.advertise(lidar_pub);
florine_van 10:be9de79cf6b0 80 nh.subscribe(motor_sub);
florine_van 3:a3144a45f44c 81
florine_van 3:a3144a45f44c 82 // load settings onto VL6180X sensors
florine_van 6:858a5116688e 83 sensor_forward.init();
florine_van 3:a3144a45f44c 84 // change default address of sensor 2
florine_van 6:858a5116688e 85 sensor_forward.changeAddress(addr2);
florine_van 2:c537f1ebad7b 86
florine_van 6:858a5116688e 87 sensor_right.init();
florine_van 3:a3144a45f44c 88 // change default address of sensor 3
florine_van 6:858a5116688e 89 sensor_right.changeAddress(addr3);
florine_van 2:c537f1ebad7b 90
florine_van 6:858a5116688e 91 sensor_back.init();
florine_van 3:a3144a45f44c 92 // change default address of sensor 4
florine_van 6:858a5116688e 93 sensor_back.changeAddress(addr4);
florine_van 6:858a5116688e 94
florine_van 6:858a5116688e 95 sensor_left.init();
florine_van 2:c537f1ebad7b 96
florine_van 6:858a5116688e 97 //Set Speeds
florine_van 6:858a5116688e 98 motor_left.setSpeed(0.5f);
florine_van 6:858a5116688e 99 motor_right.setSpeed(0.5f);
florine_van 0:57855aafa907 100
florine_van 0:57855aafa907 101 while (1)
florine_van 10:be9de79cf6b0 102 {
florine_van 12:0fa4c5a86c75 103 lidar_msg.sensor_forward = sensor_forward.read();
florine_van 12:0fa4c5a86c75 104 lidar_msg.sensor_back = sensor_back.read();
florine_van 12:0fa4c5a86c75 105 lidar_msg.sensor_left = sensor_left.read();
florine_van 12:0fa4c5a86c75 106 lidar_msg.sensor_right = sensor_right.read();
florine_van 12:0fa4c5a86c75 107 lidar_pub.publish(&lidar_msg);
florine_van 10:be9de79cf6b0 108
florine_van 10:be9de79cf6b0 109 /*
florine_van 10:be9de79cf6b0 110 int range;
florine_van 10:be9de79cf6b0 111 range = sensor_forward.read();
florine_van 10:be9de79cf6b0 112 pc.printf("Range = %d\r\n", range);
florine_van 8:57aa8a35d983 113 */
florine_van 12:0fa4c5a86c75 114
florine_van 7:2cf57f28255d 115 nh.spinOnce();
florine_van 5:8ef79eebbc97 116
florine_van 4:cb50c6fa340b 117 wait_ms(10);
florine_van 0:57855aafa907 118 }
florine_van 0:57855aafa907 119 }
florine_van 0:57855aafa907 120
florine_van 5:8ef79eebbc97 121
florine_van 5:8ef79eebbc97 122